Converting GMT to local time

Converting GMT to local time

Post by Paul » Sat, 08 Feb 2003 05:56:57



I have the following "C" prorgam where I want to convert GMT into MY
localtime().

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

main()
{
    char *gmt ="20030206102245";
    char current_time[50];

    time_t      secs1;
    struct      tm tm1;

    memset (&tm1, 0, sizeof tm1);

    if (csstrptime(gmt, "YYYYMMDDHHMMSS", &tm1) != 0)
    {
        printf ("Call to csstrptime failed. Date = %s", gmt );
        return (1);
    }

    secs1 = mktime(&tm1);

    (void)strftime(current_time, sizeof(current_time),
        "%Y %m %d %H %M %S", localtime(&secs1));

    printf ("%s\n", current_time);

Quote:}

int
csstrptime(char *buf, char *format, struct tm *tm )
{

    int year=0, month=0, day=0, hour=0, minute=0, second=0;

    tm->tm_isdst = 0;
    if (!strcmp(format, "YYYYMMDDHHMMSS") &&
         (sscanf(buf, "%4d%2d%2d%2d%2d%2d",
            &year, &month, &day, &hour, &minute, &second) == 6))
    {
        tm->tm_year = year - 1900;
        tm->tm_mon = month - 1;
        tm->tm_mday = day;
        tm->tm_hour = hour;
        tm->tm_min = minute;
        tm->tm_sec = second;
    }
    else
        return (1);

Quote:}

When I run the program the printf() in the main prints 2003 02 06 10
22 45, which is the same as my GMT. The actual result I am looking for
should be 2003 02 06 05 22 45

Can somebody tell me what I need to do make this work. Thanks in
advance for all who answer this post.

 
 
 

Converting GMT to local time

Post by John Gordo » Sat, 08 Feb 2003 06:42:36



> When I run the program the printf() in the main prints 2003 02 06 10
> 22 45, which is the same as my GMT. The actual result I am looking for
> should be 2003 02 06 05 22 45

call me silly, but i don't see any code in your program that even
attempts to actually perform a time zone conversion.  were you
expecting strftime() to somehow magically do it for you?

Quote:> Can somebody tell me what I need to do make this work. Thanks in
> advance for all who answer this post.

you can switch between time zones by setting the TZ environment
variable to the appropriate string and then calling tzset().

---
John Gordon            "No Silicon Heaven?  Preposterous!  Where would


 
 
 

Converting GMT to local time

Post by Kurtis D. Rade » Sat, 08 Feb 2003 13:22:01



> I have the following "C" prorgam where I want to convert GMT into MY
> localtime().

> When I run the program the printf() in the main prints 2003 02 06 10 22
> 45, which is the same as my GMT. The actual result I am looking for
> should be 2003 02 06 05 22 45

> Can somebody tell me what I need to do make this work.

The mktime() function assumes the struct tm passed to it is in the local
time zone and converts it to a time_t in the UTC time zone. The localtime()
function converts a time_t in the UTC time zone to a struct tm in the local
time zone. You've done two conversions that are inverses of each other. So
of course the output is the same as the input :-)

To actually convert a time represented as a string in one timezone to the
local timezone involves manipulating the TZ environment variable and using
tzset() to make the manipulation visible to the standard time conversion
functions.

 
 
 

Converting GMT to local time

Post by Valentin Nechaye » Sat, 08 Feb 2003 16:43:56


PH> I have the following "C" prorgam where I want to convert GMT into MY
PH> localtime().

Check whether your system knows timegm(). If yes, you can convert GMT time into
time_t and then to localtime. Otherwise you should use some workaround.
E.g., you can emulate timegm() using 1) mktime() which treats broken-out time
description as localtime, 2) comparative function which counts difference
between two times described in struct tm, and 3) simple iterative algorithm
to walk to result.
In any case, conversion of GMT time to time_t requires only static formulas,
not reading platform-specific formats for timezones.

Another approach is to set TZ=GMT in environment, call tzset(), mktime(),
return previous TZ and call tzset() again. But it isn't thread safe way unless
wrapped by mutex, which means performance gain.

-netch-

 
 
 

Converting GMT to local time

Post by Valentin Nechaye » Sat, 08 Feb 2003 16:48:14


VN> Another approach is to set TZ=GMT in environment, call tzset(), mktime(),
VN> return previous TZ and call tzset() again. But it isn't thread safe way unless
VN> wrapped by mutex, which means performance gain.

Sorry, it should be noted also that standard localtime() also isn't thread
safe, so, if you hasn't its thread safe version, you have to serialize access
in this variant too. And, tzset() is expensive.

-netch-

 
 
 

Converting GMT to local time

Post by Paul » Sun, 09 Feb 2003 02:44:01


All thanks for your help :-)

If you are intersted here is my final soluition. I thought this was a
better  than mucking around with the TZ environment variable.

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

time_t timegm(struct tm *);
static char *gmt2localtime(struct tm *);

main()
{

    char *gmt ="20030206102245";
    struct      tm tm1;

    memset (&tm1, 0, sizeof tm1);

    if (strptime(gmt, "YYYYMMDDHHMMSS", &tm1) != 0)
    {
        printf ("Call to csstrptime failed. Date = %s", gmt );
        return (1);
    }

    printf ("gmt  time = %s\n", gmt );
    printf ("localtime = %s\n", gmt2localtime(&tm1) );

Quote:}

