Converting UTC time to local time 
Author Message
 Converting UTC time to local time

Dear comp.lang.c,

I'm puzzled about the use of the standard C facilities for converting from
UTC time to local time. On my system, gmtime() works as I expect yet
localtime() has no discernible effect. Is my system misconfigured
or have I greatly misunderstood time.h?

In particular, given a struct tm assumed to contain a UTC date/time,
I'd like to produce a time_t or struct tm containing the equivalent local
date/time. Applying localtime(mktime()) doesn't seem to do this on my system.

The test code attached below gives me output like this:
        Local time is Mon Feb  1 20:26:55 1999
        UTC time is Tue Feb  2 01:26:55 1999
        The time is Tue Feb  2 01:26:55 1999

Comments?

Thanks
-Lewis McCarthy

/* tested with cc under Irix 5.3 */

#include <time.h>
#include <stdio.h>

int main() {
        time_t time1, time2;
        char *theLocalTime;
        char *theUTCTime;
        char *theTime;

        tzset();

        time1 = time( NULL );
        theLocalTime = asctime( localtime( &time1 ));
        printf("Local time is %s", theLocalTime);

        theUTCTime = asctime( gmtime( &time1 ));
        printf("UTC time is %s", theUTCTime);

        time2 = mktime( gmtime( &time1 ));
        theTime = asctime( localtime( &time2 ));
        /* I expect to see theTime as local time, not UTC */
        printf("The time is %s", theTime);  

        exit(0);

Quote:
}



Fri, 20 Jul 2001 03:00:00 GMT  
 Converting UTC time to local time
: Dear comp.lang.c,

: I'm puzzled about the use of the standard C facilities for converting from
: UTC time to local time. On my system, gmtime() works as I expect yet
: localtime() has no discernible effect. Is my system misconfigured
: or have I greatly misunderstood time.h?

: In particular, given a struct tm assumed to contain a UTC date/time,
: I'd like to produce a time_t or struct tm containing the equivalent local
: date/time. Applying localtime(mktime()) doesn't seem to do this on my system.

: The test code attached below gives me output like this:
:       Local time is Mon Feb  1 20:26:55 1999
:       UTC time is Tue Feb  2 01:26:55 1999
:       The time is Tue Feb  2 01:26:55 1999

You may need either an environmental variable TZ, or a (non-ANSI)
function tzset().  Check your man pages.

Will



Sat, 21 Jul 2001 03:00:00 GMT  
 Converting UTC time to local time
Hi,

Quote:

>: In particular, given a struct tm assumed to contain a UTC date/time,
>: I'd like to produce a time_t or struct tm containing the equivalent
>: local date/time. Applying localtime(mktime()) doesn't seem to do this
>: on my system.

>You may need either an environmental variable TZ, or a (non-ANSI)
>function tzset().  Check your man pages.

TZ is set correctly and tzset() seems to work fine. But I still
can't get back to local time from UTC time.

-Lewis

Sample output:
  Local time zone name is EST EDT, 18000 seconds from UTC time.
  Local time is Tue Feb  2 16:37:40 1999
  UTC time is Tue Feb  2 21:37:40 1999
  The time is Tue Feb  2 21:37:40 1999  [here I want/expect 16:37]

/* test program compiled with cc -O3 under Irix 5.3 */

#include <time.h>
#include <stdio.h>

int main() {
        time_t time1, time2;
        char *theLocalTime;
        char *theUTCTime;
        char *theTime;

        extern char *tzname[2];
        extern time_t timezone;

        tzset();
        printf("Local time zone name is %s %s, ", tzname[0], tzname[1]);
        printf("%ld seconds from UTC time.\n", (long int)timezone);

        time1 = time( NULL );
        theLocalTime = asctime( localtime( &time1 ));
        printf("Local time is %s", theLocalTime);

        theUTCTime = asctime( gmtime( &time1 ));
        printf("UTC time is %s", theUTCTime);

        time2 = mktime( gmtime( &time1 ));
        theTime = asctime( localtime( &time2 ));
        printf("The time is %s", theTime);  

        exit(0);

Quote:
}



Sat, 21 Jul 2001 03:00:00 GMT  
 Converting UTC time to local time
Groovy hepcat Lewis McCarthy was jivin' on 1 Feb 1999 20:53:31 -0500
in comp.lang.c.
Converting UTC time to local time's a cool scene! Dig it!

Quote:
>I'm puzzled about the use of the standard C facilities for converting from
>UTC time to local time. On my system, gmtime() works as I expect yet
>localtime() has no discernible effect. Is my system misconfigured
>or have I greatly misunderstood time.h?

>In particular, given a struct tm assumed to contain a UTC date/time,
>I'd like to produce a time_t or struct tm containing the equivalent local
>date/time. Applying localtime(mktime()) doesn't seem to do this on my system.

  You seem to be a little confused about localtime() and mktime().
They do not change a time's representation from UTC to local time. The
argument to localtime() is a pointer to time_t representing the local
time. The argument to mktime() is a pointer to struct tm representing
the local time. No conversion from UTC to local lime is done by these
functions.
  The only way I can think of to convert from UTC to local time is to
