timemac.c (3228B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 /* 7 * file: timemac.c 8 * description: test time and date routines on the Mac 9 */ 10 #include <stdio.h> 11 #include "prinit.h" 12 #include "prtime.h" 13 14 static char* dayOfWeek[] = {"Sun", "Mon", "Tue", "Wed", 15 "Thu", "Fri", "Sat", "???"}; 16 static char* month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", 17 "Aug", "Sep", "Oct", "Nov", "Dec", "???"}; 18 19 static void printExplodedTime(const PRExplodedTime* et) { 20 PRInt32 totalOffset; 21 PRInt32 hourOffset, minOffset; 22 const char* sign; 23 24 /* Print day of the week, month, day, hour, minute, and second */ 25 printf("%s %s %ld %02ld:%02ld:%02ld ", dayOfWeek[et->tm_wday], 26 month[et->tm_month], et->tm_mday, et->tm_hour, et->tm_min, et->tm_sec); 27 28 /* Print time zone */ 29 totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset; 30 if (totalOffset == 0) { 31 printf("UTC "); 32 } else { 33 sign = ""; 34 if (totalOffset < 0) { 35 totalOffset = -totalOffset; 36 sign = "-"; 37 } 38 hourOffset = totalOffset / 3600; 39 minOffset = (totalOffset % 3600) / 60; 40 printf("%s%02ld%02ld ", sign, hourOffset, minOffset); 41 } 42 43 /* Print year */ 44 printf("%d", et->tm_year); 45 } 46 47 int main(int argc, char** argv) { 48 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); 49 50 /* 51 ************************************************************* 52 ** 53 ** Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime 54 ** on the current time 55 ** 56 ************************************************************* 57 */ 58 59 { 60 PRTime t1, t2; 61 PRExplodedTime et; 62 63 printf("*********************************************\n"); 64 printf("** **\n"); 65 printf("** Testing PR_Now(), PR_ExplodeTime, and **\n"); 66 printf("** PR_ImplodeTime on the current time **\n"); 67 printf("** **\n"); 68 printf("*********************************************\n\n"); 69 t1 = PR_Now(); 70 71 /* First try converting to UTC */ 72 73 PR_ExplodeTime(t1, PR_GMTParameters, &et); 74 if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) { 75 printf("ERROR: UTC has nonzero gmt or dst offset.\n"); 76 return 1; 77 } 78 printf("Current UTC is "); 79 printExplodedTime(&et); 80 printf("\n"); 81 82 t2 = PR_ImplodeTime(&et); 83 if (LL_NE(t1, t2)) { 84 printf("ERROR: Explode and implode are NOT inverse.\n"); 85 return 1; 86 } 87 88 /* Next, try converting to local (US Pacific) time */ 89 90 PR_ExplodeTime(t1, PR_LocalTimeParameters, &et); 91 printf("Current local time is "); 92 printExplodedTime(&et); 93 printf("\n"); 94 printf("GMT offset is %ld, DST offset is %ld\n", et.tm_params.tp_gmt_offset, 95 et.tm_params.tp_dst_offset); 96 t2 = PR_ImplodeTime(&et); 97 if (LL_NE(t1, t2)) { 98 printf("ERROR: Explode and implode are NOT inverse.\n"); 99 return 1; 100 } 101 } 102 103 printf("Please examine the results\n"); 104 return 0; 105 }