home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / TIMEXSRC.ZIP / TMCLOCK.C < prev    next >
C/C++ Source or Header  |  1990-03-29  |  4KB  |  163 lines

  1. /* tmclock.c -- Convert tm struct to a clock value.
  2.  
  3.         Taken from WRAP, but really taken from sources listed below.
  4.  
  5. This is a PD routine that fills in one of the major holes in the suite
  6. of time conversion functions.  Read on...
  7.  
  8. Also contains xlocaltime(), which I had to include with TIMEX because
  9. the standard localtime() function doesn't work correctly in the non-root
  10. thread.  Sheesh!
  11.  
  12. */
  13.  
  14.  
  15. #include <stdio.h>
  16. #include <memory.h>
  17. #include <sys/types.h>
  18. #include <time.h>
  19.  
  20. static  struct tm        xtmblk;
  21.  
  22. struct tm *
  23. xlocaltime(
  24.         time_t          *timeP
  25.         ) {
  26.  
  27.         time_t          timval;
  28.  
  29.     memcpy( &xtmblk, localtime( timeP ), sizeof( struct tm ) );
  30.  
  31.     /* Fix up hours and minutes and seconds */
  32.     timval = *timeP - timezone;
  33.     xtmblk.tm_sec = (int)((long)timval % 60);
  34.     xtmblk.tm_min = (int)(((long)timval / 60 ) % 60);
  35.     xtmblk.tm_hour = (int)(((long)timval / 3600 ) % 24);
  36.  
  37.     return( &xtmblk );
  38. }
  39.  
  40.  
  41.  
  42. /*
  43.  
  44. *//* os_tmclock( tmP )
  45.  
  46.     Convert tm struct to time value
  47.  
  48. Stolen from Arc 5.21 sources and inserted here in original form
  49. (except for any changes needed for os-specific time representation).
  50.  
  51. */
  52.  
  53. /************************* begin tmclock.c *****************************/
  54.  
  55. /*
  56.  * Stolen from the Arc 5.21 sources...
  57.  *
  58.  * Stolen from Jef Poskanzer's tws time library, which was stolen from
  59.  * Marshall Rose's MH Message Handling system...
  60.  *
  61.  * tmclock() will convert time from a tm struct back to a clock value.
  62.  * tmjuliandate() converts a tm struct to its julian day number.
  63.  * tmsubdayclock() takes hours, minutes, and seconds from a tm struct
  64.  * and returns the number of seconds since midnight of that day. (?)
  65.  *  -- Howard Chu, August 1 1988      hyc@umix.cc.umich.edu, umix!hyc
  66.  */
  67.  
  68. /* $Header: tmclock.c,v 1.3 88/08/02 14:15:58 hyc Exp $ */
  69.  
  70. /* Julian day number of the Unix* clock's origin, 01 Jan 1970. */
  71. #define JD1970 2440587L
  72. #define    CENTURY    19
  73.  
  74. long    tzone;
  75.  
  76. long
  77. tmjuliandate( tm )
  78. struct tm *tm;
  79.     {
  80.     register int mday, mon, year;
  81.     register long a, b;
  82.     double jd;
  83.  
  84.     if ( (mday = tm -> tm_mday) < 1 || mday > 31 ||
  85.         (mon = tm -> tm_mon + 1) < 1 || mon > 12 ||
  86.         (year = tm -> tm_year) < 1 || year > 10000 )
  87.     return ( -1L );
  88.     if ( year < 100 )
  89.     year += CENTURY * 100;
  90.  
  91.     if ( mon == 1 || mon == 2 )
  92.     {
  93.     --year;
  94.     mon += 12;
  95.     }
  96.     if ( year < 1583 )
  97.     return ( -1L );
  98.     a = year / 100;
  99.     b = 2 - a + a / 4;
  100.     b += (long) ( (double) year * 365.25 );
  101.     b += (long) ( 30.6001 * ( (double) mon + 1.0 ) );
  102.     jd = mday + b + 1720994.5;
  103.     return ( (long) jd );
  104.     }
  105.  
  106.  
  107. long
  108. tmsubdayclock( tm )
  109. struct tm *tm;
  110.     {
  111.     register int sec, min, hour;
  112.     long result;
  113.     struct tm *tmcheck;
  114. #if    BSD
  115.     {
  116.        struct timeval tp;
  117.        struct timezone tzp;
  118.  
  119.        gettimeofday(&tp, &tzp);
  120.        daylight=tzp.tz_dsttime;
  121.        tzone=tzp.tz_minuteswest*(-60);
  122.     }
  123. #else
  124.     tzset();
  125.     tzone = -timezone;    /* declared as extern in SYSV <time.h> */
  126. #endif
  127.     if ( (sec = tm -> tm_sec) < 0 || sec > 59 ||
  128.         (min = tm -> tm_min) < 0 || min > 59 ||
  129.         (hour = tm -> tm_hour) < 0 || hour > 23 )
  130.     return ( -1L );
  131.  
  132.     result = ( (long)hour * 60 + min ) * 60 + sec;
  133.     result -= tzone;
  134.  
  135. #ifdef    OUT            /* MSDOS doesn't know from dst */
  136.     /* Convert back to swag the dst flag */
  137.     tmcheck = xlocaltime( &result );
  138.     if ( tmcheck->tm_isdst )
  139.     result -= 60 * 60;
  140. #endif  /* OUT */
  141.  
  142.     return ( result );
  143.     }
  144.  
  145.  
  146. long
  147. tmclock( tm )
  148. struct tm *tm;
  149.     {
  150.     register long jd, sdc, result;
  151.  
  152.     if ( ( jd = tmjuliandate( tm ) ) == -1L )
  153.     return ( -1L );
  154.     if ( ( sdc = tmsubdayclock( tm ) ) == -1L )
  155.     return ( -1L );
  156.  
  157.     result = ( jd - JD1970 ) * 24 * 60 * 60 + sdc;
  158.  
  159.     return ( result );
  160.     }
  161.  
  162. /************************* end tmclock.c *******************************/
  163.