home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / lan / soss.arj / SRC / DTIME.C < prev    next >
C/C++ Source or Header  |  1991-04-11  |  3KB  |  98 lines

  1. /*
  2.  *  dtime.c --
  3.  *      Converts dos to unix time, ie. # of seconds since Jan 1, 1970.
  4.  *
  5.  *  Author: See-Mong Tan
  6.  */
  7.  
  8. #ifdef RCSID
  9. static char _rcsid_ = "$Id: dtime.c_v 1.4 1991/04/11 20:35:05 richb Exp $";
  10. #endif
  11.  
  12. #include "common.h"
  13.  
  14. /*
  15.  *  bool_t dtime_init() --
  16.  *      Initialize the time module.  Should be called before any other
  17.  *    routine in this module.  Returns TRUE if no error occurs.
  18.  */
  19. bool_t dtime_init()
  20. {
  21.         if (!getenv ("TZ")) {
  22.         fprintf (stderr, "Timezone not set; SET TZ and try again\n");
  23.         return FALSE;
  24.     }
  25.     tzset();        /* set global time variables */
  26.  
  27.     DBGPRT2 (nfsdebug, "timezone is GMT-%ld minutes; daylight is %d",
  28.          timezone / 60, daylight);
  29.     return TRUE;
  30. }
  31.  
  32. /* cumulative count of the days in a month */
  33. static long dayspermonth[] = {
  34.      0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365,
  35. };
  36.  
  37. /*
  38.  * long unixtime(unsigned time, unsigned date) --
  39.  *      Converts DOS style time and date to UNIX style time, ie. # of
  40.  *    seconds since midnight, Dec 31, 1969.
  41.  */
  42. long unixtime(time, date)
  43.     unsigned time, date;
  44. {
  45.     long t = 0;
  46.     long sec, min, hour, day, month, year;
  47.     
  48.     
  49.     sec = (long) ((time & 0x1f) << 1);
  50.     min = (long) ((time >> 5) & 0x3f);
  51.     hour = (long) ((time >> 11) & 0x1f);
  52.     t = sec + min * 60 + hour * 60 * 60;
  53.  
  54.     day = (long) (date & 0x1f);
  55.     t += (day - 1) * 24 * 60 * 60;            /* day of month */
  56.     /* calculate the month */
  57.     month = (long) ((date >> 5) & 0xf);        /* month field */
  58.     t += dayspermonth[(int) month - 1] * 24 * 60 * 60;
  59.  
  60.     /* calculate the year */
  61.     year = (long) (((date >> 9) & 0x7f) + 1980);    /* year field */
  62.     t += (year - 1970) * 365 * 24 * 60 * 60;
  63.  
  64.     /* now add the number of leap days since 1970 including this */
  65.     t += (long) ((year - 1968) >> 2) * 24 * 60 * 60;
  66.  
  67.     if (year % 4 == 0 && month < 3)            /* this year is leap */
  68.         t -= (long) 24 * 60 * 60;        /* take way 29 feb */
  69.     t += timezone;        /* take away difference in secs from GMT */
  70.  
  71.     return t;
  72. }
  73.  
  74. /*
  75.  * void dostime(long unixtime, unsigned *time, unsigned *date) --
  76.  *      Converts Unix time (seconds since midnight) into DOS-style
  77.  *      time format.
  78.  *    seconds since midnight, Dec 31, 1969.
  79.  */
  80. void dostime (unixtime, date, time)
  81.      long unixtime;
  82.      unsigned *date, *time;
  83. {
  84.     struct tm *t;
  85.  
  86.     /* convert secs to time struct; adjust for time zone */
  87.     unixtime -= timezone;
  88.     t = gmtime((time_t *) &unixtime);
  89.     if (t == NULL) {
  90.     *date = 0;
  91.     *time = 0;
  92.     }
  93.     else {
  94.     *date = t->tm_mday + ((t->tm_mon + 1) << 5) + ((t->tm_year - 80) << 9);
  95.     *time = (t->tm_sec >> 1) + (t->tm_min << 5) + (t->tm_hour << 11);
  96.     }
  97. }
  98.