home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / rcs57pc3.zip / rcs / src / maketime.c < prev    next >
C/C++ Source or Header  |  1996-03-21  |  9KB  |  334 lines

  1. /* Convert struct partime into time_t.  */
  2.  
  3. /* Copyright 1992, 1993, 1994, 1995 Paul Eggert
  4.    Distributed under license by the Free Software Foundation, Inc.
  5.  
  6. This file is part of RCS.
  7.  
  8. RCS is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. RCS is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with RCS; see the file COPYING.
  20. If not, write to the Free Software Foundation,
  21. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22.  
  23. Report problems and direct all questions to:
  24.  
  25.     rcs-bugs@cs.purdue.edu
  26.  
  27. */
  28.  
  29. #include "rcsbase.h"
  30.  
  31. #include "partime.h"
  32. #include "maketime.h"
  33.  
  34. char const maketId[]
  35.   = "$Id: maketime.c,v 5.11 1995/06/16 06:19:24 eggert Exp $";
  36.  
  37. static int isleap P((int));
  38. static int month_days P((struct tm const*));
  39. static time_t maketime P((struct partime const*,time_t));
  40.  
  41. /*
  42. * For maximum portability, use only localtime and gmtime.
  43. * Make no assumptions about the time_t epoch or the range of time_t values.
  44. * Avoid mktime because it's not universal and because there's no easy,
  45. * portable way for mktime to yield the inverse of gmtime.
  46. */
  47.  
  48. #define TM_YEAR_ORIGIN 1900
  49.  
  50.     static int
  51. isleap(y)
  52.     int y;
  53. {
  54.     return (y&3) == 0  &&  (y%100 != 0 || y%400 == 0);
  55. }
  56.  
  57. static int const month_yday[] = {
  58.     /* days in year before start of months 0-12 */
  59.     0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
  60. };
  61.  
  62. /* Yield the number of days in TM's month.  */
  63.     static int
  64. month_days(tm)
  65.     struct tm const *tm;
  66. {
  67.     int m = tm->tm_mon;
  68.     return month_yday[m+1] - month_yday[m]
  69.         + (m==1 && isleap(tm->tm_year + TM_YEAR_ORIGIN));
  70. }
  71.  
  72. /*
  73. * Convert UNIXTIME to struct tm form.
  74. * Use gmtime if available and if !LOCALZONE, localtime otherwise.
  75. */
  76.     struct tm *
  77. time2tm(unixtime, localzone)
  78.     time_t unixtime;
  79.     int localzone;
  80. {
  81.     struct tm *tm;
  82. #    if TZ_must_be_set
  83.         static char const *TZ;
  84.         if (!TZ  &&  !(TZ = getenv("TZ")))
  85.             faterror("The TZ environment variable is not set; please set it to your timezone");
  86. #    endif
  87.     if (localzone  ||  !(tm = gmtime(&unixtime)))
  88.         tm = localtime(&unixtime);
  89.     return tm;
  90. }
  91.  
  92. /* Yield A - B, measured in seconds.  */
  93.     time_t
  94. difftm(a, b)
  95.     struct tm const *a, *b;
  96. {
  97.     int ay = a->tm_year + (TM_YEAR_ORIGIN - 1);
  98.     int by = b->tm_year + (TM_YEAR_ORIGIN - 1);
  99.     int difference_in_day_of_year = a->tm_yday - b->tm_yday;
  100.     int intervening_leap_days = (
  101.         ((ay >> 2) - (by >> 2))
  102.         - (ay/100 - by/100)
  103.         + ((ay/100 >> 2) - (by/100 >> 2))
  104.     );
  105.     time_t difference_in_years = ay - by;
  106.     time_t difference_in_days = (
  107.         difference_in_years*365
  108.         + (intervening_leap_days + difference_in_day_of_year)
  109.     );
  110.     return
  111.         (
  112.             (
  113.                 24*difference_in_days
  114.                 + (a->tm_hour - b->tm_hour)
  115.             )*60 + (a->tm_min - b->tm_min)
  116.         )*60 + (a->tm_sec - b->tm_sec);
  117. }
  118.  
  119. /*
  120. * Adjust time T by adding SECONDS.  SECONDS must be at most 24 hours' worth.
  121. * Adjust only T's year, mon, mday, hour, min and sec members;
  122. * plus adjust wday if it is defined.
  123. */
  124.     void
  125. adjzone(t, seconds)
  126.     register struct tm *t;
  127.     long seconds;
  128. {
  129.     /*
  130.     * This code can be off by a second if SECONDS is not a multiple of 60,
  131.     * if T is local time, and if a leap second happens during this minute.
  132.     * But this bug has never occurred, and most likely will not ever occur.
  133.     * Liberia, the last country for which SECONDS % 60 was nonzero,
  134.     * switched to UTC in May 1972; the first leap second was in June 1972.
  135.     */
  136.     int leap_second = t->tm_sec == 60;
  137.     long sec = seconds + (t->tm_sec - leap_second);
  138.     if (sec < 0) {
  139.         if ((t->tm_min -= (59-sec)/60) < 0) {
  140.         if ((t->tm_hour -= (59-t->tm_min)/60) < 0) {
  141.             t->tm_hour += 24;
  142.             if (TM_DEFINED(t->tm_wday)  &&  --t->tm_wday < 0)
  143.             t->tm_wday = 6;
  144.             if (--t->tm_mday <= 0) {
  145.             if (--t->tm_mon < 0) {
  146.                 --t->tm_year;
  147.                 t->tm_mon = 11;
  148.             }
  149.             t->tm_mday = month_days(t);
  150.             }
  151.         }
  152.         t->tm_min += 24 * 60;
  153.         }
  154.         sec += 24L * 60 * 60;
  155.     } else
  156.         if (60 <= (t->tm_min += sec/60))
  157.         if (24 <= (t->tm_hour += t->tm_min/60)) {
  158.             t->tm_hour -= 24;
  159.             if (TM_DEFINED(t->tm_wday)  &&  ++t->tm_wday == 7)
  160.             t->tm_wday = 0;
  161.             if (month_days(t) < ++t->tm_mday) {
  162.             if (11 < ++t->tm_mon) {
  163.                 ++t->tm_year;
  164.                 t->tm_mon = 0;
  165.             }
  166.             t->tm_mday = 1;
  167.             }
  168.         }
  169.     t->tm_min %= 60;
  170.     t->tm_sec = (int) (sec%60) + leap_second;
  171. }
  172.  
  173. /*
  174. * Convert TM to time_t, using localtime if LOCALZONE and gmtime otherwise.
  175. * Use only TM's year, mon, mday, hour, min, and sec members.
  176. * Ignore TM's old tm_yday and tm_wday, but fill in their correct values.
  177. * Yield -1 on failure (e.g. a member out of range).
  178. * Posix 1003.1-1990 doesn't allow leap seconds, but some implementations
  179. * have them anyway, so allow them if localtime/gmtime does.
  180. */
  181.     time_t
  182. tm2time(tm, localzone)
  183.     struct tm *tm;
  184.     int localzone;
  185. {
  186.     /* Cache the most recent t,tm pairs; 1 for gmtime, 1 for localtime.  */
  187.     static time_t t_cache[2];
  188.     static struct tm tm_cache[2];
  189.  
  190.     time_t d, gt;
  191.     struct tm const *gtm;
  192.     /*
  193.     * The maximum number of iterations should be enough to handle any
  194.     * combinations of leap seconds, time zone rule changes, and solar time.
  195.     * 4 is probably enough; we use a bigger number just to be safe.
  196.     */
  197.     int remaining_tries = 8;
  198.  
  199.     /* Avoid subscript errors.  */
  200.     if (12 <= (unsigned)tm->tm_mon)
  201.         return -1;
  202.  
  203.     tm->tm_yday = month_yday[tm->tm_mon] + tm->tm_mday
  204.         -  (tm->tm_mon<2  ||  ! isleap(tm->tm_year + TM_YEAR_ORIGIN));
  205.  
  206.     /* Make a first guess.  */
  207.     gt = t_cache[localzone];
  208.     gtm = gt ? &tm_cache[localzone] : time2tm(gt,localzone);
  209.  
  210.     /* Repeatedly use the error from the guess to improve the guess.  */
  211.     while ((d = difftm(tm, gtm)) != 0) {
  212.         if (--remaining_tries == 0)
  213.             return -1;
  214.         gt += d;
  215.         gtm = time2tm(gt,localzone);
  216.     }
  217.     t_cache[localzone] = gt;
  218.     tm_cache[localzone] = *gtm;
  219.  
  220.     /*
  221.     * Check that the guess actually matches;
  222.     * overflow can cause difftm to yield 0 even on differing times,
  223.     * or tm may have members out of range (e.g. bad leap seconds).
  224.     */
  225.     if (   (tm->tm_year ^ gtm->tm_year)
  226.         |  (tm->tm_mon  ^ gtm->tm_mon)
  227.         |  (tm->tm_mday ^ gtm->tm_mday)
  228.         |  (tm->tm_hour ^ gtm->tm_hour)
  229.         |  (tm->tm_min  ^ gtm->tm_min)
  230.         |  (tm->tm_sec  ^ gtm->tm_sec))
  231.         return -1;
  232.  
  233.     tm->tm_wday = gtm->tm_wday;
  234.     return gt;
  235. }
  236.  
  237. /*
  238. * Check *PT and convert it to time_t.
  239. * If it is incompletely specified, use DEFAULT_TIME to fill it out.
  240. * Use localtime if PT->zone is the special value TM_LOCAL_ZONE.
  241. * Yield -1 on failure.
  242. * ISO 8601 day-of-year and week numbers are not yet supported.
  243. */
  244.     static time_t
  245. maketime(pt, default_time)
  246.     struct partime const *pt;
  247.     time_t default_time;
  248. {
  249.     int localzone, wday;
  250.     struct tm tm;
  251.     struct tm *tm0 = 0;
  252.     time_t r;
  253.  
  254.     tm0 = 0; /* Keep gcc -Wall happy.  */
  255.     localzone = pt->zone==TM_LOCAL_ZONE;
  256.  
  257.     tm = pt->tm;
  258.  
  259.     if (TM_DEFINED(pt->ymodulus) || !TM_DEFINED(tm.tm_year)) {
  260.         /* Get tm corresponding to current time.  */
  261.         tm0 = time2tm(default_time, localzone);
  262.         if (!localzone)
  263.         adjzone(tm0, pt->zone);
  264.     }
  265.  
  266.     if (TM_DEFINED(pt->ymodulus))
  267.         tm.tm_year +=
  268.         (tm0->tm_year + TM_YEAR_ORIGIN)/pt->ymodulus * pt->ymodulus;
  269.     else if (!TM_DEFINED(tm.tm_year)) {
  270.         /* Set default year, month, day from current time.  */
  271.         tm.tm_year = tm0->tm_year + TM_YEAR_ORIGIN;
  272.         if (!TM_DEFINED(tm.tm_mon)) {
  273.         tm.tm_mon = tm0->tm_mon;
  274.         if (!TM_DEFINED(tm.tm_mday))
  275.             tm.tm_mday = tm0->tm_mday;
  276.         }
  277.     }
  278.  
  279.     /* Convert from partime year (Gregorian) to Posix year.  */
  280.     tm.tm_year -= TM_YEAR_ORIGIN;
  281.  
  282.     /* Set remaining default fields to be their minimum values.  */
  283.     if (!TM_DEFINED(tm.tm_mon)) tm.tm_mon = 0;
  284.     if (!TM_DEFINED(tm.tm_mday)) tm.tm_mday = 1;
  285.     if (!TM_DEFINED(tm.tm_hour)) tm.tm_hour = 0;
  286.     if (!TM_DEFINED(tm.tm_min)) tm.tm_min = 0;
  287.     if (!TM_DEFINED(tm.tm_sec)) tm.tm_sec = 0;
  288.  
  289.     if (!localzone)
  290.         adjzone(&tm, -pt->zone);
  291.     wday = tm.tm_wday;
  292.  
  293.     /* Convert and fill in the rest of the tm.  */
  294.     r = tm2time(&tm, localzone);
  295.  
  296.     /* Check weekday.  */
  297.     if (r != -1  &&  TM_DEFINED(wday)  &&  wday != tm.tm_wday)
  298.         return -1;
  299.  
  300.     return r;
  301. }
  302.  
  303. /* Parse a free-format date in SOURCE, yielding a Unix format time.  */
  304.     time_t
  305. str2time(source, default_time, default_zone)
  306.     char const *source;
  307.     time_t default_time;
  308.     long default_zone;
  309. {
  310.     struct partime pt;
  311.  
  312.     if (*partime(source, &pt))
  313.         return -1;
  314.     if (pt.zone == TM_UNDEFINED_ZONE)
  315.         pt.zone = default_zone;
  316.     return maketime(&pt, default_time);
  317. }
  318.  
  319. #if TEST
  320. #include <stdio.h>
  321.     int
  322. main(argc, argv) int argc; char **argv;
  323. {
  324.     time_t default_time = time((time_t *)0);
  325.     long default_zone = argv[1] ? atol(argv[1]) : 0;
  326.     char buf[1000];
  327.     while (gets(buf)) {
  328.         time_t t = str2time(buf, default_time, default_zone);
  329.         printf("%s", asctime(gmtime(&t)));
  330.     }
  331.     return 0;
  332. }
  333. #endif
  334.