home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / timemac.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.0 KB  |  135 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.  * file: timemac.c
  21.  * description: test time and date routines on the Mac
  22.  */
  23. #include <stdio.h>
  24. #include "prinit.h"
  25. #include "prtime.h"
  26.  
  27. #ifdef XP_MAC
  28. #include "prlog.h"
  29. #define printf PR_LogPrint
  30. extern void SetupMacPrintfLog(char *logFile);
  31. #endif
  32.  
  33.  
  34. static char *dayOfWeek[] =
  35.     { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???" };
  36. static char *month[] =
  37.     { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  38.       "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???" };
  39.  
  40. static void printExplodedTime(const PRExplodedTime *et) {
  41.     PRInt32 totalOffset;
  42.     PRInt32 hourOffset, minOffset;
  43.     const char *sign;
  44.  
  45.     /* Print day of the week, month, day, hour, minute, and second */
  46.     printf( "%s %s %ld %02ld:%02ld:%02ld ",
  47.         dayOfWeek[et->tm_wday], month[et->tm_month], et->tm_mday,
  48.         et->tm_hour, et->tm_min, et->tm_sec);
  49.  
  50.     /* Print time zone */
  51.     totalOffset = et->tm_params.tp_gmt_offset + et->tm_params.tp_dst_offset;
  52.     if (totalOffset == 0) {
  53.     printf("UTC ");
  54.     } else {
  55.         sign = "";
  56.         if (totalOffset < 0) {
  57.         totalOffset = -totalOffset;
  58.         sign = "-";
  59.         }
  60.         hourOffset = totalOffset / 3600;
  61.         minOffset = (totalOffset % 3600) / 60;
  62.         printf("%s%02ld%02ld ", sign, hourOffset, minOffset);
  63.     }
  64.  
  65.     /* Print year */
  66.     printf("%d", et->tm_year);
  67. }
  68.  
  69. int main(int argc, char** argv)
  70. {
  71.     PR_STDIO_INIT();
  72.     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  73.  
  74.  
  75. #ifdef XP_MAC
  76.     SetupMacPrintfLog("timemac.log");
  77. #endif
  78.  
  79.    /*
  80.      *************************************************************
  81.      **
  82.      **  Testing PR_Now(), PR_ExplodeTime, and PR_ImplodeTime
  83.      **  on the current time
  84.      **
  85.      *************************************************************
  86.      */
  87.  
  88.     {
  89.     PRTime t1, t2;
  90.     PRExplodedTime et;
  91.  
  92.     printf("*********************************************\n");
  93.     printf("**                                         **\n");
  94.         printf("** Testing PR_Now(), PR_ExplodeTime, and   **\n");
  95.     printf("** PR_ImplodeTime on the current time      **\n");
  96.     printf("**                                         **\n");
  97.     printf("*********************************************\n\n");
  98.         t1 = PR_Now();
  99.  
  100.         /* First try converting to UTC */
  101.  
  102.         PR_ExplodeTime(t1, PR_GMTParameters, &et);
  103.         if (et.tm_params.tp_gmt_offset || et.tm_params.tp_dst_offset) {
  104.         printf("ERROR: UTC has nonzero gmt or dst offset.\n");
  105.         return 1;
  106.         }
  107.         printf("Current UTC is ");
  108.     printExplodedTime(&et);
  109.     printf("\n");
  110.  
  111.         t2 = PR_ImplodeTime(&et);
  112.         if (LL_NE(t1, t2)) {
  113.         printf("ERROR: Explode and implode are NOT inverse.\n");
  114.         return 1;
  115.         }
  116.  
  117.         /* Next, try converting to local (US Pacific) time */
  118.  
  119.         PR_ExplodeTime(t1, PR_LocalTimeParameters, &et);
  120.         printf("Current local time is ");
  121.     printExplodedTime(&et);
  122.     printf("\n");
  123.     printf("GMT offset is %ld, DST offset is %ld\n",
  124.         et.tm_params.tp_gmt_offset, et.tm_params.tp_dst_offset);
  125.         t2 = PR_ImplodeTime(&et);
  126.         if (LL_NE(t1, t2)) {
  127.         printf("ERROR: Explode and implode are NOT inverse.\n");
  128.         return 1;
  129.     }
  130.     }
  131.  
  132.     printf("Please examine the results\n");
  133.     return 0;
  134. }
  135.