| 1 |
#include <stdio.h>
|
| 2 |
#include <stdlib.h>
|
| 3 |
#include <time.h>
|
| 4 |
|
| 5 |
#define LINELEN 128
|
| 6 |
|
| 7 |
time_t parse_yearmonthday(char* str)
|
| 8 |
{
|
| 9 |
time_t date;
|
| 10 |
int day, month, year;
|
| 11 |
|
| 12 |
if (sscanf( str, "%d-%d-%d", &year, &month, &day) == 3 ) {
|
| 13 |
struct tm timestruct;
|
| 14 |
char *tz;
|
| 15 |
|
| 16 |
tz = getenv("TZ");
|
| 17 |
#ifdef HAVE_SETENV
|
| 18 |
setenv("TZ", "GMT", 1);
|
| 19 |
#else /* end of HAVE_SETENV */
|
| 20 |
putenv("TZ=GMT");
|
| 21 |
#endif /* else if not HAVE_SETENV */
|
| 22 |
tzset();
|
| 23 |
memset(×truct, 0, sizeof(timestruct));
|
| 24 |
timestruct.tm_mday = day;
|
| 25 |
timestruct.tm_mon = month - 1;
|
| 26 |
timestruct.tm_year = year - 1900;
|
| 27 |
date = mktime(×truct);
|
| 28 |
#ifdef HAVE_SETENV
|
| 29 |
if (tz)
|
| 30 |
setenv("TZ", tz, 1);
|
| 31 |
else
|
| 32 |
unsetenv("TZ");
|
| 33 |
#else /* end of HAVE_SETENV */
|
| 34 |
if (tz) {
|
| 35 |
char envstr[LINELEN];
|
| 36 |
snprintf(envstr, LINELEN, "TZ=%s", tz);
|
| 37 |
putenv(envstr);
|
| 38 |
} else
|
| 39 |
putenv("TZ=");
|
| 40 |
#endif /* else if not HAVE_SETENV */
|
| 41 |
tzset();
|
| 42 |
return date;
|
| 43 |
} else
|
| 44 |
return -1;
|
| 45 |
}
|
| 46 |
|
| 47 |
int main()
|
| 48 |
{
|
| 49 |
int t;
|
| 50 |
|
| 51 |
t = parse_yearmonthday("2003-04-02");
|
| 52 |
if (t == 1049241600) {
|
| 53 |
printf("OK.\n");
|
| 54 |
exit(0);
|
| 55 |
} else {
|
| 56 |
printf("Failed.\n");
|
| 57 |
exit(1);
|
| 58 |
}
|
| 59 |
}
|