Import from 1.9a8 tarball
[mozilla-nspr.git] / nsprpub / pr / tests / timemac.c
blobc58c24bdd3fbfbfadcb221b5149be85746f35a25
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 * file: timemac.c
40 * description: test time and date routines on the Mac
42 #include <stdio.h>
43 #include "prinit.h"
44 #include "prtime.h"
46 #ifdef XP_MAC
47 #include "prlog.h"
48 #define printf PR_LogPrint
49 extern void SetupMacPrintfLog(char *logFile);
50 #endif
53 static char *dayOfWeek[] =
54 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???" };
55 static char *month[] =
56 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
57 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???" };
59 static void printExplodedTime(const PRExplodedTime *et) {
60 PRInt32 totalOffset;
61 PRInt32 hourOffset, minOffset;
62 const char *sign;
64 /* Print day of the week, month, day, hour, minute, and second */
65 printf( "%s %s %ld %02ld:%02ld:%02ld ",
66 dayOfWeek[et->tm_wday], month[et->tm_month], et->tm_mday,
67 et->tm_hour, et->tm_min, et->tm_sec);
69 /* Print time zone */
70 totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset;
71 if (totalOffset == 0) {
72 printf("UTC ");
73 } else {
74 sign = "";
75 if (totalOffset < 0) {
76 totalOffset = -totalOffset;
77 sign = "-";
79 hourOffset = totalOffset / 3600;
80 minOffset = (totalOffset % 3600) / 60;
81 printf("%s%02ld%02ld ", sign, hourOffset, minOffset);
84 /* Print year */
85 printf("%d", et->tm_year);
88 int main(int argc, char** argv)
90 PR_STDIO_INIT();
91 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
94 #ifdef XP_MAC
95 SetupMacPrintfLog("timemac.log");
96 #endif
99 *************************************************************
101 ** Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime
102 ** on the current time
104 *************************************************************
108 PRTime t1, t2;
109 PRExplodedTime et;
111 printf("*********************************************\n");
112 printf("** **\n");
113 printf("** Testing PR_Now(), PR_ExplodeTime, and **\n");
114 printf("** PR_ImplodeTime on the current time **\n");
115 printf("** **\n");
116 printf("*********************************************\n\n");
117 t1 = PR_Now();
119 /* First try converting to UTC */
121 PR_ExplodeTime(t1, PR_GMTParameters, &et);
122 if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) {
123 printf("ERROR: UTC has nonzero gmt or dst offset.\n");
124 return 1;
126 printf("Current UTC is ");
127 printExplodedTime(&et);
128 printf("\n");
130 t2 = PR_ImplodeTime(&et);
131 if (LL_NE(t1, t2)) {
132 printf("ERROR: Explode and implode are NOT inverse.\n");
133 return 1;
136 /* Next, try converting to local (US Pacific) time */
138 PR_ExplodeTime(t1, PR_LocalTimeParameters, &et);
139 printf("Current local time is ");
140 printExplodedTime(&et);
141 printf("\n");
142 printf("GMT offset is %ld, DST offset is %ld\n",
143 et.tm_params.tp_gmt_offset, et.tm_params.tp_dst_offset);
144 t2 = PR_ImplodeTime(&et);
145 if (LL_NE(t1, t2)) {
146 printf("ERROR: Explode and implode are NOT inverse.\n");
147 return 1;
151 printf("Please examine the results\n");
152 return 0;