2

I've a function which converts an RFC 822 timestamp to unixtime

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

int main() {

struct tm tm;
time_t unixtime;

strptime("Sun, 20 Feb 2011 10:28:02 +0800","%a, %e %h %Y %H:%M:%S %z",&tm);

unixtime = mktime(&tm);
printf("%d\n",unixtime);

return 0;
}

Problem: The timezone part (%z) doesn't seem to be working. I tried changing input timezone to other values +0100, + 0200 ect without changing other date values, it always gives the same unixtime stamp (ie, unixtimestamp corresponding to GMT)

What can be the issue here ?

Nands
  • 1,541
  • 2
  • 20
  • 33

1 Answers1

1

struct tm does not contain a timezone field. %z is supported simply to support the same flags as strftime. You'll need to extract and adjust the timezone offset manually.

Erik
  • 88,732
  • 13
  • 198
  • 189
  • Well, the man page for strptime says it supports %z via GNU extensions. check at the bottom of the page: http://linux.die.net/man/3/strptime – Nands Feb 21 '11 at 12:43
  • Yes, it supports *parsing of* these. That doesn't give struct tm a timezone member though. The man page (at least on my closest linux box) explicitly states "In most cases the corresponding fields are parsed, but no field in tm is changed." – Erik Feb 21 '11 at 12:46
  • Thanks for the clarification. I'll manually parse the timezone string and offset the required number of seconds. – Nands Feb 21 '11 at 18:00