home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip540.zip / amiga / time_lib.c < prev    next >
C/C++ Source or Header  |  1998-11-26  |  16KB  |  542 lines

  1. #define __amiga_time_lib_c
  2.  
  3. /* -----------------------------------------------------------------------------
  4. This source is copyrighted by Norbert Pueschel <pueschel@imsdd.meb.uni-bonn.de>
  5. From 'clockdaemon.readme':
  6. (available from Aminet, main site is ftp.wustl.edu:/pub/aminet/ under
  7.  util/time/clockdaemon.lha)
  8. "The original SAS/C functions gmtime, localtime, mktime and time do not
  9. work correctly. The supplied link library time.lib contains replacement
  10. functions for them."
  11. The time.lib library consists of three parts (time.c, timezone.c and version.c),
  12. all included here. [time.lib 1.2 (1997-04-02)]
  13. Permission is granted to the Info-ZIP group to redistribute the time.lib source.
  14. The use of time.lib functions in own, noncommercial programs is permitted.
  15. It is only required to add the timezone.doc to such a distribution.
  16. Using the time.lib library in commercial software (including Shareware) is only
  17. permitted after prior consultation of the author.
  18. ------------------------------------------------------------------------------*/
  19. /* History */
  20. /* 30 Mar 1997, Haidinger Walter, added AVAIL_GETVAR macro to support OS <V36 */
  21. /* 24 May 1997, Haidinger Walter, added NO_MKTIME macro to allow use of Zip's */
  22. /*              mktime.c. NO_MKTIME must be defined in the makefile, though.  */
  23. /* 25 May 1997, Haidinger Walter, moved set_TZ() here from filedate.c         */
  24. /* 20 Jul 1997, Paul Kienitz, adapted for Aztec C, added mkgmtime(),          */
  25. /*              debugged, and made New York settings default, as is common.   */
  26. /* 30 Sep 1997, Paul Kienitz, restored real_timezone_is_set flag              */
  27. /* 19 Oct 1997, Paul Kienitz, corrected 16 bit multiply overflow bug          */
  28. /* 21 Oct 1997, Chr. Spieler, shortened long lines, removed more 16 bit stuff */
  29. /*              (n.b. __stdoffset and __dstoffset now have to be long ints)   */
  30. /* 25 Oct 1997, Paul Kienitz, cleanup, make tzset() not redo work needlessly  */
  31. /* 29 Oct 1997, Chr. Spieler, initialized globals _TZ, real_timezone_is_set   */
  32. /* 31 Dec 1997, Haidinger Walter, created z-time.h to overcome sas/c header   */
  33. /*              dependencies. TZ_ENVVAR macro added. Happy New Year!          */
  34. /* 25 Apr 1998, Chr. Spieler, __timezone must always contain __stdoffset      */
  35. /* 28 Apr 1998, Chr. Spieler, P. Kienitz, changed __daylight to standard usage */
  36.  
  37. #ifdef __SASC
  38. #  include <proto/dos.h>
  39. #  include <proto/locale.h>
  40. #  include <proto/exec.h>
  41.    /* this setenv() is in amiga/filedate.c */
  42.    extern int setenv(const char *var, const char *value, int overwrite);
  43. #else
  44. #  include <clib/dos_protos.h>
  45. #  include <clib/locale_protos.h>
  46. #  include <clib/exec_protos.h>
  47. #  include <pragmas/exec_lib.h>
  48. #  include <pragmas/dos_lib.h>
  49. #  include <pragmas/locale_lib.h>
  50. /* Info-ZIP accesses these by their standard names: */
  51. #  define __timezone timezone
  52. #  define __daylight daylight
  53. #  define __tzset    tzset
  54. #endif
  55. #define NO_TIME_H
  56. #include "amiga/z-time.h"
  57. #include <exec/execbase.h>
  58. #include <clib/alib_stdio_protos.h>
  59. #include <string.h>
  60. #include <stdlib.h>
  61.  
  62. extern struct ExecBase *SysBase;
  63. extern char *getenv(const char *var);
  64.  
  65. typedef unsigned long time_t;
  66. struct tm {
  67.     int tm_sec;      /* seconds after the minute */
  68.     int tm_min;      /* minutes after the hour */
  69.     int tm_hour;     /* hours since midnight */
  70.     int tm_mday;     /* day of the month */
  71.     int tm_mon;      /* months since January */
  72.     int tm_year;     /* years since 1900 */
  73.     int tm_wday;     /* days since Sunday */
  74.     int tm_yday;     /* days since January 1 */
  75.     int tm_isdst;    /* Daylight Savings Time flag */
  76. };
  77. struct dstdate {
  78.   enum { JULIAN0, JULIAN, MWD } dd_type;
  79.   int                           dd_day;
  80.   int                           dd_week;
  81.   int                           dd_month;
  82.   int                           dd_secs;
  83. };
  84. static struct dstdate __dststart;
  85. static struct dstdate __dstend;
  86.  
  87. #define isleapyear(y)    (((y)%4==0&&(!((y)%100==0)||((y)%400==0)))?1:0)
  88. #define yearlen(y)       (isleapyear(y)?366:365)
  89. #define weekday(d)       (((d)+4)%7)
  90. #define jan1ofyear(y)    (((y)-70)*365+((y)-69)/4-((y)-1)/100+((y)+299)/400)
  91. #define wdayofyear(y)    weekday(jan1ofyear(y))
  92. #define AMIGA2UNIX       252460800 /* seconds between 1.1.1970 and 1.1.1978 */
  93. #define CHECK            300       /* min. time between checks of IXGMTOFFSET */
  94. #define GETVAR_REQVERS   36L       /* required OS version for GetVar() */
  95. #define AVAIL_GETVAR     (SysBase->LibNode.lib_Version >= GETVAR_REQVERS)
  96. #ifndef TZ_ENVVAR
  97. #  define TZ_ENVVAR      "TZ"      /* environment variable to parse */
  98. #endif
  99.  
  100. #ifdef __SASC
  101.   extern int  __daylight;
  102.   extern long __timezone;
  103.   extern char *__tzname[2];
  104.   extern char *_TZ;
  105. #else
  106.   int __daylight;
  107.   long __timezone;
  108.   char *__tzname[2];
  109.   char *_TZ = NULL;
  110. #endif
  111. int real_timezone_is_set = FALSE;   /* globally visible TZ_is_valid signal */
  112. char __tzstn[MAXTIMEZONELEN];
  113. char __tzdtn[MAXTIMEZONELEN];
  114. /* the following 4 variables are only used internally; make them static ? */
  115. int __isdst;
  116. time_t __nextdstchange;
  117. long __stdoffset;
  118. long __dstoffset;
  119. #define TZLEN 64
  120. static char TZ[TZLEN];
  121. static struct tm TM;
  122. static const unsigned short days[2][13] = {
  123.   { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
  124.   { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  125. };
  126. #ifndef NO_MKTIME     /* only used by mktime() */
  127. static const unsigned short monlen[2][12] = {
  128.   { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
  129.   { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
  130. };
  131. #endif
  132.  
  133. /* internal prototypes */
  134. static time_t dst2time(int year,struct dstdate *dst);
  135. static void time2tm(time_t time);
  136. static int checkdst(time_t time);
  137. #ifndef NO_MKTIME
  138. static void normalize(int *i,int *j,int norm);
  139. #endif
  140. static long gettime(char **s);
  141. static void getdstdate(char **s,struct dstdate *dst);
  142. #ifdef __SASC
  143. void set_TZ(long time_zone, int day_light);
  144. #endif
  145.  
  146. /* prototypes for sc.lib replacement functions */
  147. struct tm *gmtime(const time_t *t);
  148. struct tm *localtime(const time_t *t);
  149. #ifndef NO_MKTIME
  150. time_t mkgmtime(struct tm *tm);
  151. time_t mktime(struct tm *tm);
  152. #endif
  153. time_t time(time_t *tm);
  154. void __tzset(void);
  155.  
  156.  
  157. static time_t dst2time(int year,struct dstdate *dst)
  158. {
  159.   int isleapyear,week,mon,mday;
  160.   mon = 0;
  161.   mday = dst->dd_day;
  162.   isleapyear = isleapyear(year);
  163.   switch(dst->dd_type) {
  164.     case JULIAN:
  165.       if(!isleapyear || dst->dd_day <= 59) break;
  166.     default:
  167.       mday++;
  168.       break;
  169.     case MWD:
  170.       mon = dst->dd_month-1;
  171.       week = dst->dd_week;
  172.       if(week == 5) {
  173.         mon++;
  174.         week = 0;
  175.       }
  176.       mday = dst->dd_day - weekday(jan1ofyear(year)+days[isleapyear][mon]);
  177.       if(mday < 0) mday += 7;
  178.       mday += (week - 1) * 7 + 1;
  179.       break;
  180.   }
  181.   return((time_t)(jan1ofyear(year)+days[isleapyear][mon]+mday-1)*(time_t)86400L+
  182.          (time_t)dst->dd_secs);
  183. }
  184.  
  185. static void time2tm(time_t time)
  186. {
  187.   int isleapyear;
  188.   TM.tm_sec  = time % 60;
  189.   time /= 60;
  190.   TM.tm_min  = time % 60;
  191.   time /= 60;
  192.   TM.tm_hour = time % 24;
  193.   time /= 24;
  194.   TM.tm_year = time/365 + 70; /* guess year */
  195.   while((TM.tm_yday = time - jan1ofyear(TM.tm_year)) < 0) TM.tm_year--;
  196.   isleapyear = isleapyear(TM.tm_year);
  197.   for(TM.tm_mon = 0;
  198.       TM.tm_yday >= days[isleapyear][TM.tm_mon+1];
  199.       TM.tm_mon++);
  200.   TM.tm_mday = TM.tm_yday - days[isleapyear][TM.tm_mon] + 1;
  201.   TM.tm_wday = (time+4)%7;
  202. }
  203.  
  204. static int checkdst(time_t time)
  205. {
  206.   int year,day;
  207.   time_t t,u;
  208.   day = time / 86400L;
  209.   year = day / 365 + 70; /* guess year */
  210.   while(day - jan1ofyear(year) < 0) year--;
  211.   t = dst2time(year,&__dststart) + __stdoffset;
  212.   u = dst2time(year,&__dstend)   + __dstoffset;
  213.   if(u > t) {
  214.     return((time >= t && time < u)?1:0);
  215.   }
  216.   else {
  217.     return((time < u || time >= t)?1:0);
  218.   }
  219. }
  220.  
  221. struct tm *gmtime(const time_t *t)
  222. {
  223.   TM.tm_isdst = 0;
  224.   time2tm(*t);
  225.   return(&TM);
  226. }
  227.  
  228. struct tm *localtime(const time_t *t)
  229. {
  230.   if(!_TZ) __tzset();
  231.   TM.tm_isdst = checkdst(*t);
  232.   time2tm(*t - (TM.tm_isdst ? __dstoffset : __stdoffset));
  233.   return(&TM);
  234. }
  235.  
  236. #ifndef NO_MKTIME   /* normalize() only used by mktime() */
  237. static void normalize(int *i,int *j,int norm)
  238. {
  239.   while(*i < 0) {
  240.     *i += norm;
  241.     (*j)--;
  242.   }
  243.   while(*i >= norm) {
  244.     *i -= norm;
  245.     (*j)++;
  246.   }
  247. }
  248.  
  249. time_t mkgmtime(struct tm *tm)
  250. {
  251.   time_t t;
  252.   normalize(&tm->tm_sec,&tm->tm_min,60);
  253.   normalize(&tm->tm_min,&tm->tm_hour,60);
  254.   normalize(&tm->tm_hour,&tm->tm_mday,24);
  255.   normalize(&tm->tm_mon,&tm->tm_year,12);
  256.   while(tm->tm_mday > monlen[isleapyear(tm->tm_year)][tm->tm_mon]) {
  257.     tm->tm_mday -= monlen[isleapyear(tm->tm_year)][tm->tm_mon];
  258.     tm->tm_mon++;
  259.     if(tm->tm_mon == 12) {
  260.       tm->tm_mon = 0;
  261.       tm->tm_year++;
  262.     }
  263.   }
  264.   while(tm->tm_mday < 0) {
  265.     tm->tm_mon--;
  266.     if(tm->tm_mon == -1) {
  267.       tm->tm_mon = 11;
  268.       tm->tm_year--;
  269.     }
  270.     tm->tm_mday += monlen[isleapyear(tm->tm_year)][tm->tm_mon];
  271.   }
  272.   tm->tm_yday = tm->tm_mday + days[isleapyear(tm->tm_year)][tm->tm_mon] - 1;
  273.   t = jan1ofyear(tm->tm_year) + tm->tm_yday;
  274.   tm->tm_wday = weekday(t);
  275.   if(tm->tm_year < 70) return((time_t)0);
  276.   t = t * 86400L + tm->tm_hour * 3600L + tm->tm_min * 60L + (time_t)tm->tm_sec;
  277.   return(t);
  278. }
  279.  
  280. time_t mktime(struct tm *tm)
  281. {
  282.   time_t t;
  283.   if(!_TZ) __tzset();
  284.   t = mkgmtime(tm);
  285.   if(tm->tm_isdst < 0) tm->tm_isdst = checkdst(t);
  286.   t += tm->tm_isdst ? __dstoffset : __stdoffset;
  287.   return(t);
  288. }
  289. #endif /* !NO_MKTIME */
  290.  
  291. static long gettime(char **s)
  292. {
  293.   long num,time;
  294.   for(num = 0;**s >= '0' && **s <= '9';(*s)++) {
  295.     num = 10*num + (**s - '0');
  296.   }
  297.   time = 3600L * num;
  298.   if(**s == ':') {
  299.     (*s)++;
  300.     for(num = 0;**s >= '0' && **s <= '9';(*s)++) {
  301.       num = 10*num + (**s - '0');
  302.     }
  303.     time += 60 * num;
  304.     if(**s == ':') {
  305.       (*s)++;
  306.       for(num = 0;**s >= '0' && **s <= '9';(*s)++) {
  307.         num = 10*num + (**s - '0');
  308.       }
  309.       time += num;
  310.     }
  311.   }
  312.   return(time);
  313. }
  314.  
  315. static void getdstdate(char **s,struct dstdate *dst)
  316. {
  317.   switch(**s) {
  318.     case 'J':
  319.     case 'j':
  320.       (*s)++;
  321.       dst->dd_type = JULIAN;
  322.       for(dst->dd_day = 0;**s >= '0' && **s <= '9';(*s)++) {
  323.         dst->dd_day = 10*dst->dd_day + (**s - '0');
  324.       }
  325.       break;
  326.     case 'M':
  327.     case 'm':
  328.       (*s)++;
  329.       dst->dd_type = MWD;
  330.       for(dst->dd_month = 0;**s >= '0' && **s <= '9';(*s)++) {
  331.         dst->dd_month = 10*dst->dd_month + (**s - '0');
  332.       }
  333.       if(**s != '.') return;
  334.       (*s)++;
  335.       for(dst->dd_week = 0;**s >= '0' && **s <= '9';(*s)++) {
  336.         dst->dd_week = 10*dst->dd_week + (**s - '0');
  337.       }
  338.       if(**s != '.') return;
  339.       (*s)++;
  340.       for(dst->dd_day = 0;**s >= '0' && **s <= '9';(*s)++) {
  341.         dst->dd_day = 10*dst->dd_day + (**s - '0');
  342.       }
  343.       break;
  344.     default:
  345.       dst->dd_type = JULIAN0;
  346.       for(dst->dd_day = 0;**s >= '0' && **s <= '9';(*s)++) {
  347.         dst->dd_day = 10*dst->dd_day + (**s - '0');
  348.       }
  349.       break;
  350.   }
  351.   if(**s == '/') {
  352.     (*s)++;
  353.     dst->dd_secs = gettime(s);
  354.   }
  355. }
  356.  
  357. void __tzset(void)
  358. {
  359.   char *s,*t;
  360.   int minus = 0;
  361.   time_t tm;
  362.   struct Library *LocaleBase;
  363.   struct Locale *loc = NULL;
  364.   if (real_timezone_is_set)
  365.     return;
  366.   real_timezone_is_set = TRUE;
  367.   __dststart.dd_secs = __dstend.dd_secs = 7200;
  368.   __dststart.dd_type = __dstend.dd_type = MWD;
  369.   __dststart.dd_month = 4;
  370.   __dststart.dd_week = 1;
  371.   __dstend.dd_month = 10;
  372.   __dstend.dd_week = 5;
  373.   __dststart.dd_day = __dstend.dd_day = 0; /* sunday */
  374.   _TZ = NULL;
  375.   if (AVAIL_GETVAR) {                /* GetVar() available? */
  376.     if(GetVar(TZ_ENVVAR,TZ,TZLEN,GVF_GLOBAL_ONLY) > 0)
  377.       _TZ = TZ;
  378.   } else
  379.       _TZ = getenv(TZ_ENVVAR);
  380.   if (_TZ == NULL || !_TZ[0]) {
  381.     static char gmt[MAXTIMEZONELEN] = DEFAULT_TZ_STR;
  382.     LocaleBase = OpenLibrary("locale.library",0);
  383.     if(LocaleBase) {
  384.       loc = OpenLocale(0);  /* cannot return null */
  385.       if (loc->loc_GMTOffset == -300 || loc->loc_GMTOffset == 300) {
  386.         BPTR eh;
  387.         if (eh = Lock("ENV:sys/locale.prefs", ACCESS_READ))
  388.           UnLock(eh);
  389.         else {
  390.           real_timezone_is_set = FALSE;
  391.           loc->loc_GMTOffset = 300; /* Amigados bug: default when locale is */
  392.         }                           /* not initialized can have wrong sign  */
  393.       }
  394.       sprintf(gmt, "GMT%ld:%02ld", loc->loc_GMTOffset / 60L,
  395.               labs(loc->loc_GMTOffset) % 60L);
  396.       CloseLocale(loc);
  397.       CloseLibrary(LocaleBase);
  398.     } else
  399.       real_timezone_is_set = FALSE;
  400.     _TZ = gmt;
  401.   }
  402.   for(s = _TZ,t = __tzstn;*s && *s != '+' && *s != '-' && *s != ',' &&
  403.       (*s < '0' || *s > '9');s++) {
  404.     if(t-__tzstn < MAXTIMEZONELEN-1) *(t++) = *s;
  405.   }
  406.   *t = '\0';
  407.   if(*s == '+') {
  408.     s++;
  409.   }
  410.   else {
  411.     if(*s == '-') {
  412.       minus = 1;
  413.       s++;
  414.     }
  415.   }
  416.   __stdoffset = gettime(&s);
  417.   if(minus) {
  418.     __stdoffset *= -1;
  419.   }
  420.   if(*s) {
  421.     minus = 0;
  422.     for(t = __tzdtn;*s && *s != '+' && *s != '-' && *s != ',' &&
  423.         (*s < '0' || *s > '9');s++) {
  424.       if(t-__tzdtn < MAXTIMEZONELEN-1) *(t++) = *s;
  425.     }
  426.     *t = '\0';
  427.     if(*s == '+') {
  428.       s++;
  429.     }
  430.     else {
  431.       if(*s == '-') {
  432.         minus = 1;
  433.         s++;
  434.       }
  435.     }
  436.     if(*s && *s != ',') {
  437.       __dstoffset = gettime(&s);
  438.       if(minus) {
  439.         __dstoffset *= -1;
  440.       }
  441.     }
  442.     else {
  443.       __dstoffset = __stdoffset - 3600L;
  444.     }
  445.     if(*s == ',') {
  446.       s++;
  447.       getdstdate(&s,&__dststart);
  448.       if(*s == ',') {
  449.         s++;
  450.         getdstdate(&s,&__dstend);
  451.       }
  452.     }
  453.   }
  454.   else {
  455.     __dstoffset = __stdoffset;
  456.   }
  457.   time2tm(time(&tm));
  458.   __isdst = checkdst(tm);
  459.   __daylight = (__dstoffset != __stdoffset);
  460.   __timezone = __stdoffset;
  461.   __nextdstchange = dst2time(TM.tm_year, __isdst ? &__dstend : &__dststart);
  462.   if(tm >= __nextdstchange) {
  463.     __nextdstchange = dst2time(TM.tm_year+1,
  464.                                __isdst ? &__dstend : &__dststart);
  465.   }
  466.   __tzname[0] = __tzstn;
  467.   __tzname[1] = __tzdtn;
  468. #ifdef __SASC
  469.   if (loc)         /* store TZ envvar if data read from locale */
  470.     set_TZ(__timezone, __daylight);
  471. #endif
  472. }
  473.  
  474. time_t time(time_t *tm)
  475. {
  476.   static time_t last_check = 0;
  477.   static struct _ixgmtoffset {
  478.     LONG  Offset;
  479.     UBYTE DST;
  480.     UBYTE Null;
  481.   } ixgmtoffset;
  482.   static char *envvarstr; /* ptr to environm. string (used if !AVAIL_GETVAR) */
  483.   struct DateStamp ds;
  484.   time_t now;
  485.   DateStamp(&ds);
  486.   now = ds.ds_Days * 86400L + ds.ds_Minute * 60L +
  487.         ds.ds_Tick / TICKS_PER_SECOND;
  488.   if(now - last_check > CHECK) {
  489.     last_check = now;
  490.     if (AVAIL_GETVAR)    /* GetVar() available? */
  491.       if(GetVar("IXGMTOFFSET",(STRPTR)&ixgmtoffset,6,
  492.                 GVF_BINARY_VAR|GVF_GLOBAL_ONLY) == -1) {
  493.         __tzset();
  494.         ixgmtoffset.Offset = __isdst ? __dstoffset : __stdoffset;
  495.       }
  496.     else
  497.       if (envvarstr=getenv("IXGMTOFFSET")) {
  498.         ixgmtoffset = *((struct _ixgmtoffset *)envvarstr);  /* copy to struct */
  499.         __tzset();
  500.         ixgmtoffset.Offset = __isdst ? __dstoffset : __stdoffset;
  501.       }
  502.   }
  503.   now += AMIGA2UNIX;
  504.   now += ixgmtoffset.Offset;
  505.   if(tm) *tm = now;
  506.   return(now);
  507. }
  508.  
  509. #ifdef __SASC
  510.  
  511. /* Stores data from timezone and daylight to ENV:TZ.                  */
  512. /* ENV:TZ is required to exist by some other SAS/C library functions, */
  513. /* like stat() or fstat().                                            */
  514. void set_TZ(long time_zone, int day_light)
  515. {
  516.   char put_tz[MAXTIMEZONELEN];  /* string for putenv: "TZ=aaabbb:bb:bbccc" */
  517.   int offset;
  518.   void *exists;     /* dummy ptr to see if global envvar TZ already exists */
  519.   if (AVAIL_GETVAR)
  520.      exists = (void *)FindVar(TZ_ENVVAR,GVF_GLOBAL_ONLY);  /* OS V36+ */
  521.   else
  522.      exists = (void *)getenv(TZ_ENVVAR);
  523.   /* see if there is already an envvar TZ_ENVVAR. If not, create it */
  524.   if (exists == NULL) {
  525.     /* create TZ string by pieces: */
  526.     sprintf(put_tz, "GMT%+ld", time_zone / 3600L);
  527.     if (time_zone % 3600L) {
  528.       offset = (int) labs(time_zone % 3600L);
  529.       sprintf(put_tz + strlen(put_tz), ":%02d", offset / 60);
  530.       if (offset % 60)
  531.         sprintf(put_tz + strlen(put_tz), ":%02d", offset % 60);
  532.     }
  533.     if (day_light)
  534.       strcat(put_tz,"DST");
  535.     if (AVAIL_GETVAR)       /* store TZ to ENV:TZ. */
  536.        SetVar(TZ_ENVVAR,put_tz,-1,GVF_GLOBAL_ONLY);     /* OS V36+ */
  537.     else
  538.        setenv(TZ_ENVVAR,put_tz, 1);
  539.   }
  540. }
  541. #endif /* __SASC */
  542.