/* -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 */ /******************************************************************************* * c0xcaltest.c * Test driver for proposed ISO/IEC C (C0X) calendar types and functions. * * This source file tests the implementation of several proposed calendar * functions. * * Usage * c0xcaltest [-z] [+formatstring]... * * Notes * This is written to conform with ISO C89. * * References * See . * * See also * c0xcalendar.c * c0xcalendar.h * c0xtimezone.c * *------------------------------------------------------------------------------- * History * 1.0, 2002-01-26, David R Tribble. * First attempt. * * 1.1, 2002-01-28, David R Tribble. * Finished the Unix includes and function calls. * * 1.2, 2002-01-29, David R Tribble. * Added the optional '+formatstring' command argument. * * 1.3, 2002-02-14, David R Tribble. * Rename constant '_TZ_GMT' to '_TZ_UTC'. * Display the time in both UTC and the local timezone. * * 1.4, 2002-02-17, David R Tribble. * Added tests for the improved '_gettimezonelist()' function. *******************************************************************************/ /* Identification */ static const char rev[] = "@(#)$Header: c0xcaltest.c 1.4, 2002-02-17 $"; /* Standard includes */ #include #include #include #include #include #include #if defined(_WIN32) #define WIN32_LEAN_AND_MEAN #include #elif defined(unix) or defined(_unix) or defined(__unix) or defined(__unix__) #undef unix #define unix 1 #include #endif /* Local includes */ #include "c0xcalendar.h" /******************************************************************************* * Private types *******************************************************************************/ struct timeinfo { time_t ti_time; /* Current system time */ long ti_nsec; /* Nanoseconds */ }; /******************************************************************************* * Private functions *******************************************************************************/ /*------------------------------------------------------------------------------ * list_zones() * Display a list of supported timezones. */ static void list_zones(void) { const struct _timezone ** tz; size_t n; size_t i; printf("Timezones:\n"); /* Determine the number of supported timezones */ n = _gettimezonelist(NULL, 0); if (n < 1) { printf(" No timezones are supported\n"); return; } /* Allocate and fill a list of timezones */ tz = malloc(n * sizeof(const struct _timezone *)); n = _gettimezonelist(tz, n); /* Print the timezone list */ for (i = 0; i < n; i++) printf(" %2u. %s \"%s\"\n", i, tz[i]->tz_abbr, tz[i]->tz_name); printf("\n"); /* Clean up */ free((void *)tz); } /*------------------------------------------------------------------------------ * get_tod() * Determine the current system time. * * Parameter t * Points to a time structure that is filled with the system time, as a * 'time_t' value and the nearest subsecond (if the system is capable of * discerning time with such precision). */ static void get_tod(struct timeinfo *ti) { #if defined(_WIN32) struct _SYSTEMTIME ts; ti->ti_time = time(NULL); GetSystemTime(&ts); ti->ti_nsec = ts.wMilliseconds*1000000L; #elif defined(unix) struct timeval tv; gettimeofday(&tv, NULL); ti->ti_time = (time_t)tv.tv_sec; ti->ti_nsec = (long)tv.tv_usec*1000L; #else ti->ti_time = time(NULL); ti->ti_nsec = 0; #endif } /*------------------------------------------------------------------------------ * print_date() * Set a calendar to a given time, format it as a string, and print it. */ static void print_date(struct _calendar *cal, const struct timeinfo *ti) { char buf[80]; /* Convert the system time into a calendar date */ _setcalendartime(cal, ti->ti_time); cal->cal_nsec = ti->ti_nsec; /* Format the calendar date and print it */ _calendarformat(buf, sizeof(buf), "%a %Y-%b-%d %H:%M:%S.%3s %Z", cal); printf("-> %s\n", buf); } /******************************************************************************* * Public functions *******************************************************************************/ /*------------------------------------------------------------------------------ * main() * Test driver. */ int main(int argc, char **argv) { struct timeinfo now; struct _calendar cal; int i; if (argc > 1 and strcmp(argv[1], "-z") == 0) { /* Display a list of supported timezones */ list_zones(); argc--, argv++; } /* Get the current system time */ get_tod(&now); printf("System time: %+ld.%09ld\n", (long)now.ti_time, now.ti_nsec); /* Get a default (Gregorian) calendar for the UTC timezone */ _getcalendar(&cal, "", _TZ_UTC); /* Convert the system time into a calendar date and print it */ print_date(&cal, &now); /* Get a default (Gregorian) calendar for the local timezone */ _getcalendar(&cal, "", _TZ_LOCAL); /* Convert the system time into a calendar date and print it */ print_date(&cal, &now); /* Check for user-specified formats on the command line */ for (i = 1; i < argc and argv[i][0] == '+'; i++) { char buf[80]; _calendarformat(buf, sizeof(buf), &argv[i][1], &cal); printf("-> %s\n", buf); } /* Done */ return (0); } /* End c0xcaltest.c */ /* -----BEGIN PGP SIGNATURE----- Version: PGPfreeware 7.0.3 for non-commercial use iQA/AwUBPHgLj3S9RCOKzj55EQI52gCeJ+3/BP+DMdiLxV+UwCF8Pfr22fQAn3yt LEuKtKEWhPR1RAFx+SOWoxWp =cxD3 -----END PGP SIGNATURE----- */