Back to index

lightning-sunbird  0.9+nobinonly
timemac.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * file: timemac.c
00040  * description: test time and date routines on the Mac
00041  */
00042 #include <stdio.h>
00043 #include "prinit.h"
00044 #include "prtime.h"
00045 
00046 #ifdef XP_MAC
00047 #include "prlog.h"
00048 #define printf PR_LogPrint
00049 extern void SetupMacPrintfLog(char *logFile);
00050 #endif
00051 
00052 
00053 static char *dayOfWeek[] =
00054        { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???" };
00055 static char *month[] =
00056        { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
00057          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???" };
00058 
00059 static void printExplodedTime(const PRExplodedTime *et) {
00060     PRInt32 totalOffset;
00061     PRInt32 hourOffset, minOffset;
00062     const char *sign;
00063 
00064     /* Print day of the week, month, day, hour, minute, and second */
00065     printf( "%s %s %ld %02ld:%02ld:%02ld ",
00066            dayOfWeek[et->tm_wday], month[et->tm_month], et->tm_mday,
00067            et->tm_hour, et->tm_min, et->tm_sec);
00068 
00069     /* Print time zone */
00070     totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset;
00071     if (totalOffset == 0) {
00072        printf("UTC ");
00073     } else {
00074         sign = "";
00075         if (totalOffset < 0) {
00076            totalOffset = -totalOffset;
00077            sign = "-";
00078         }
00079         hourOffset = totalOffset / 3600;
00080         minOffset = (totalOffset % 3600) / 60;
00081         printf("%s%02ld%02ld ", sign, hourOffset, minOffset);
00082     }
00083 
00084     /* Print year */
00085     printf("%d", et->tm_year);
00086 }
00087 
00088 int main(int argc, char** argv)
00089 {
00090     PR_STDIO_INIT();
00091     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
00092 
00093  
00094 #ifdef XP_MAC
00095        SetupMacPrintfLog("timemac.log");
00096 #endif
00097 
00098    /*
00099      *************************************************************
00100      **
00101      **  Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime
00102      **  on the current time
00103      **
00104      *************************************************************
00105      */
00106 
00107     {
00108        PRTime t1, t2;
00109        PRExplodedTime et;
00110 
00111        printf("*********************************************\n");
00112        printf("**                                         **\n");
00113         printf("** Testing PR_Now(), PR_ExplodeTime, and   **\n");
00114        printf("** PR_ImplodeTime on the current time      **\n");
00115        printf("**                                         **\n");
00116        printf("*********************************************\n\n");
00117         t1 = PR_Now();
00118 
00119         /* First try converting to UTC */
00120 
00121         PR_ExplodeTime(t1, PR_GMTParameters, &et);
00122         if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) {
00123            printf("ERROR: UTC has nonzero gmt or dst offset.\n");
00124            return 1;
00125         }
00126         printf("Current UTC is ");
00127        printExplodedTime(&et);
00128        printf("\n");
00129 
00130         t2 = PR_ImplodeTime(&et);
00131         if (LL_NE(t1, t2)) {
00132            printf("ERROR: Explode and implode are NOT inverse.\n");
00133            return 1;
00134         }
00135 
00136         /* Next, try converting to local (US Pacific) time */
00137 
00138         PR_ExplodeTime(t1, PR_LocalTimeParameters, &et);
00139         printf("Current local time is ");
00140        printExplodedTime(&et);
00141        printf("\n");
00142        printf("GMT offset is %ld, DST offset is %ld\n",
00143               et.tm_params.tp_gmt_offset, et.tm_params.tp_dst_offset);
00144         t2 = PR_ImplodeTime(&et);
00145         if (LL_NE(t1, t2)) {
00146            printf("ERROR: Explode and implode are NOT inverse.\n");
00147            return 1;
00148        }
00149     }
00150 
00151     printf("Please examine the results\n");
00152     return 0;
00153 }