home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / archivers / arcppc / src / rcs / tmclock.c,v < prev   
Text File  |  1998-04-23  |  3KB  |  174 lines

  1. head     1.3;
  2. branch   ;
  3. access   ;
  4. symbols  patch2:1.2;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.3
  10. date     88.08.02.14.15.58;  author hyc;  state Exp;
  11. branches ;
  12. next     1.2;
  13.  
  14. 1.2
  15. date     88.08.01.14.34.38;  author hyc;  state Exp;
  16. branches ;
  17. next     1.1;
  18.  
  19. 1.1
  20. date     88.08.01.14.17.44;  author hyc;  state Exp;
  21. branches ;
  22. next     ;
  23.  
  24.  
  25. desc
  26. @Routines to convert tm struct to clock value,
  27. @
  28.  
  29.  
  30. 1.3
  31. log
  32. @Rename timezone to tzone to avoid conflict with 4.3 routine of same name.
  33. @
  34. text
  35. @/*
  36.  * Stolen from Jef Poskanzer's tws time library, which was stolen from
  37.  * Marshall Rose's MH Message Handling system...
  38.  *
  39.  * tmclock() will convert time from a tm struct back to a clock value.
  40.  * tmjuliandate() converts a tm struct to its julian day number.
  41.  * tmsubdayclock() takes hours, minutes, and seconds from a tm struct
  42.  * and returns the number of seconds since midnight of that day. (?)
  43.  *  -- Howard Chu, August 1 1988      hyc@@umix.cc.umich.edu, umix!hyc
  44.  */
  45.  
  46. /* $Header: tmclock.c,v 1.2 88/08/01 14:34:38 hyc Locked $ */
  47.  
  48. /* Julian day number of the Unix* clock's origin, 01 Jan 1970. */
  49. #define JD1970 2440587L
  50. #define    CENTURY    19
  51. #if    BSD
  52. #include <sys/time.h>
  53. int    daylight;
  54. #else
  55. #include <time.h>
  56. #endif
  57.  
  58. long    tzone;
  59.  
  60. long
  61. tmjuliandate( tm )
  62. struct tm *tm;
  63.     {
  64.     register int mday, mon, year;
  65.     register long a, b;
  66.     double jd;
  67.  
  68.     if ( (mday = tm -> tm_mday) < 1 || mday > 31 ||
  69.         (mon = tm -> tm_mon + 1) < 1 || mon > 12 ||
  70.         (year = tm -> tm_year) < 1 || year > 10000 )
  71.     return ( -1L );
  72.     if ( year < 100 )
  73.     year += CENTURY * 100;
  74.  
  75.     if ( mon == 1 || mon == 2 )
  76.     {
  77.     --year;
  78.     mon += 12;
  79.     }
  80.     if ( year < 1583 )
  81.     return ( -1L );
  82.     a = year / 100;
  83.     b = 2 - a + a / 4;
  84.     b += (long) ( (double) year * 365.25 );
  85.     b += (long) ( 30.6001 * ( (double) mon + 1.0 ) );
  86.     jd = mday + b + 1720994.5;
  87.     return ( (long) jd );
  88.     }
  89.  
  90.  
  91. long
  92. tmsubdayclock( tm )
  93. struct tm *tm;
  94.     {
  95.     register int sec, min, hour;
  96.     register long result;
  97. #if    BSD
  98.     {
  99.        struct timeval tp;
  100.        struct timezone tzp;
  101.  
  102.        gettimeofday(&tp, &tzp);
  103.        daylight=tzp.tz_dsttime;
  104.        tzone=tzp.tz_minuteswest*(-60);
  105.     }
  106. #else
  107.     tzone=timezone;    /* declared as extern in SYSV <time.h> */
  108. #endif
  109.     if ( (sec = tm -> tm_sec) < 0 || sec > 59 ||
  110.         (min = tm -> tm_min) < 0 || min > 59 ||
  111.         (hour = tm -> tm_hour) < 0 || hour > 23 )
  112.     return ( -1L );
  113.  
  114.     result = ( hour * 60 + min ) * 60 + sec;
  115.     result -= tzone;
  116.     if ( daylight )
  117.     result -= 60 * 60;
  118.  
  119.     return ( result );
  120.     }
  121.  
  122.  
  123. long
  124. tmclock( tm )
  125. struct tm *tm;
  126.     {
  127.     register long jd, sdc, result;
  128.  
  129.     if ( ( jd = tmjuliandate( tm ) ) == -1L )
  130.     return ( -1L );
  131.     if ( ( sdc = tmsubdayclock( tm ) ) == -1L )
  132.     return ( -1L );
  133.  
  134.     result = ( jd - JD1970 ) * 24 * 60 * 60 + sdc;
  135.  
  136.     return ( result );
  137.     }
  138. @
  139.  
  140.  
  141. 1.2
  142. log
  143. @change timezone from int to long.
  144. @
  145. text
  146. @d12 1
  147. a12 1
  148. /* $Header: tmclock.c,v 1.1 88/08/01 14:17:44 hyc Exp $ */
  149. a19 1
  150. long    timezone;
  151. d24 2
  152. d70 1
  153. a70 1
  154.        timezone=tzp.tz_minuteswest*(-60);
  155. d72 2
  156. d81 1
  157. a81 1
  158.     result -= timezone;
  159. @
  160.  
  161.  
  162. 1.1
  163. log
  164. @Initial revision
  165. @
  166. text
  167. @d12 1
  168. a12 1
  169. /* $Header$ */
  170. d19 2
  171. a20 1
  172. int    daylight, timezone;
  173. @
  174.