Strptime tm_gmtoff is always zero

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

void main () {
        struct tm tm;
        memset (&tm, '\0', sizeof(tm));
        if (!strptime ("15/Sep/2014:16:00:00 +0300", "%d/%b/%Y:%H:%M:%S%t%z", &tm)) {
                perror("strptime");
                return;
        }

        time_t gmt = timegm (&tm); // or mktime(), not relevant here
        printf("%d gmtoff=%lu %s %lu\n", tm.tm_hour, tm.tm_gmtoff, tm.tm_zone, gmt);

}

      

Result:

16 gmtoff=0 GMT 1410796800

      

I expected gmtoff to be 3.

It's like strptime, parsing the offset in one way or another, because it succeeds, but it doesn't fill the offset in the structure. I've tried any variation of this format string and datetime string with no luck.

According to http://lxr.devzen.net/source/xref/glibc/time/strptime_l.c#751 the structure field should be changed.

+3


source to share


1 answer


According to http://lxr.devzen.net/source/xref/glibc/time/strptime_l.c#751 the structure field should be changed.

It. In fact, it has changed twice. Nice! To demonstrate this to yourself, you can add this line just before timegm

:

printf("%lu\n", tm.tm_gmtoff);

      



You will notice that the bit after gmtoff

is different before and after the line timegm

. timegm

changes the structure you pass into. Before it is called, you get the expected values; then, you ... no.

Now, none of the documentation I could find says anything about this particular feature. This is why I prefer to use standard functions like mktime

; this is a bit more code, but what each function does is stated very well.

0


source







All Articles