002
07.12.2006, 13:41 Uhr
0xdeadbeef
Gott (Operator)
|
Rund über den Daumen so:
C++: |
#include <ctype.h> #include <stdio.h> #include <string.h> #include <time.h>
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))
static struct { char const *const str; int num; } const months[] = { { "jan", 0 }, { "feb", 1 }, { "mar", 2 }, { "apr", 3 }, { "may", 4 }, { "jun", 5 }, { "jul", 6 }, { "aug", 7 }, { "sep", 8 }, { "oct", 9 }, { "okt", 9 }, { "nov", 10 }, { "dec", 11 }, { "dez", 11 } };
void make_lower(char *dest, char const *src) { while(*dest++ = tolower(*src++)); }
int map_month(char const *str) { int i; for(i = 0; i < ARRAY_SIZE(months); ++i) { if(!strcmp(str, months[i].str)) return months[i].num; }
return -1; }
int main(void) { static char const *const input = "25/Feb/2006:01:00:18 +0100";
struct tm my_tm = { 0 }; char month[4]; char month_lower[4]; char tz_sign; int timezone; time_t timestamp;
sscanf(input, "%d/%3s/%d:%d:%d:%d %c%d", &my_tm.tm_mday, month, &my_tm.tm_year, &my_tm.tm_hour, &my_tm.tm_min, &my_tm.tm_sec, &tz_sign, &timezone);
timezone /= 100; if(tz_sign == '-') timezone = -timezone;
my_tm.tm_hour -= timezone; make_lower(month_lower, month); my_tm.tm_mon = map_month(month_lower);
timestamp = mktime(&my_tm);
printf("%d\n", timestamp);
return 0; }
|
-- Einfachheit ist Voraussetzung für Zuverlässigkeit. -- Edsger Wybe Dijkstra |