Strptime in c with time offsets

I'm having trouble finding a way to parse the time zone from strings, for example: "Thu, Sep 1, 2011 09:06:03 AM -0400 (EDT)"

What I need to do in a larger scheme of my program, take char * and convert it to time_t. The following is a simple test program that I wrote to try to find out that strptime takes into account the time zone in general, and it doesn't seem to be so (when this test program runs all the printed numbers are the same, when they should be different). Suggestions?

I also tried using GNU getdate and getdate_r because it seems like the best option for possible flexible formats, but I got an implicit function warning from the compiler, implying that I did not include the correct libraries. Is there anything else I need to include #include to use getdate?

#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include <stdio.h>

#ifndef __USE_XOPEN
#define __USE_XOPEN
#endif
#include <time.h>

int main (int argc, char** argv){

char *timestr1, *timestr2, *timestr3;
struct tm time1, time2, time3;
time_t timestamp1, timestamp2, timestamp3;

timestr1 = "Thu, 1 Sep 2011 09:06:03 -0400 (EDT)"; // -4, and with timezone name
timestr2 = "Thu, 1 Sep 2011 09:06:03 -0000"; // -0

strptime(timestr1, "%a, %d %b %Y %H:%M:%S %Z", &time1); //includes UTC offset
strptime(timestr2, "%a, %d %b %Y %H:%M:%S %Z", &time2); //different UTC offset
strptime(timestr1, "%a, %d %b %Y %H:%M:%S", &time3); //ignores UTC offset

time1.tm_isdst = -1;
timestamp1 = mktime(&time1);
time2.tm_isdst = -1;
timestamp2 = mktime(&time2);
time3.tm_isdst = -1;
timestamp3 = mktime(&time3);

printf("Hello \n");
printf("%d\n%d\n%d\n", timestamp1, timestamp2, timestamp3);
printf("Check the numbers \n");
return 0;
}
+5
source share
1 answer

On MacOS X (10.7.1), one of the “errors” listed on the manual page strptime(3):

% Z       " ". - .       EST, , .

- timestamp ( 1989 . 1.1) strptime ( 2007 . 1.1). strftime() strptime().

MacOS X, :

$ strptime -T '%Y-%m-%dT%H:%M:%S %Z' 2011-09-08T21:00:00PDT
1315540800 = 2011-09-08T21:00:00PDT
$ timestamp 1315540800
1315540800 = Thu Sep 08 21:00:00 2011
$ timestamp -u 1315540800
1315540800 = Fri Sep 09 04:00:00 2011
$ strptime -T '%Y-%m-%dT%H:%M:%S %Z' 2011-09-08T21:00:00GMT
1315515600 = 2011-09-08T21:00:00GMT
$ timestamp 1315515600
1315515600 = Thu Sep 08 14:00:00 2011
$ timestamp -T '%Y-%m-%dT%H:%M:%S %z' 1315515600
1315515600 = 2011-09-08T14:00:00 -0700
$

/ ( /Los _Angeles, Pacific Daylight Time, UTC-07: 00), , ( ) strptime() PDT GMT . -u timestamp " UTC ( GMT)", . 9 UTC - 2 PDT.

, - . .

+3

All Articles