Thank you Paul and Zefram.
Well my bad i missed to update the code with seconds while calling localtime.
Even after that things didn't work for me.
Code
cat test_tz_simple.c
#include
<stdlib.h>
#include <string.h>
#include <time.h>
#include <syslog.h>
#include <sys/time.h> /* clude defn. of gettimeofday */
#include <sys/types.h>
int main()
{
u_int64_t time = 0;
struct timeval tv;
struct tm *tm;
int gmtoff, gmtoff_hr, gmtoff_min;
char *gmtoff_sign;
if ( gettimeofday(&tv, NULL) == 0)
{
time = (long long) tv.tv_sec;
}
tm = localtime(&tv.tv_sec);
printf ("epoch:%lld, %lld-%02d-%02d %02d:%02d:%02d gmtoff: %ld,
TZ: %s\n",
time, tm->tm_year + 1900LL, tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec,
tm->tm_gmtoff, tm->tm_zone);
if ( tm->tm_gmtoff < 0 )
{
gmtoff = (0 - tm->tm_gmtoff );
gmtoff_sign = "-";
}
else
{
gmtoff = tm->tm_gmtoff;
gmtoff_sign = "+";
}
gmtoff_hr = gmtoff/3600;
gmtoff_min = gmtoff/60 - gmtoff_hr*60;
printf("Time: %04d-%02d-%02d %02d:%02d:%02d.%03d %s%02d%02d \n",
(tm->tm_year + 1900),
tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min,
tm->tm_sec, time,
gmtoff_sign, gmtoff_hr, gmtoff_min);
}
Regarding setting TZ variable , we use customized linux platform. The way time zone is set is as follows
$ cat
/etc/sysconfig/clock
UTC=true
ARC=false
ZONE=Asia/Manila
$ls -ltr
/etc/localtime
lrwxrwxrwx 1 root root 31 Oct 26 12:31 /etc/localtime ->
/usr/share/zoneinfo/Asia/Manila
I tried export TZ=Asia/Manila before running a.out. But no luck.
surprisingly, when i copy back 2016g version of tzdb binary, a.out gives correct result.
$ cksum
/usr/share/zoneinfo/Asia/Manila_2016
2614106258 319 /usr/share/zoneinfo/Asia/Manila_2016
Output with 2016g tzdb
$ ./a.out
epoch:1509609002, 2017-11-02 15:50:02 gmtoff: 28800, TZ: PHT
Time: 2017-11-02 15:50:02.1509609002 +0800
$
$ cksum
/usr/share/zoneinfo/Asia/Manila_2017
2995436782 311 /usr/share/zoneinfo/Asia/Manila_2017
Output with 2017a tzdb
$./a.out
epoch:1509608903,
2017-11-02 07:48:23 gmtoff: 0, TZ:
Time: 2017-11-02 07:48:23.1509608903 +0000
On 11/01/2017 01:12 PM, Zefram wrote:
localtime() operates on a time_t, the value of
which you need to get from time() or from the .tv_sec part of what you
get from gettimeofday(). Instead you've taken a correct .tv_sec and
multiplied it by 1000. Instead of asking about November 2017 you've
asked about October 49804. A zero offset is still not what the tzdb
projects for 49804, but localtime() can be forgiven for giving up.
Although that is indeed a problem with the program, I suspect that there's some other issue as well. I just now compiled and ran the following simpler variant of the program on Fedora 26 x86-64 (64-bit long and time_t), linked to the current tzdb code and data:
#include <time.h>
#include <stdio.h>
int
main (void)
{
time_t time = 1509521131510;
long long ltime = time;
struct tm *tm = localtime (&time);
printf ("epoch:%lld, %lld-%02d-%02d %02d:%02d:%02d gmtoff: %ld, TZ: %s\n",
ltime, tm->tm_year + 1900LL, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec,
tm->tm_gmtoff, tm->tm_zone);
return 0;
}
With the TZ environment variable set to "Asia/Manila", it outputs the expected value:
epoch:1509521131510, 49804-10-27 17:25:10 gmtoff: 28800, TZ: +08
With the TZ environment variable unset, it outputs:
epoch:1509521131510, 49804-10-27 09:25:10 gmtoff: 0, TZ: GMT
which are the reported symptoms. But the latter output is also correct, since the default wall-clock time, if you just install tzdb without any special configuration and do not set TZ, is the same as if you had set TZ="GMT0".
Learn how to protect users, data, and applications with security engineered for the public cloud by Barracuda. http://barracuda.com