/* * $d$ * www.pccl.demon.co.uk * * Program to interpret time since Unix epoch. */ #include #include #include #include #include #include extern char *basename (); static char *sProgname; static void usage (void) { (void) printf ("Usage: %s [-h] [-M] time...\n", sProgname); } static void help (void) { usage (); (void ) printf (" -h help.\n"); (void ) printf (" -m input is in milliseconds since midnight.\n"); (void ) printf (" time input times (dec, hex or oct).\n"); (void ) printf (" default - secs since Unix epoch\n"); } static void display (char * aText, time_t aTime, int aDisplayDate) { struct tm *lTm = gmtime (&aTime); char *lTextTime; if (aDisplayDate) { lTm = gmtime (&aTime); lTextTime = asctime (lTm); (void) printf ("%s interpreted as %ld (0x%lx) - GMT: %s", aText, aTime, aTime, lTextTime); lTm = localtime (&aTime); lTextTime = asctime (lTm); (void) printf ("%s interpreted as %ld (0x%lx) - local: %s", aText, aTime, aTime, lTextTime); } else { lTm = gmtime (&aTime); (void) printf ("%s interpreted as %ld (0x%lx) - GMT: %02d:%02d:%02d\n", aText, aTime, aTime, lTm->tm_hour, lTm->tm_min, lTm->tm_sec); lTm = localtime (&aTime); (void) printf ("%s interpreted as %ld (0x%lx) - local: %02d:%02d:%02d\n", aText, aTime, aTime, lTm->tm_hour, lTm->tm_min, lTm->tm_sec); } } main (int argc, char *argv[]) { int c; int lMidnight = 0; time_t lNow; sProgname = basename (argv[0]); while ((c = getopt (argc, argv, "hm")) != EOF) { switch (c) { case 'h': { help (); exit (0);; } case 'm': { lMidnight = 1; break; } case '?': { usage (); exit (EINVAL); } } } (void) time (&lNow); display ("Time now", lNow, 1); for (; optind < argc; optind++) { long lInputTime = strtoul (argv[optind], NULL, 0); if (lMidnight) { display (argv[optind], lInputTime / 1000, 0); } else { display (argv[optind], lInputTime, 1); } } }