123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <OSUtils.h>
- #include "mactime.h"
- const unsigned long MacOS_2_Unix = 2082844800L;
- #ifndef TEST_TIME_LIB
- #define my_gmtime gmtime
- #define my_localtime localtime
- #define my_mktime mktime
- #define my_time time
- #endif
- static void clear_tm(struct tm * tm);
- static long GMTDelta(void);
- static Boolean DaylightSaving(void);
- static time_t GetTimeMac(void);
- static time_t Mactime(time_t *timer);
- static void normalize(int *i,int *j,int norm);
- static struct tm *time2tm(const time_t *timer);
- static time_t tm2time(struct tm *tp);
- static void myReadLocation(MachineLocation * loc);
- struct tm *my_gmtime(const time_t *t);
- struct tm *my_localtime(const time_t *t);
- time_t my_mktime(struct tm *tp);
- time_t my_time(time_t *t);
-
- time_t MacFtime2UnixFtime(unsigned long macftime)
- {
- long UTCoffset;
- GetGMToffsetMac(macftime, &UTCoffset);
- MACOS_TO_UNIX(macftime);
- macftime -= UTCoffset;
- return macftime;
- }
-
- unsigned long UnixFtime2MacFtime(time_t unxftime)
- {
- long UTCoffset;
- unsigned long macftime = unxftime;
- UNIX_TO_MACOS(macftime);
- GetGMToffsetMac(macftime, &UTCoffset);
- macftime += UTCoffset;
- return macftime;
- }
- time_t AdjustForTZmoveMac(unsigned long macloctim, long s_gmtoffs)
- {
- time_t MacGMTTime;
- long UTCoffset;
-
- MacGMTTime = macloctim - s_gmtoffs;
- GetGMToffsetMac(macloctim, &UTCoffset);
- return (MacGMTTime + UTCoffset);
- }
- Boolean GetGMToffsetMac(unsigned long mactime, long *UTCoffset)
- {
- mactime = mactime;
- *UTCoffset = GMTDelta();
- return true;
- }
- struct tm *my_gmtime(const time_t *timer)
- {
- return time2tm(timer);
- }
- struct tm *my_localtime(const time_t *timer)
- {
- time_t maclocal;
- maclocal = *timer;
- maclocal += GMTDelta();
- return time2tm(&maclocal);
- }
- time_t my_mktime(struct tm *tp)
- {
- time_t maclocal;
- maclocal = tm2time(tp);
- maclocal -= GMTDelta();
- return maclocal;
- }
- time_t my_time(time_t *time)
- {
- time_t tmp_time;
- GetDateTime(&tmp_time);
- MACOS_TO_UNIX(tmp_time);
- if (time)
- {
- *time = tmp_time;
- }
- return tmp_time;
- }
- static void myReadLocation(MachineLocation * loc)
- {
- static MachineLocation storedLoc;
- static time_t first_call = 0, last_call = 86400;
- if ((last_call - first_call) > 7200)
- {
- GetDateTime(&first_call);
- ReadLocation(&storedLoc);
- }
- GetDateTime(&last_call);
- *loc = storedLoc;
- }
- static Boolean DaylightSaving(void)
- {
- MachineLocation loc;
- unsigned char dlsDelta;
- myReadLocation(&loc);
- dlsDelta = loc.u.dlsDelta;
- return (dlsDelta != 0);
- }
- static long GMTDelta(void)
- {
- MachineLocation loc;
- long gmtDelta;
- myReadLocation(&loc);
-
- gmtDelta = loc.u.gmtDelta & 0x00FFFFFF;
- if ((gmtDelta & 0x00800000) != 0)
- {
- gmtDelta |= 0xFF000000;
- }
- return gmtDelta;
- }
- static time_t GetTimeMac(void)
- {
- unsigned long maclocal;
-
- GetDateTime(&maclocal);
- maclocal -= GMTDelta();
- MACOS_TO_UNIX(maclocal);
- return maclocal;
- }
- static void clear_tm(struct tm * tm)
- {
- tm->tm_sec = 0;
- tm->tm_min = 0;
- tm->tm_hour = 0;
- tm->tm_mday = 1;
- tm->tm_mon = 0;
- tm->tm_year = 0;
- tm->tm_wday = 1;
- tm->tm_yday = 0;
- tm->tm_isdst = -1;
- }
- static void normalize(int *i,int *j,int norm)
- {
- while(*i < 0)
- {
- *i += norm;
- (*j)--;
- }
- while(*i >= norm)
- {
- *i -= norm;
- (*j)++;
- }
- }
- static time_t Mactime(time_t *timer)
- {
- time_t t = GetTimeMac();
- if (timer != NULL)
- *timer = t;
- return t;
- }
- static struct tm *time2tm(const time_t *timer)
- {
- DateTimeRec dtr;
- MachineLocation loc;
- time_t macLocal = *timer;
- static struct tm statictime;
- static const short monthday[12] =
- {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
- UNIX_TO_MACOS(macLocal);
- SecondsToDate(macLocal, &dtr);
- statictime.tm_sec = dtr.second;
- statictime.tm_min = dtr.minute;
- statictime.tm_hour = dtr.hour;
- statictime.tm_mday = dtr.day;
- statictime.tm_mon = dtr.month - 1;
- statictime.tm_year = dtr.year - 1900;
- statictime.tm_wday = dtr.dayOfWeek - 1;
- statictime.tm_yday = monthday[statictime.tm_mon]
- + statictime.tm_mday - 1;
- if (2 < statictime.tm_mon && !(statictime.tm_year & 3))
- {
- ++statictime.tm_yday;
- }
- myReadLocation(&loc);
- statictime.tm_isdst = DaylightSaving();
- return(&statictime);
- }
- static time_t tm2time(struct tm *tp)
- {
- time_t intMacTime;
- DateTimeRec dtr;
- normalize(&tp->tm_sec, &tp->tm_min, 60);
- normalize(&tp->tm_min, &tp->tm_hour,60);
- normalize(&tp->tm_hour,&tp->tm_mday,24);
- normalize(&tp->tm_mon, &tp->tm_year,12);
- dtr.year = tp->tm_year + 1900;
- dtr.month = tp->tm_mon + 1;
- dtr.day = tp->tm_mday;
- dtr.hour = tp->tm_hour;
- dtr.minute = tp->tm_min;
- dtr.second = tp->tm_sec;
- DateToSeconds(&dtr, &intMacTime);
- MACOS_TO_UNIX(intMacTime);
- return intMacTime;
- }
|