| 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_STDIO_INIT(); |
| 49 |
PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); |
| 50 |
|
| 51 |
/* |
| 52 |
************************************************************* |
| 53 |
** |
| 54 |
** Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime |
| 55 |
** on the current time |
| 56 |
** |
| 57 |
************************************************************* |
| 58 |
*/ |
| 59 |
|
| 60 |
{ |
| 61 |
PRTime t1, t2; |
| 62 |
PRExplodedTime et; |
| 63 |
|
| 64 |
printf("*********************************************\n"); |
| 65 |
printf("** **\n"); |
| 66 |
printf("** Testing PR_Now(), PR_ExplodeTime, and **\n"); |
| 67 |
printf("** PR_ImplodeTime on the current time **\n"); |
| 68 |
printf("** **\n"); |
| 69 |
printf("*********************************************\n\n"); |
| 70 |
t1 = PR_Now(); |
| 71 |
|
| 72 |
/* First try converting to UTC */ |
| 73 |
|
| 74 |
PR_ExplodeTime(t1, PR_GMTParameters, &et); |
| 75 |
if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) { |
| 76 |
printf("ERROR: UTC has nonzero gmt or dst offset.\n"); |
| 77 |
return 1; |
| 78 |
} |
| 79 |
printf("Current UTC is "); |
| 80 |
printExplodedTime(&et); |
| 81 |
printf("\n"); |
| 82 |
|
| 83 |
t2 = PR_ImplodeTime(&et); |
| 84 |
if (LL_NE(t1, t2)) { |
| 85 |
printf("ERROR: Explode and implode are NOT inverse.\n"); |
| 86 |
return 1; |
| 87 |
} |
| 88 |
|
| 89 |
/* Next, try converting to local (US Pacific) time */ |
| 90 |
|
| 91 |
PR_ExplodeTime(t1, PR_LocalTimeParameters, &et); |
| 92 |
printf("Current local time is "); |
| 93 |
printExplodedTime(&et); |
| 94 |
printf("\n"); |
| 95 |
printf("GMT offset is %ld, DST offset is %ld\n", et.tm_params.tp_gmt_offset, |
| 96 |
et.tm_params.tp_dst_offset); |
| 97 |
t2 = PR_ImplodeTime(&et); |
| 98 |
if (LL_NE(t1, t2)) { |
| 99 |
printf("ERROR: Explode and implode are NOT inverse.\n"); |
| 100 |
return 1; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
printf("Please examine the results\n"); |
| 105 |
return 0; |
| 106 |
} |