home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 2.ddi / CLIBSRC3.ZIP / TZSET.CAS < prev    next >
Encoding:
Text File  |  1992-06-10  |  8.3 KB  |  230 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - tzset.cas
  3.  *
  4.  * function(s)
  5.  *        tzset     - UNIX time compatibility
  6.  *        __isDst   - determines whether daylight savings is in effect
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*
  10.  *      C/C++ Run Time Library - Version 5.0
  11.  *
  12.  *      Copyright (c) 1987, 1992 by Borland International
  13.  *      All Rights Reserved.
  14.  *
  15.  */
  16.  
  17.  
  18. #pragma inline
  19. #include <asmrules.h>
  20. #include <_io.h>
  21. #include <io.h>
  22. #include <time.h>
  23. #include <ctype.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. #define YES 1
  29. #define NO  0
  30.  
  31. #define Normal    0
  32. #define Daylight  1
  33. #define TZstrlen        3        /* Len of tz string(- null terminator) */
  34. #define DefaultTimeZone 5L
  35. #define DefaultDaylight YES
  36. #define DefaultTZname   "EST"    /* Default normal time zone name */
  37. #define DefaultDSTname  "EDT"    /* Default daylight savings zone name */
  38.  
  39. unsigned _monthDay [] =
  40. {
  41.      0,   31,  59,  90, 120, 151, 181, 212, 243, 273, 304, 334, 365
  42. };
  43.  
  44. #if !defined( _RTLDLL )
  45. static char _DfltZone[ TZstrlen+1 ], _DfltLight[ TZstrlen+1 ];
  46. char  *const tzname[2] = {&_DfltZone[0], &_DfltLight[0]};
  47.  
  48. long  timezone = DefaultTimeZone * 60L * 60L; /* Set for EST */
  49.  
  50. int   daylight = DefaultDaylight;             /* Apply daylight savings */
  51. #endif
  52.  
  53. /*---------------------------------------------------------------------*
  54.  
  55. Name            tzset
  56.  
  57. Usage           void tzset(void);
  58.  
  59. Prototype in    time.h
  60.  
  61. Description     sets local timezone info base on the "TZ" environment string
  62.  
  63. Return value    None
  64.  
  65. *---------------------------------------------------------------------*/
  66. void  _FARFUNC tzset(void)
  67. {
  68.         register int  i;       /* A loop index */
  69.         char *env;             /* Pointer to "TZ" environment string */
  70.  
  71. #define  issign(c)   (((c) == '-') || ((c) == '+'))
  72.  
  73.         if (
  74.            /************************************************************
  75.               1. Check for "TZ" string in the environment.
  76.                     env[0] - 1st char in time zone name
  77.                     env[1] - 2nd "    "   "    "
  78.                     env[2] - 3rd "    "   "    "
  79.                     env[3] - 1st char in time zone difference value
  80.                     env[4] - 2nd "    "   "    "       "        "
  81.               2. Rule out short strings.
  82.               3. Rule out non A-Z time zone characters.
  83.               4. Rule out bad time zone difference numbers.
  84.                  a. Not a +/- or 0-9.
  85.                  b. Sign with no following digit(s).
  86.            ************************************************************/
  87. /* 1. */ ((env = getenv("TZ")) == 0)                                      ||
  88. /* 2. */ (strlen(env) < (TZstrlen+1))                                     ||
  89. /* 3. */ ((!isalpha(env[0])) || (!isalpha(env[1])) || (!isalpha(env[2]))) ||
  90. /* 4a.*/ (!(issign(env[ TZstrlen ]) || isdigit(env[ TZstrlen ])))         ||
  91. /* 4b.*/ ((!isdigit(env[ TZstrlen ])) && (!isdigit(env[ TZstrlen+1 ]))) )
  92.         {
  93.                 /*----- Missing or bogus "TZ" string, set default values -----*/
  94.  
  95.                 daylight = DefaultDaylight;
  96.                 timezone = DefaultTimeZone * 60L * 60L;
  97.                 strcpy(tzname[Normal], DefaultTZname);
  98.                 strcpy(tzname[Daylight], DefaultDSTname);
  99.         }
  100.         else    /*----- Parse the "TZ" string and set values from string -----*/
  101.         {
  102.                 memset(tzname[Daylight], 0, TZstrlen+1); /* Dlt daylight to NULL  */
  103.                 strncpy(tzname[Normal], env, TZstrlen);  /* Set zime zone string  */
  104.                 tzname[Normal][TZstrlen] = '\0';         /* Force NULL termination*/
  105.                 timezone = atol(&env[TZstrlen]) * 3600L; /* Base timezone on "TZ" */
  106.  
  107.                 /*----- Scan for optional daylight savings field -----*/
  108.  
  109.                 /* Scan for string start  */
  110.                 for (daylight=NO,i=TZstrlen; env[i]; i++)
  111.                 {
  112.                         if (isalpha(env[i]))        /* Found the string start */
  113.                         {
  114.                                 if ((strlen(&env[i])<TZstrlen) ||
  115.                                     (!isalpha(env[i+1]))       ||
  116.                                     (!isalpha(env[i+2])) )
  117.                                         break;
  118.                                 /* Copy and null-terminate dlt sav string */
  119.                                 strncpy(tzname[Daylight], &env[i], TZstrlen);
  120.                                 tzname[Daylight][TZstrlen] = '\0';
  121.                                 daylight = YES;
  122.                                 break;
  123.                         }
  124.                 }
  125.         }
  126. }
  127.  
  128. #pragma startup tzset 30
  129.  
  130. /*--------------------------------------------------------------------------*
  131.  
  132. Name            __isDST  -  determines whether daylight savings is in effect
  133.  
  134. Usage           int  pascal __isDST (unsigned hour,  unsigned yday,
  135.                                      unsigned month, unsigned year);
  136.  
  137. Prototype in    _io.h
  138.  
  139. Description     Returns non-zero if daylight savings is in effect for
  140.                 the given date.
  141.  
  142.                 If month is 0, yday is the day of the year, otherwise yday is
  143.                 the day of the month.  yday is zero based.
  144.  
  145.                 It is assumed that the caller has called tzset() to fill in
  146.                 the timezone info.
  147.  
  148. Return value    Non-zero if DST is in effect for the given date.
  149.  
  150. *---------------------------------------------------------------------------*/
  151. int pascal near __isDST(unsigned hour, unsigned yday, unsigned month, unsigned year)
  152. {
  153.         register  unsigned  temp;
  154.  
  155.         if (month == 0)         /* if only day of year given    */
  156.             {
  157.             temp = yday;
  158.             /* if Mar 1 or later and leap year, sub day to calculate month */
  159.             if (yday >= 31+28 && (((year+70) & 3) == 0))
  160.                 temp--;
  161.             for (month = 0; temp >= _monthDay[month]; month++)
  162.                 ;
  163.             }
  164.         else                    /* if month+day of month given  */
  165.             {
  166.                 yday += _monthDay[month-1];
  167.                 if (month > 3 && (((year+70) & 3) == 0))
  168.                     yday++;
  169.             }
  170.  
  171. asm     cmp     word ptr month, 4
  172. asm     jb      _notDST         /* has to be >= April           */
  173. asm     je      _check
  174. asm     cmp     word ptr month, 10
  175. asm     ja      _notDST         /* has to be <= October         */
  176. asm     jne     _isDST
  177. _check:
  178. asm     mov     bx, month
  179. asm     shl     bx, 1
  180. asm     cmp     word ptr year, 16
  181. asm     jle     _else_1         /* skip if before 1986          */
  182. asm     cmp     word ptr month, 4
  183. asm     jne     _else_1         /* skip if not April            */
  184. asm     mov     cx, _monthDay-2[bx]
  185. asm     add     cx, 7           /* day = 7th day in month       */
  186. asm     jmp     _endif_1
  187. _else_1:
  188. asm     mov     cx, _monthDay[bx]/* day = last day in month     */
  189. _endif_1:
  190. asm     mov     bx, year
  191. asm     add     bx, 1970
  192. asm     test    bl, 3           /* leap year ?                  */
  193. asm     jz      _leap
  194. asm     dec     cx              /* no --> adjust                */
  195. _leap:
  196. asm     mov     bx, year
  197. asm     inc     bx
  198. asm     sar     bx, 1
  199. asm     sar     bx, 1
  200. asm     add     bx, cx          /* add leap days since 1970     */
  201. asm     mov     ax, 365
  202. asm     mul     word ptr year   /* ax = (year-1970) * 365       */
  203. asm     add     ax, bx
  204. asm     add     ax, 4           /* 01-01-70 was Thursday        */
  205. asm     xor     dx, dx
  206. asm     mov     bx, 7           /* find day of week             */
  207. asm     div     bx
  208. asm     sub     cx, dx          /* cx = threshold day of year   */
  209. asm     mov     ax, yday
  210. asm     cmp     word ptr month, 4
  211. asm     jne     _October
  212. asm     cmp     ax, cx
  213. asm     ja      _isDST          /* is DST if past threshold     */
  214. asm     jne     _notDST
  215. asm     cmp     byte ptr hour, 2
  216. asm     jb      _notDST         /* not DST if too early         */
  217. asm     jmp     SHORT _isDST
  218. _October:
  219. asm     cmp     ax, cx
  220. asm     jb      _isDST          /* is DST if before threshold   */
  221. asm     jne     _notDST
  222. asm     cmp     byte ptr hour, 1
  223. asm     ja      _notDST         /* not DST if too late          */
  224.  
  225. _isDST:
  226.         return(1);
  227. _notDST:
  228.         return(0);
  229. }
  230.