michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * file: timemac.c michael@0: * description: test time and date routines on the Mac michael@0: */ michael@0: #include michael@0: #include "prinit.h" michael@0: #include "prtime.h" michael@0: michael@0: michael@0: static char *dayOfWeek[] = michael@0: { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???" }; michael@0: static char *month[] = michael@0: { "Jan", "Feb", "Mar", "Apr", "May", "Jun", michael@0: "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???" }; michael@0: michael@0: static void printExplodedTime(const PRExplodedTime *et) { michael@0: PRInt32 totalOffset; michael@0: PRInt32 hourOffset, minOffset; michael@0: const char *sign; michael@0: michael@0: /* Print day of the week, month, day, hour, minute, and second */ michael@0: printf( "%s %s %ld %02ld:%02ld:%02ld ", michael@0: dayOfWeek[et->tm_wday], month[et->tm_month], et->tm_mday, michael@0: et->tm_hour, et->tm_min, et->tm_sec); michael@0: michael@0: /* Print time zone */ michael@0: totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset; michael@0: if (totalOffset == 0) { michael@0: printf("UTC "); michael@0: } else { michael@0: sign = ""; michael@0: if (totalOffset < 0) { michael@0: totalOffset = -totalOffset; michael@0: sign = "-"; michael@0: } michael@0: hourOffset = totalOffset / 3600; michael@0: minOffset = (totalOffset % 3600) / 60; michael@0: printf("%s%02ld%02ld ", sign, hourOffset, minOffset); michael@0: } michael@0: michael@0: /* Print year */ michael@0: printf("%d", et->tm_year); michael@0: } michael@0: michael@0: int main(int argc, char** argv) michael@0: { michael@0: PR_STDIO_INIT(); michael@0: PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); michael@0: michael@0: michael@0: /* michael@0: ************************************************************* michael@0: ** michael@0: ** Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime michael@0: ** on the current time michael@0: ** michael@0: ************************************************************* michael@0: */ michael@0: michael@0: { michael@0: PRTime t1, t2; michael@0: PRExplodedTime et; michael@0: michael@0: printf("*********************************************\n"); michael@0: printf("** **\n"); michael@0: printf("** Testing PR_Now(), PR_ExplodeTime, and **\n"); michael@0: printf("** PR_ImplodeTime on the current time **\n"); michael@0: printf("** **\n"); michael@0: printf("*********************************************\n\n"); michael@0: t1 = PR_Now(); michael@0: michael@0: /* First try converting to UTC */ michael@0: michael@0: PR_ExplodeTime(t1, PR_GMTParameters, &et); michael@0: if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) { michael@0: printf("ERROR: UTC has nonzero gmt or dst offset.\n"); michael@0: return 1; michael@0: } michael@0: printf("Current UTC is "); michael@0: printExplodedTime(&et); michael@0: printf("\n"); michael@0: michael@0: t2 = PR_ImplodeTime(&et); michael@0: if (LL_NE(t1, t2)) { michael@0: printf("ERROR: Explode and implode are NOT inverse.\n"); michael@0: return 1; michael@0: } michael@0: michael@0: /* Next, try converting to local (US Pacific) time */ michael@0: michael@0: PR_ExplodeTime(t1, PR_LocalTimeParameters, &et); michael@0: printf("Current local time is "); michael@0: printExplodedTime(&et); michael@0: printf("\n"); michael@0: printf("GMT offset is %ld, DST offset is %ld\n", michael@0: et.tm_params.tp_gmt_offset, et.tm_params.tp_dst_offset); michael@0: t2 = PR_ImplodeTime(&et); michael@0: if (LL_NE(t1, t2)) { michael@0: printf("ERROR: Explode and implode are NOT inverse.\n"); michael@0: return 1; michael@0: } michael@0: } michael@0: michael@0: printf("Please examine the results\n"); michael@0: return 0; michael@0: }