home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / libc / dateconv.c < prev    next >
C/C++ Source or Header  |  1992-11-18  |  976b  |  45 lines

  1. /*
  2.  * dateconv - convert a split-out date back into a time_t
  3.  */
  4.  
  5. #include <time.h>
  6. #include <sys/types.h>
  7. #include <sys/timeb.h>
  8.  
  9. /* imports */
  10. extern time_t qmktime();
  11.  
  12. /* turn a (struct tm) and a few variables into a time_t, with range checking */
  13. time_t
  14. dateconv(tm, zone)
  15. register struct tm *tm;
  16. int zone;
  17. {
  18.     tm->tm_wday = tm->tm_yday = 0;
  19.  
  20.     /* validate, before going out of range on some members */
  21.     if (!validtm(tm))
  22.         return -1;
  23.  
  24.     /*
  25.      * zone should really be -zone, and tz should be set to tp->value, not
  26.      * -tp->value.  Or the table could be fixed.
  27.      */
  28.     tm->tm_min += zone;        /* mktime lets it be out of range */
  29.  
  30.     /* convert to seconds */
  31.     return qmktime(tm);
  32. }
  33.  
  34. int
  35. validtm(tm)
  36. register struct tm *tm;
  37. {
  38.     if (tm->tm_year < 0 || tm->tm_mon < 0 || tm->tm_mon > 11 ||
  39.         tm->tm_mday < 1 || tm->tm_hour < 0 || tm->tm_hour >= 24 ||
  40.         tm->tm_min < 0 || tm->tm_min > 59 ||
  41.         tm->tm_sec < 0 || tm->tm_sec > 61)    /* allow 2 leap seconds */
  42.         return 0;
  43.     return 1;
  44. }
  45.