home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / TIME / MKTIME.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  1.3 KB  |  61 lines

  1. /* mktime.c (emx+gcc) -- Copyright (c) 1990-1993 by Eberhard Mattes */
  2.  
  3. #include <sys/emx.h>
  4. #include <time.h>
  5.  
  6. time_t mktime (struct tm *t)
  7. {
  8.   time_t x;
  9.   struct tm *tmp;
  10.  
  11.   if (!_tzset_flag) tzset ();
  12.   if (t->tm_sec < 0 || t->tm_min < 0 || t->tm_hour < 0
  13.       || t->tm_mday < 0 || t->tm_mon < 0 || t->tm_year < 0)
  14.     return ((time_t)(-1));
  15.   if (t->tm_mon >= 12)
  16.     {
  17.       t->tm_year += (t->tm_mon / 12);
  18.       t->tm_mon %= 12;        /* _mktime wants 0 <= tm_mon <= 11 */
  19.     }
  20.   x = (time_t)_mktime (t) + timezone;
  21.   tmp = localtime (&x);
  22.   if (tmp == NULL)
  23.     return ((time_t)(-1));
  24.   else
  25.     *t = *tmp;
  26.   return (x);
  27. }
  28.  
  29.  
  30. /* year >= 1582, 1 <= month <= 12, 1 <= day <= 31 */
  31. /* source: emclib */
  32.  
  33. int _day (int year, int month, int day)
  34. {
  35.   int result;
  36.  
  37.   if (year < 1582)
  38.     return (-1);
  39.   result = 365 * year + day + 31 * (month - 1);
  40.   if (month <= 2)
  41.     --year;
  42.   else
  43.     result -= (4 * month + 23) / 10;
  44.   result += year / 4 - (3 * (year / 100 + 1)) / 4;
  45.   return (result);
  46. }
  47.  
  48.  
  49. int _mktime (struct tm *t)
  50. {
  51.   int result;
  52.  
  53.   result = _day (t->tm_year+1900, t->tm_mon+1, t->tm_mday);
  54.   if (result < 0)
  55.     return (-1);
  56.   result -= 719528;           /* day (1970, 1, 1); */
  57.   result *= 24*60*60;
  58.   result += t->tm_sec + 60*t->tm_min + 60*60*t->tm_hour;
  59.   return (result);
  60. }
  61.