home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / central / mactime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.2 KB  |  182 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. #include <time.h>
  20. #include "xp_mcom.h"
  21.  
  22. /* Note that the ordering of the '#include "xp_mcom.h"' and the '#undef ctime' is
  23.    important.  xp_mcom.h defines ctime as Macctime.  Macctime calls ctime.  The result
  24.    was endless recursion. -km */
  25.  
  26. #ifdef mktime
  27. #undef mktime
  28. #undef ctime
  29. #undef localtime
  30. __extern_c    /* declarations */
  31. time_t mktime(struct tm *);
  32. char *ctime(const time_t *);
  33. struct tm *localtime(const time_t *);
  34. __end_extern_c
  35. #endif
  36.  
  37.  
  38. // Because serial port and SLIP conflict with ReadXPram calls,
  39. // we cache the call here so we don't hang on calling ReadLocation()
  40. void MyReadLocation(MachineLocation * loc);
  41.  
  42. long GMTDelta();
  43. Boolean DaylightSaving();
  44. time_t    GetTimeMac();
  45. time_t Mactime(time_t *timer);
  46. time_t Macmktime (struct tm *timeptr);
  47. char * Macctime(const time_t * t);
  48. struct tm *Macgmtime (const time_t *clock);
  49.  
  50.  
  51. void MyReadLocation(MachineLocation * loc)
  52. {
  53.     static MachineLocation storedLoc;    // InsideMac, OSUtilities, page 4-20
  54.     static Boolean didReadLocation = FALSE;
  55.     if (!didReadLocation)
  56.     {    
  57.         ReadLocation(&storedLoc);
  58.         didReadLocation = TRUE;
  59.     }
  60.     *loc = storedLoc;
  61. }
  62.  
  63. Boolean DaylightSaving()
  64. {
  65.     MachineLocation loc;
  66.     unsigned char dlsDelta;
  67.     MyReadLocation(&loc);
  68.     dlsDelta =     loc.u.dlsDelta;
  69.     return (dlsDelta != 0);
  70. }
  71.  
  72.  
  73. // We need to do this, because we wrap localtime as a macro in
  74. #ifdef localtime
  75. #undef localtime
  76. #endif
  77.  
  78. // current local time = GMTDelta() + GMT
  79. // GMT = local time - GMTDelta()
  80. long GMTDelta()
  81. {
  82.     MachineLocation loc;
  83.     long gmtDelta;
  84.     
  85.     MyReadLocation(&loc);
  86.     gmtDelta = loc.u.gmtDelta & 0x00FFFFFF;
  87.     if ((gmtDelta & 0x00800000) != 0)
  88.         gmtDelta |= 0xFF000000;
  89.     return gmtDelta;
  90. }
  91.  
  92.  
  93. // This routine simulates stdclib time(), time in seconds since 1.1.1970
  94. // The time is in GMT
  95. time_t    GetTimeMac()
  96. {
  97.     unsigned long maclocal;
  98.     // Get Mac local time
  99.     GetDateTime(&maclocal); 
  100.     // Get Mac GMT    
  101.     maclocal -= GMTDelta();
  102.     // return unix GMT
  103.     return (maclocal - UNIXMINUSMACTIME);
  104. }
  105.  
  106. // Returns the GMT times
  107. time_t Mactime(time_t *timer)
  108. {
  109.     time_t t = GetTimeMac();
  110.     if (timer != NULL)
  111.         *timer = t;
  112.     return t;
  113. }
  114.  
  115. time_t Macmktime (struct tm *timeptr)
  116. {
  117.     time_t theTime;
  118.     
  119.     // ÑÑÑ HACK to work around the bug in mktime
  120.     int negativeDiff = 0;
  121.     if (timeptr->tm_sec < 0)
  122.     {
  123.         negativeDiff += timeptr->tm_sec;
  124.         timeptr->tm_sec = 0;    
  125.     }
  126.     if (timeptr->tm_min < 0)
  127.     {
  128.         negativeDiff += timeptr->tm_min*60;
  129.         timeptr->tm_min = 0;    
  130.     }
  131.     if (timeptr->tm_hour < 0)
  132.     {
  133.         negativeDiff += timeptr->tm_hour*60*60;
  134.         timeptr->tm_hour = 0;    
  135.     }
  136.     if (timeptr->tm_mday < 0)
  137.     {
  138.         negativeDiff += timeptr->tm_mday*60*60*24;
  139.         timeptr->tm_mday = 0;    
  140.     }
  141.     
  142.     // local/Mac
  143.     theTime = mktime(timeptr);
  144.     // mktime does not care what the daylightsavings flag is
  145.     timeptr->tm_isdst = 0;//DaylightSavings(); 
  146.     theTime += negativeDiff;
  147.  
  148.     // GMT/Mac
  149.     theTime -= GMTDelta();
  150.     // unix/GMT
  151.     return theTime - UNIXMINUSMACTIME;
  152. }
  153.  
  154. //  
  155. char * Macctime(const time_t * t)
  156. {
  157.     // GMT Mac
  158.     time_t macTime = *t + UNIXMINUSMACTIME;
  159.     // local Mac
  160.     macTime += GMTDelta();
  161.     
  162.     return ctime(&macTime);
  163. }
  164.  
  165. struct tm *Maclocaltime(const time_t * t)
  166. {
  167.     // GMT Mac
  168.     time_t macLocal = *t + UNIXMINUSMACTIME;
  169.     // local Mac
  170.     macLocal += GMTDelta();
  171.     return localtime(&macLocal);
  172. }
  173.  
  174. // -> unix GMT
  175. struct tm *Macgmtime (const time_t *clock)
  176. {
  177.     // GMT Mac
  178.     time_t macGMT =  *clock + UNIXMINUSMACTIME;
  179.     return localtime(&macGMT);
  180. }
  181.  
  182.