subtract the time zone. (Time zones west of Greenwitch are considered
to be positive, while eastern zones are considered negative.) Of
course, that won't help if you can't find the time zone.
  Anyhow, the following might give you some idea how this works. It's
far from perfect, and may give erroneous output under some conditions.
But this is just a demonstration of a basic concept, not a perfectly
bug free program. I defined the macro TZ as -10 because that is the
time zone in which I live, 10 hours east of Greenwitch.

#include <time.h>
#include <stdio.h>

#define TZ -10    /* my time zone */

int main(void)
{
  time_t time1, time2;
  char *s;
  struct tm *tp;

  time1 = time(NULL);
  s = asctime(localtime(&time1));
  printf("Local time is %s", s);

  s = asctime(gmtime(&time1));
  printf("UTC time is    %s", s);

  time2 = mktime(gmtime(&time1));
  tp = localtime(&time2);
  tp->tm_hour -= TZ;
  if(tp->tm_hour > 23)
  {
    tp->tm_hour -= 24;
    tp->tm_mday++;
    tp->tm_wday++;
    tp->tm_yday++;
  }
  s = asctime(tp);
  printf("The time is    %s", s);  

  return 0;

Quote:
}

  From this program I get the following output:

Local time is Thu Feb  4 00:27:55 1999
UTC time is   Wed Feb  3 14:27:55 1999
The time is   Rhu Feb  4 00:27:55 1999
--

----- Dig the EVEN NEWER, MORE IMPROVED news sig!! -----

-------------- Shaggy was here! ---------------
    http://aardvark.apana.org.au/~phaywood/
============= Ain't I'm a dawg!! ==============



Mon, 23 Jul 2001 03:00:00 GMT  
 Converting UTC time to local time
Quote:
Peter "Shaggy" Haywood writes:
>  You seem to be a little confused about localtime() and mktime().
>They do not change a time's representation from UTC to local time. The
>argument to localtime() is a pointer to time_t representing the local
>time. The argument to mktime() is a pointer to struct tm representing
>the local time. No conversion from UTC to local lime is done by these
>functions.
>  The only way I can think of to convert from UTC to local time is to
>subtract the time zone.

[...code elided...]

Thanks much, this is the kind of answer I sought!
In the interests of portability, I'm going to stick to UTC time in my
application.

-Lewis



Mon, 23 Jul 2001 03:00:00 GMT  
 Converting UTC time to local time

: Groovy hepcat Lewis McCarthy was jivin' on 1 Feb 1999 20:53:31 -0500
: in comp.lang.c.
: Converting UTC time to local time's a cool scene! Dig it!

: >I'm puzzled about the use of the standard C facilities for converting from
: >UTC time to local time. On my system, gmtime() works as I expect yet
: >localtime() has no discernible effect. Is my system misconfigured
: >or have I greatly misunderstood time.h?
: >
: >In particular, given a struct tm assumed to contain a UTC date/time,
: >I'd like to produce a time_t or struct tm containing the equivalent local
: >date/time. Applying localtime(mktime()) doesn't seem to do this on my system.

:   You seem to be a little confused about localtime() and mktime().
: They do not change a time's representation from UTC to local time. The
: argument to localtime() is a pointer to time_t representing the local
: time. The argument to mktime() is a pointer to struct tm representing
: the local time. No conversion from UTC to local lime is done by these
: functions.
:   The only way I can think of to convert from UTC to local time is to
: subtract the time zone. (Time zones west of Greenwitch are considered
: to be positive, while eastern zones are considered negative.) Of
: course, that won't help if you can't find the time zone.

time() returns calendar time in a time_t, localtime() converts a
calendar time_t to a struct tm of local time, and gmtime() converts
a calendar time_t to a struct tm of UTC.  mktime() converts a
struct tm in local time to a time_t in calendar time.

Will



Tue, 24 Jul 2001 03:00:00 GMT  
 Converting UTC time to local time

Quote:
Will Rose writes:
>time() returns calendar time in a time_t, localtime() converts a
>calendar time_t to a struct tm of local time, and gmtime() converts
>a calendar time_t to a struct tm of UTC.  mktime() converts a
>struct tm in local time to a time_t in calendar time.

Thanks very much for your help. My local manpage for mktime()
doesn't (IMHO) clearly explain that assumption on the function's
input.

Lewis
--
"we have to yet really seriously debate the constitutional issues and
whether or not we're willing to give up more freedom in order to have
more security" -- U.S. Secretary of Defense William Cohen, 3 Feb 1999
--
"we have to yet really seriously debate the constitutional issues and
whether or not we're willing to give up more freedom in order to have
more security" -- U.S. Secretary of Defense William Cohen, 3 Feb 1999



Wed, 25 Jul 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

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

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

3. converting UTC to local time

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

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

6. How to convert GMT time to local time?

7. local Time <-> UTC

8. local Time <-> UTC

9. UTC time from time server

10. convert time to UTC format

11. Time: UCT, local time, daylight - any standards?

12. Time: UCT, local time, daylight - any standards?

 

 
Powered by phpBB® Forum Software