home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / maketime.c < prev    next >
C/C++ Source or Header  |  1992-01-19  |  10KB  |  337 lines

  1. /*
  2.  * MAKETIME        derive 32-bit time value from TM structure.
  3.  *
  4.  * Usage:
  5.  *    int zone;    Minutes west of GMT, or
  6.  *            48*60 for localtime
  7.  *    time_t t;
  8.  *    struct tm *tp;    Pointer to TM structure from <time.h>
  9.  *    t = maketime(tp,zone);
  10.  *
  11.  * Returns:
  12.  *    -1 if failure; parameter out of range or nonsensical.
  13.  *    else time-value.
  14.  * Notes:
  15.  *    This code is quasi-public; it may be used freely in like software.
  16.  *    It is not to be sold, nor used in licensed software without
  17.  *    permission of the author.
  18.  *    For everyone's benefit, please report bugs and improvements!
  19.  *     Copyright 1981 by Ken Harrenstien, SRI International.
  20.  *    (ARPANET: KLH @ SRI)
  21.  */
  22. /* $Log:    maketime.c,v $
  23.  * Revision 1.2.1.1  91/01/18  12:15:21  berliner
  24.  * For CVS 1.2, contributed by Paul Eggert
  25.  *
  26.  * Revision 1.3  1991/01/18  00:14:48  eggert
  27.  * Make minimal changes to interface to CVS 1.0.
  28.  *
  29.  * Revision 5.2  1990/11/01  05:03:30  eggert
  30.  * Remove lint.
  31.  *
  32.  * Revision 5.1  1990/10/04  06:30:13  eggert
  33.  * Calculate the GMT offset of 'xxx LT' as of xxx, not as of now.
  34.  * Don't assume time_t is 32 bits.  Fix bugs near epoch and near end of time.
  35.  *
  36.  * Revision 5.0  1990/08/22  08:12:38  eggert
  37.  * Switch to GMT and fix the bugs exposed thereby.
  38.  * Permit dates past 1999/12/31.  Ansify and Posixate.
  39.  *
  40.  * Revision 1.8  88/11/08  13:54:53  narten
  41.  * allow negative timezones (-24h <= x <= 24h)
  42.  *
  43.  * Revision 1.7  88/08/28  14:47:52  eggert
  44.  * Allow cc -R.  Remove unportable "#endif XXX"s.
  45.  *
  46.  * Revision 1.6  87/12/18  17:05:58  narten
  47.  * include rcsparam.h
  48.  *
  49.  * Revision 1.5  87/12/18  11:35:51  narten
  50.  * maketime.c: fixed USG code - you have tgo call "tzset" in order to have
  51.  * "timezone" set. ("localtime" calls it, but it's probably better not to
  52.  * count on "localtime" having been called.)
  53.  *
  54.  * Revision 1.4  87/10/18  10:26:57  narten
  55.  * Updating version numbers. Changes relative to 1.0 are actually
  56.  * relative to 1.2
  57.  *
  58.  * Revision 1.3  87/09/24  13:58:45  narten
  59.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf
  60.  * warnings)
  61.  *
  62.  * Revision 1.2  87/03/27  14:21:48  jenkins
  63.  * Port to suns
  64.  *
  65.  * Revision 1.2  83/12/05  10:12:56  wft
  66.  * added cond. compilation for USG Unix; long timezone;
  67.  *
  68.  * Revision 1.1  82/05/06  11:38:00  wft
  69.  * Initial revision
  70.  *
  71.  */
  72.  
  73.  
  74. /* minimal changes needed to get this file to work for CVS rather than RCS */
  75. /* #include "rcsbase.h" */
  76. #include <sys/types.h>
  77. #include <time.h>
  78. #include "cvs.h"
  79. #define datesize 32
  80. #define faterror(x,y) error(0,x,y)
  81. #define libId(x,y) static char x[] = y;
  82. #define P(x) ()
  83. #define RCSversion 5
  84. #define VERSION(x) x
  85. #define VOID (void)
  86. #if !__STDC__
  87. #    define const
  88. #endif
  89. void str2date();
  90. Make_Date(a,b) const char *a; char *b; { str2date(a,b); }
  91.  
  92. libId(maketId, "$Id: maketime.c,v 1.2.1.1 91/01/18 12:15:21 berliner Exp $")
  93.  
  94. static const struct tm *time2tm P((time_t));
  95.  
  96. #define given(v) (0 <= (v)) /* Negative values are unspecified. */
  97.  
  98. static const int daytb[] = {
  99.     /* # days in year thus far, indexed by month (0-12!!) */
  100.     0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
  101. };
  102.  
  103.     static time_t
  104. maketime(atm,zone)
  105.     const struct tm *atm;
  106.     int zone;
  107. {
  108.     register const struct tm *tp;
  109.     register int i;
  110.     int year, yday, mon, day, hour, min, sec, leap, localzone;
  111.     int attempts;
  112.     time_t t, tres;
  113.  
  114.     attempts = 2;
  115.     localzone = zone==48*60;
  116.     tres = -1;
  117.     year = mon = day = 0;  /* Keep lint happy.  */
  118.  
  119.     do {
  120.  
  121.     if (localzone || !given(atm->tm_year)) {
  122.         if (tres == -1)
  123.             if ((tres = time((time_t*)0))  ==  -1)
  124.                 return -1;
  125.         tp = time2tm(tres);
  126.         /* Get breakdowns of default time, adjusting to zone. */
  127.         year = tp->tm_year;        /* Use to set up defaults */
  128.         yday = tp->tm_yday;
  129.         mon = tp->tm_mon;
  130.         day = tp->tm_mday;
  131.         hour = tp->tm_hour;
  132.         min = tp->tm_min;
  133.         if (localzone) {
  134.             tp = localtime(&tres);
  135.             zone =
  136.             min - tp->tm_min + 60*(
  137.                 hour - tp->tm_hour + 24*(
  138.                     /* If years differ, it's by one day. */
  139.                         year - tp->tm_year
  140.                     ?    year - tp->tm_year
  141.                     :    yday - tp->tm_yday));
  142.         }
  143.         /* Adjust the default day, month and year according to zone.  */
  144.         if ((min -= zone) < 0) {
  145.             if (hour-(59-min)/60 < 0  &&  --day <= 0) {
  146.             if (--mon < 0) {
  147.                 --year;
  148.                 mon = 11;
  149.             }
  150.             day  =  daytb[mon+1] - daytb[mon] + (mon==1&&!(year&3));
  151.             }
  152.         } else
  153.             if (
  154.               24 <= hour+min/60  &&
  155.               daytb[mon+1] - daytb[mon] + (mon==1&&!(year&3))  <  ++day
  156.             ) {
  157.                 if (11 < ++mon) {
  158.                     ++year;
  159.                     mon = 0;
  160.                 }
  161.                 day = 1;
  162.             }
  163.     }
  164.     if (zone < -24*60  ||  24*60 < zone)
  165.         return -1;
  166.  
  167.  
  168. #ifdef DEBUG
  169. printf("first YMD: %d %d %d\n",year,mon,day);
  170. #endif
  171.     tp = atm;
  172.  
  173.     /* First must find date, using specified year, month, day.
  174.      * If one of these is unspecified, it defaults either to the
  175.      * current date (if no more global spec was given) or to the
  176.      * zero-value for that spec (i.e. a more global spec was seen).
  177.      * Reject times that do not fit in time_t,
  178.      * without assuming that time_t is 32 bits or is signed.
  179.      */
  180.     if (given(tp->tm_year))
  181.       {
  182.         year = tp->tm_year;
  183.         mon = 0;        /* Since year was given, default */
  184.         day = 1;        /* for remaining specs is zero */
  185.       }
  186.     if (year < 69)            /* 1969/12/31 OK in some timezones.  */
  187.         return -1;        /* ERR: year out of range */
  188.     leap   =   !(year&3)  &&  (year%100 || !((year+300)%400));
  189.     year -= 70;            /* UNIX time starts at 1970 */
  190.  
  191.     /*
  192.      * Find day of year.
  193.      */
  194.     {
  195.         if (given(tp->tm_mon))
  196.           {    mon = tp->tm_mon;    /* Month was specified */
  197.             day = 1;        /* so set remaining default */
  198.           }
  199.         if (11 < (unsigned)mon)
  200.             return -1;        /* ERR: bad month */
  201.         if (given(tp->tm_mday)) day = tp->tm_mday;
  202.         if(day < 1
  203.          || (((daytb[mon+1]-daytb[mon]) < day)
  204.             && (day!=29 || mon!=1 || !leap) ))
  205.                 return -1;    /* ERR: bad day */
  206.         yday = daytb[mon]    /* Add # of days in months so far */
  207.           + ((leap        /* Leap year, and past Feb?  If */
  208.               && mon>1)? 1:0)    /* so, add leap day for this year */
  209.           + day-1;        /* And finally add # days this mon */
  210.  
  211.     }
  212.     if (leap+365 <= (unsigned)yday)
  213.         return -1;        /* ERR: bad YDAY */
  214.  
  215.     if (year < 0) {
  216.         if (yday != 364)
  217.         return -1;        /* ERR: too early */
  218.         t = -1;
  219.     } else {
  220.         tres = year*365;        /* Get # days of years so far */
  221.         if (tres/365 != year)
  222.             return -1;        /* ERR: overflow */
  223.         t = tres
  224.         + ((year+1)>>2)        /* plus # of leap days since 1970 */
  225.         + yday;            /* and finally add # days this year */
  226.         if (t+4 < tres)
  227.             return -1;        /* ERR: overflow */
  228.     }
  229.     tres = t;
  230.  
  231.     if (given(i = tp->tm_wday)) /* Check WDAY if present */
  232.         if (i != (tres+4)%7)    /* 1970/01/01 was Thu = 4 */
  233.             return -1;    /* ERR: bad WDAY */
  234.  
  235. #ifdef DEBUG
  236. printf("YMD: %d %d %d, T=%ld\n",year,mon,day,tres);
  237. #endif
  238.     /*
  239.      * Now determine time.  If not given, default to zeros
  240.      * (since time is always the least global spec)
  241.      */
  242.     tres *= 86400L;            /* Get # seconds (24*60*60) */
  243.     if (tres/86400L != t)
  244.         return -1;        /* ERR: overflow */
  245.     hour = min = sec = 0;
  246.     if (given(tp->tm_hour)) hour = tp->tm_hour;
  247.     if (given(tp->tm_min )) min  = tp->tm_min;
  248.     if (given(tp->tm_sec )) sec  = tp->tm_sec;
  249.     if (60 <= (unsigned)min  ||  60 < (unsigned)sec)
  250.         return -1;        /* ERR: MS out of range */
  251.     if (24 <= (unsigned)hour)
  252.         if(hour != 24 || (min+sec) !=0)    /* Allow 24:00 */
  253.             return -1;    /* ERR: H out of range */
  254.  
  255.     t = tres;
  256.     tres += sec + 60L*(zone + min + 60*hour);
  257.  
  258. #ifdef DEBUG
  259. printf("HMS: %d %d %d T=%ld\n",hour,min,sec,tres);
  260. #endif
  261.  
  262.     if (!localzone)            /* check for overflow */
  263.         return (year<0 ? (tres<0||86400L<=tres) : tres<t)  ?  -1  :  tres;
  264.  
  265.     /* Check results; LT may have had a different GMT offset back then.  */
  266.     tp = localtime(&tres);
  267.     if (given(atm->tm_sec)  &&  atm->tm_sec != tp->tm_sec)
  268.         return -1; /* If seconds don't match, we're in trouble.  */
  269.     if (!(
  270.         given(atm->tm_min)  &&  atm->tm_min != tp->tm_min  ||
  271.         given(atm->tm_hour)  &&  atm->tm_hour != tp->tm_hour  ||
  272.         given(atm->tm_mday)  &&  atm->tm_mday != tp->tm_mday  ||
  273.         given(atm->tm_mon)  &&  atm->tm_mon != tp->tm_mon  ||
  274.         given(atm->tm_year)  &&  atm->tm_year != tp->tm_year
  275.     ))
  276.         return tres; /* Everything matches.  */
  277.  
  278.     } while (--attempts);
  279.  
  280.     return -1;
  281. }
  282.  
  283. /*
  284. * Convert Unix time to struct tm format.
  285. * Use Coordinated Universal Time (UTC) if available and if version 5 or newer;
  286. * use local time otherwise.
  287. */
  288.     static const struct tm *
  289. time2tm(unixtime)
  290.     time_t unixtime;
  291. {
  292.     const struct tm *tm;
  293.     return
  294.             VERSION(5)<=RCSversion && (tm = gmtime(&unixtime))
  295.         ?    tm
  296.         :    localtime(&unixtime);
  297. }
  298.  
  299. /*
  300. * Convert Unix time to RCS format.
  301. * For compatibility with older versions of RCS,
  302. * dates before AD 2000 are stored without the leading "19".
  303. */
  304.     void
  305. time2date(unixtime,date)
  306.     time_t unixtime;
  307.     char date[datesize];
  308. {
  309.     register const struct tm *tm = time2tm(unixtime);
  310.     VOID sprintf(date, DATEFORM,
  311.         tm->tm_year  +  (tm->tm_year<100 ? 0 : 1900),
  312.         tm->tm_mon+1, tm->tm_mday,
  313.         tm->tm_hour, tm->tm_min, tm->tm_sec
  314.     );
  315. }
  316.  
  317.  
  318.  
  319.     void
  320. str2date(source, target)
  321.     const char *source;
  322.     char target[datesize];
  323. /* Parse a free-format date in SOURCE, convert it
  324.  * into RCS internal format, and store the result into TARGET.
  325.  */
  326. {
  327.     int zone;
  328.     time_t unixtime;
  329.     struct tm parseddate;
  330.  
  331.     if (!partime(source, &parseddate, &zone))
  332.         faterror("can't parse date/time: %s", source);
  333.     if ((unixtime = maketime(&parseddate, zone))  ==  -1)
  334.         faterror("bad date/time: %s", source);
  335.     time2date(unixtime, target);
  336. }
  337.