converting UTC to local time 
Author Message
 converting UTC to local time

Not sure, but check the global variables tzname and
timezone?  (First is a char[2], second the difference
in second.)  These are set after a call to localtime().
Are they correct for you?
Quote:

> I'm writing a program that gets the time in UTC from the atomic clock
> at Boulder Colo. and updates my cpu's clock. I've tried a couple of
> things to convert from UTC to localtime but it isn't working. Here's
> the pertinant section of code. timestr is the the string recieved from
> the server.

>         sscanf(timestr, "%s %s %s %s", julian, calendar, time, dors);

>         if(strcmp(dors, "00") == 0)
>                 t.tm_isdst = 0;
>         else
>                 t.tm_isdst = 1;
>         memcpy(year, calendar, 2);
>         memcpy(month, calendar + 3, 2);
>         memcpy(day, calendar + 6, 2);
>         memcpy(hour, time, 2);
>         memcpy(minute, time + 3, 2);
>         memcpy(second, time + 6, 2);

>         t.tm_year = atoi(year);
>         t.tm_mon = atoi(month);
>         t.tm_mday = atoi(day);
>         t.tm_hour = atoi(hour);
>         t.tm_min = atoi(minute);
>         t.tm_sec = atoi(second);

>         tt = mktime(&t);
>         lt = localtime(&tt);

>         printf("%d:%d:%d   %d-%d-%d\n",
>                 lt->tm_hour, lt->tm_min, lt->tm_sec,
>                 lt->tm_mon, lt->tm_mday, lt->tm_year);

> As you can see my (evidently) lame brained attempt involves passing
> the struct tm t's address to mktime and then the address of the time_t
> value to localtime. It prints out UTC. If I reverse the dst settings
> to pretend we're still on DST it will subtract an hour as I would
> expect. However it isn't adjusting for my time zone. What am I missing
> here?
> TIA and regards,
> Greg Martin.



Fri, 19 Apr 2002 03:00:00 GMT  
 converting UTC to local time


Quote:
>Not sure, but check the global variables tzname and
>timezone?  (First is a char[2], second the difference
>in second.)  These are set after a call to localtime().
>Are they correct for you?

Thanks Andomar.
Yes, they are right. I guess for the winsock version I can use the the
_timezone value but I would have thought it unneccessary. I want to
write a version for BSD Sockets as well so it would be nice to know
the problem. My FreeBSD machine keeps pretty poor time. (for a
computer - compared to a hour glass it's pretty good  :-)
Greg.


Fri, 19 Apr 2002 03:00:00 GMT  
 converting UTC to local time
I'm writing a program that gets the time in UTC from the atomic clock
at Boulder Colo. and updates my cpu's clock. I've tried a couple of
things to convert from UTC to localtime but it isn't working. Here's
the pertinant section of code. timestr is the the string recieved from
the server.

        sscanf(timestr, "%s %s %s %s", julian, calendar, time, dors);

        if(strcmp(dors, "00") == 0)
                t.tm_isdst = 0;
        else
                t.tm_isdst = 1;
        memcpy(year, calendar, 2);
        memcpy(month, calendar + 3, 2);
        memcpy(day, calendar + 6, 2);
        memcpy(hour, time, 2);
        memcpy(minute, time + 3, 2);
        memcpy(second, time + 6, 2);

        t.tm_year = atoi(year);
        t.tm_mon = atoi(month);
        t.tm_mday = atoi(day);
        t.tm_hour = atoi(hour);
        t.tm_min = atoi(minute);
        t.tm_sec = atoi(second);

        tt = mktime(&t);
        lt = localtime(&tt);

        printf("%d:%d:%d   %d-%d-%d\n",
                lt->tm_hour, lt->tm_min, lt->tm_sec,
                lt->tm_mon, lt->tm_mday, lt->tm_year);

As you can see my (evidently) lame brained attempt involves passing
the struct tm t's address to mktime and then the address of the time_t
value to localtime. It prints out UTC. If I reverse the dst settings
to pretend we're still on DST it will subtract an hour as I would
expect. However it isn't adjusting for my time zone. What am I missing
here?
TIA and regards,
Greg Martin.



Sat, 20 Apr 2002 03:00:00 GMT  
 converting UTC to local time


Quote:


> >Not sure, but check the global variables tzname and
> >timezone?  (First is a char[2], second the difference
> >in second.)  These are set after a call to localtime().
> >Are they correct for you?

> Thanks Andomar.
> Yes, they are right. I guess for the winsock version I can use the the
> _timezone value but I would have thought it unneccessary. I want to
> write a version for BSD Sockets as well so it would be nice to know
> the problem. My FreeBSD machine keeps pretty poor time. (for a
> computer - compared to a hour glass it's pretty good  :-)

how about fiddling with the tm_isdst field in struct tm. I havn't tried
it so I havn't a clue if it will work.

--
Of course I'm going to be in an aeroplane on 31st December 1999.
You scientists wouldn't be stupid enough to build things that
won't work.
Besides what have computers got to do with aeroplanes anyway?

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 20 Apr 2002 03:00:00 GMT  
 converting UTC to local time
On Tue, 02 Nov 1999 16:38:18 GMT, Nick Keighley

Quote:





>> >Not sure, but check the global variables tzname and
>> >timezone?  (First is a char[2], second the difference
>> >in second.)  These are set after a call to localtime().
>> >Are they correct for you?

>> Thanks Andomar.
>> Yes, they are right. I guess for the winsock version I can use the the
>> _timezone value but I would have thought it unneccessary. I want to
>> write a version for BSD Sockets as well so it would be nice to know
>> the problem. My FreeBSD machine keeps pretty poor time. (for a
>> computer - compared to a hour glass it's pretty good  :-)

>how about fiddling with the tm_isdst field in struct tm. I havn't tried
>it so I havn't a clue if it will work.

Hi Nick.
That field toggles the Daylight Savings Time adjustment and it's the
one that works. Chris Torek e-mailed me and explained that the way the
time functions are implemented doesn't allow for conversion from UTC
to local time. I had to write my own logic for this. Hopefully I've
caught all possible permutations
Regards,
Greg Martin.


Sat, 20 Apr 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How to convert local time with specific time zone to UTC time in Win32 API

2. Converting UTC time to local time

3. converting from Local Time to UTC time...

4. How to convert local time to gmt using a local variable time zone per process/thread

5. local Time <-> UTC

6. local Time <-> UTC

7. convert time to UTC format

8. Convert local time to gmt time without using TimeZone Env variable

9. How to convert GMT time to local time?

10. UTC time from time server

11. How to convert local time to milliseconds?

12. Q: How to convert GMT to local time?

 

 
Powered by phpBB® Forum Software