home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / stdwin / Appls / klok / bsdsetdate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-03  |  1.6 KB  |  76 lines  |  [TEXT/????]

  1. /* Set the date and time -- 4.3 BSD Unix version */
  2.  
  3. #ifdef _AIX
  4. #include <time.h>
  5. #endif
  6. #include <sys/time.h>
  7.  
  8. #define isleap(y) ((y)%4 == 0 && ((y)%100 != 0 || (y)%400 == 0))
  9.  
  10. /* Convert a struct tm to seconds since Jan. 1, 1970.
  11.    This knows nothing about time zones or daylight saving time. */
  12.  
  13. static unsigned long
  14. tm2tv(tp)
  15.     struct tm *tp;
  16. {
  17.     static short mdays[12]=
  18.         {31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  19.     unsigned long s= 0;
  20.     int y, m, d;
  21.     
  22.     for (y= 1970; y < tp->tm_year + 1900; ++y) {
  23.         s += 365;
  24.         if (isleap(y))
  25.             ++s;
  26.     }
  27.     mdays[1]= 28 + isleap(y); /* Months have origin 0 */
  28.     for (m= 0; m < tp->tm_mon; ++m)
  29.         s += mdays[m];
  30.     s += tp->tm_mday - 1;
  31.     return ((s*24 + tp->tm_hour)*60 + tp->tm_min)*60 /*+ tp->tm_sec*/;
  32. }
  33.  
  34. /* Set the date and time from a struct tm.
  35.    The Input time is in local time.
  36.    If 'minchange' is zero, minutes and seconds are not taken
  37.    from the input but from the current system time. */
  38.  
  39. setdatetime(tp, minchange)
  40.     struct tm *tp;
  41.     int minchange; /* nonzero if we must reset minutes and seconds, too */
  42. {
  43.     struct timeval tv;
  44.     struct timezone tz;
  45.     unsigned long t;
  46.     
  47.     t= tm2tv(tp);                /* t is local time */
  48.     if (gettimeofday(&tv, &tz) != 0)
  49.         return -1;
  50.     if (tp->tm_isdst)
  51.         t -= 3600;            /* t is local time less DST */
  52.     t += tz.tz_minuteswest*60;        /* t is GMT time */
  53.     if (minchange)
  54.         t= t/60 * 60;            /* Clear seconds */
  55.     else
  56.         t= t/3600 * 3600 + tv.tv_sec % 3600; /* Use current min/sec */
  57.     tv.tv_sec= t;
  58.     if (settimeofday(&tv, &tz) != 0)
  59.         return -1;
  60.     return 0;
  61. }
  62.  
  63. #ifdef SYSV
  64.  
  65. /* XXX How do you set the date/time on system V? */
  66.  
  67. int
  68. settimeofday(ptv, ptz)
  69.     struct timeval *ptv;
  70.     struct timezone *ptz;
  71. {
  72.     return -1;
  73. }
  74.  
  75. #endif
  76.