#include "date.h" #include <stdlib.h> extern struct tz_tab tzs; extern char *tzname[2]; extern time_t timezone; extern int daylight; int main (int argc, char *argv[]) { char tt[200]; char *pt; char *t; int load_status; int shift; int i; int error; time_t res; /********************************************************/ /* Chek command line */ /********************************************************/ if (argc != 4) { fprintf (stdout, "\nUsage: date2timestamp.test <date> <shift> <tz conf>\n"); fprintf (stdout, "\n-> \"date au format Tue Aug 28 01:02:24 MET DST 2001\""); fprintf (stdout, "\n-> if shift = 86400, then automatic time shift requested"); fprintf (stdout, "\n-> 'tz conf' points to the time zones configuration file\n\n"); return 1; } /********************************************************/ /* Set local time zone to UTC */ /********************************************************/ if (set_local_TZ_to_GMT()) { fprintf (stderr, "\nCan not set local time zone to UTC!!!\n"); return 1; } /********************************************************/ /* Load time zones configuration file */ /********************************************************/ load_status = load_timezones(argv[3]); if (load_status != TIME_ZONES_LOAD_OK) { fprintf (stderr, "\nFailure loading TZ configuration, error code is %d\n", load_status); return 1; } fprintf (stdout, "\n\nConfiration --- time zones are: \n\n"); for (i=0; i<tzs.entry_number; i++) { fprintf (stdout, "\n[%d]", i); fprintf (stdout, "\n timezone: %s", ((tzs.tz)[i]).timezone); fprintf (stdout, "\n daylight: %s", ((tzs.tz)[i]).daylight); fprintf (stdout, "\n shift: %d", ((tzs.tz)[i]).shift); } /********************************************************/ /* Compute timestamp */ /* If shift = AUTOMATIC_TIME_SHIFT_COMPUTATION (86400) */ /* then automatic shift discovery is requested. */ /********************************************************/ shift = atoi(argv[2]); fprintf (stdout, "\n\nRequested shift is: %d\n\n", shift); fflush (stdout); t = (char*)malloc(strlen(argv[1])+1); if (t == NULL) { fprintf (stdout, "\n%s - Out of memory", argv[1]); return 1; } strcpy (t, argv[1]); pt = date2timestamp (argv[1], tt, 200, shift); if (pt == NULL) { free (t); fprintf (stdout, "\n%s - Invalide date", argv[1]); return 1; } fprintf (stdout, "\n%s - %s\n", t, tt); res = (time_t)string2unsigned_int (tt, &error); if (error) { fprintf (stdout, "\nCan not convert %s\n", tt); return 1; } fprintf (stdout, "\nReversed date is: [%s]\n", ctime(&res)); fprintf (stdout, "\ntzname[0] = %s", tzname[0]); fprintf (stdout, "\ntzname[1] = %s", tzname[1]); fprintf (stdout, "\ndaylight = %d", daylight); fprintf (stdout, "\ntimezone = %lu", timezone); fprintf (stdout, "\n\n"); free (t); return 0; }