home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / mac / Misc / mactime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.1 KB  |  212 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. #ifdef mktime
  19. #undef mktime
  20. #undef ctime
  21. #undef localtime
  22. _C_LIB_DECL    /* declarations */
  23. time_t mktime(struct tm *);
  24. char *ctime(const time_t *);
  25. struct tm *localtime(const time_t *);
  26. _END_C_LIB_DECL
  27. #endif
  28.  
  29. #ifndef UNIXMINUSMACTIME
  30. #define UNIXMINUSMACTIME 2082844800UL
  31. #endif
  32. #include <time.h>
  33.  
  34. // Because serial port and SLIP conflict with ReadXPram calls,
  35. // we cache the call here
  36. // The symptoms are the 
  37. void MyReadLocation(MachineLocation * loc);
  38. long GMTDelta();
  39.  
  40.  
  41. void MyReadLocation(MachineLocation * loc)
  42. {
  43.     static MachineLocation storedLoc;    // InsideMac, OSUtilities, page 4-20
  44.     static Boolean didReadLocation = FALSE;
  45.     if (!didReadLocation)
  46.     {    
  47.         ReadLocation(&storedLoc);
  48.         didReadLocation = TRUE;
  49.     }
  50.     *loc = storedLoc;
  51. }
  52.  
  53. Boolean DaylightSavings()
  54. {
  55.     MachineLocation loc;
  56.     unsigned char dlsDelta;
  57.     MyReadLocation(&loc);
  58.     dlsDelta =     loc.u.dlsDelta;
  59.     return (dlsDelta != 0);
  60. }
  61.  
  62. // This routine is copied straight out of stdio sources.
  63. // The only difference is that we use MyReadLocation instead of ReadLocation.
  64. struct tm *(gmtime)(const time_t *tod)
  65. {    /* convert to Greenwich Mean Time (UTC) */
  66.     MachineLocation myLocation;
  67.     long int internalGmtDelta;
  68.     time_t ltime;
  69.     MyReadLocation(&myLocation);
  70.     internalGmtDelta = myLocation.u.gmtDelta & 0x00ffffff;
  71.     if (internalGmtDelta & 0x00800000)
  72.         internalGmtDelta = internalGmtDelta | 0xff000000;
  73.     ltime = (*tod) - internalGmtDelta;
  74.     return (localtime(&(ltime)));
  75. }
  76.  
  77.  
  78. // We need to do this, because we wrap localtime as a macro in
  79. #ifdef localtime
  80. #undef localtime
  81. #endif
  82. struct tm *localtime(const time_t *tp)
  83.     {
  84.     DateTimeRec dtr;
  85.     MachineLocation loc;
  86.     static struct tm statictime;
  87.     static const short monthday[12] =
  88.         {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
  89.  
  90.     SecondsToDate(*tp, &dtr);
  91.     statictime.tm_sec = dtr.second;
  92.     statictime.tm_min = dtr.minute;
  93.     statictime.tm_hour = dtr.hour;
  94.     statictime.tm_mday = dtr.day;
  95.     statictime.tm_mon = dtr.month - 1;
  96.     statictime.tm_year = dtr.year - 1900;
  97.     statictime.tm_wday = dtr.dayOfWeek - 1;
  98.     statictime.tm_yday = monthday[statictime.tm_mon]
  99.         + statictime.tm_mday - 1;
  100.     if (2 < statictime.tm_mon && !(statictime.tm_year & 3))
  101.         ++statictime.tm_yday;
  102.     MyReadLocation(&loc);
  103.     statictime.tm_isdst = loc.u.dlsDelta;
  104.     return(&statictime);
  105. }
  106.  
  107.  
  108. // current local time = GMTDelta() + GMT
  109. // GMT = local time - GMTDelta()
  110. long GMTDelta()
  111. {
  112.     MachineLocation loc;
  113.     long gmtDelta;
  114.     
  115.     MyReadLocation(&loc);
  116.     gmtDelta = loc.u.gmtDelta & 0x00FFFFFF;
  117.     if (BitTst((Ptr)&gmtDelta, 23))
  118.         gmtDelta = gmtDelta | 0xFF000000;
  119.     return gmtDelta;
  120. }
  121.  
  122.  
  123. // This routine simulates stdclib time(), time in seconds since 1.1.1970
  124. // The time is in GMT
  125. time_t    GetTimeMac()
  126. {
  127.     unsigned long maclocal;
  128.     // Get Mac local time
  129.     GetDateTime(&maclocal); 
  130.     // Get Mac GMT    
  131.     maclocal -= GMTDelta();
  132.     // return unix GMT
  133.     return (maclocal - UNIXMINUSMACTIME);
  134. }
  135.  
  136. // Returns the GMT times
  137. time_t Mactime(time_t *timer)
  138. {
  139.     time_t t = GetTimeMac();
  140.     if (timer != NULL)
  141.         *timer = t;
  142.     return t;
  143. }
  144.  
  145. time_t Macmktime (struct tm *timeptr)
  146. {
  147.     time_t theTime;
  148.     
  149.     // ÑÑÑ HACK to work around the bug in mktime
  150.     int negativeDiff = 0;
  151.     if (timeptr->tm_sec < 0)
  152.     {
  153.         negativeDiff += timeptr->tm_sec;
  154.         timeptr->tm_sec = 0;    
  155.     }
  156.     if (timeptr->tm_min < 0)
  157.     {
  158.         negativeDiff += timeptr->tm_min*60;
  159.         timeptr->tm_min = 0;    
  160.     }
  161.     if (timeptr->tm_hour < 0)
  162.     {
  163.         negativeDiff += timeptr->tm_hour*60*60;
  164.         timeptr->tm_hour = 0;    
  165.     }
  166.     if (timeptr->tm_mday < 0)
  167.     {
  168.         negativeDiff += timeptr->tm_mday*60*60*24;
  169.         timeptr->tm_mday = 0;    
  170.     }
  171.     
  172.     // local/Mac
  173.     theTime = mktime(timeptr);
  174.     // mktime does not care what the daylightsavings flag is
  175.     timeptr->tm_isdst = 0;//DaylightSavings(); 
  176.     theTime += negativeDiff;
  177.  
  178.     // GMT/Mac
  179.     theTime -= GMTDelta();
  180.     // unix/GMT
  181.     return theTime - UNIXMINUSMACTIME;
  182. }
  183.  
  184. //  
  185. char * Macctime(const time_t * t)
  186. {
  187.     // GMT Mac
  188.     time_t macTime = *t + UNIXMINUSMACTIME;
  189.     // local Mac
  190.     macTime += GMTDelta();
  191.     
  192.     return ctime(&macTime);
  193. }
  194.  
  195. struct tm *Maclocaltime(const time_t * t)
  196. {
  197.     // GMT Mac
  198.     time_t macLocal = *t + UNIXMINUSMACTIME;
  199.     // local Mac
  200.     macLocal += GMTDelta();
  201.     return localtime(&macLocal);
  202. }
  203.  
  204. // -> unix GMT
  205. struct tm *Macgmtime (const time_t *clock)
  206. {
  207.     // GMT Mac
  208.     time_t macGMT =  *clock + UNIXMINUSMACTIME;
  209.     return localtime(&macGMT);
  210. }
  211.  
  212.