static char *
gmt2localtime(struct tm *t)
{
    static char localtim[50];
    time_t      ltime;

    ltime = timegm(t);
    if ( ltime == (time_t)-1 )
        strcpy (localtim, "-unknown time-");
    else
        (void)strftime(localtim, sizeof(localtim),
            "%Y%m%d%H%M%S", localtime(&ltime));

    return (localtim);

Quote:}

time_t
timegm(struct tm *t)
{

    time_t tl, tb;
    struct tm *tg;

    /*
    ** Convert into the number of seconds since 00:00:00, January 1,
1970
    ** according to the current timezone setting. See tzset().
    **
    ** if mktime() returns -1 that means that it cant convert the date
    ** passed in.
    */

    tl = mktime (t);
    if (tl == -1)
       return (time_t)-1;

    /*
    ** Convert directly to Coordinated Universal Time (UTC).
    */

    tg = gmtime (&tl);
    tg->tm_isdst = 0;

    tb = mktime (tg);
    if (tb == -1)
        return (time_t)-1;

    return (tl - (tb - tl));

Quote:}

int
strptime(char *buf, char *format, struct tm *tm )
{

    int year=0, month=0, day=0, hour=0, minute=0, second=0;

    tm->tm_isdst = 0;
    if (!strcmp(format, "YYYYMMDDHHMMSS") &&
         (sscanf(buf, "%4d%2d%2d%2d%2d%2d",
            &year, &month, &day, &hour, &minute, &second) == 6))
    {

       tm->tm_year = year - 1900;
       tm->tm_mon = month - 1;
       tm->tm_mday = day;
       tm->tm_hour = hour;
       tm->tm_min = minute;
       tm->tm_sec = second;
    }
    else
        return (1);

    return(0);

Quote:}

 
 
 

Converting GMT to local time

Post by Paul » Sun, 09 Feb 2003 02:46:30


All thanks for your help :-)

If you are interested below is my solution. I thought this better than
mucking around with tzset.

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

time_t timegm(struct tm *);
static char *gmt2localtime(struct tm *);

main()
{

    char *gmt ="20030206102245";
    struct      tm tm1;

    memset (&tm1, 0, sizeof tm1);

    if (strptime(gmt, "YYYYMMDDHHMMSS", &tm1) != 0)
    {
        printf ("Call to strptime failed. Date = %s", gmt );
        return (1);
    }

    printf ("gmt  time = %s\n", gmt );
    printf ("localtime = %s\n", gmt2localtime(&tm1) );

Quote:}

static char *
gmt2localtime(struct tm *t)
{
    static char localtim[50];
    time_t      ltime;

    ltime = timegm(t);
    if ( ltime == (time_t)-1 )
        strcpy (localtim, "-unknown time-");
    else
        (void)strftime(localtim, sizeof(localtim),
            "%Y%m%d%H%M%S", localtime(&ltime));

    return (localtim);

Quote:}

time_t
timegm(struct tm *t)
{

    time_t tl, tb;
    struct tm *tg;

    /*
    ** Convert into the number of seconds since 00:00:00, January 1,
1970
    ** according to the current timezone setting. See tzset().
    **
    ** if mktime() returns -1 that means that it cant convert the date
    ** passed in.
    */

    tl = mktime (t);
    if (tl == -1)
        return (time_t)-1;

    /*
    ** Convert directly to Coordinated Universal Time (UTC).
    */

    tg = gmtime (&tl);
    tg->tm_isdst = 0;

    tb = mktime (tg);
    if (tb == -1)
        return (time_t)-1;

    return (tl - (tb - tl));

Quote:}

int
strptime(char *buf, char *format, struct tm *tm )
{

    int year=0, month=0, day=0, hour=0, minute=0, second=0;

    tm->tm_isdst = 0;
    if (!strcmp(format, "YYYYMMDDHHMMSS") &&
         (sscanf(buf, "%4d%2d%2d%2d%2d%2d",
            &year, &month, &day, &hour, &minute, &second) == 6))
    {
        tm->tm_year = year - 1900;
        tm->tm_mon = month - 1;
        tm->tm_mday = day;
        tm->tm_hour = hour;
        tm->tm_min = minute;
        tm->tm_sec = second;
    }
    else
        return (1);

    return(0);

Quote:}

 
 
 

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

The ftcs unix mktime use the function tzset to initialize time
conversion information by setting an environment variable. That is not
thread-safe.

As I want to work in multi_threaded design with different time-zone, one

per each thread, I can't use the unix standard fonctions like mktime. I
need a thread-safe function that converts a local-time attached to its
timezone into a GMT time as follow:

    void localtime_to_gmt(const char * timezone,  const struct tm
localtime,  time_t * gmt_time);

Is there a  sofftware which uses instead of TZ env variable a local time

zone variable attached to the process to convert a local time to GMT ?

Thanks for your help ?

Fran?ois REYGAGNE. Software Architect.
opt[e]way S.A., 2881 route des Crtes, BP308
06906 Sophia Antipolis Cedex, FRANCE
tl: +33 (0)4 92 95 27 01    http://www.opteway.com

2. MOUSE BUSMOUSE ATI MACH32

3. 2.4.19-rc1 out now

4. convert UT time in local time / local time in UT time

5. IP Multipathing in Redhat 7.2

6. Converting a *future* time in another timezone to local time ...

7. redhat install through PCMCIA cdrom

8. GMT vs. Local Time -- Some help please!

9. GMT and local time manipulation in Solaris 2.x

10. Netscape sending out mail with GMT, not local time

11. How to get ls to show local time and not GMT

12. Syslogd times for local logs are GMT?