home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / rcs4.lha / maketime.c < prev    next >
Text File  |  1993-03-03  |  7KB  |  246 lines

  1. /*
  2.  * MAKETIME        derive 32-bit time value from TM structure.
  3.  *
  4.  * Usage:
  5.  *    long t,maketime();
  6.  *    struct tm *tp;    Pointer to TM structure from <time.h>
  7.  *            NOTE: this must be extended version!!!
  8.  *    t = maketime(tp);
  9.  *
  10.  * Returns:
  11.  *    0 if failure; parameter out of range or nonsensical.
  12.  *    else long time-value.
  13.  * Notes:
  14.  *    This code is quasi-public; it may be used freely in like software.
  15.  *    It is not to be sold, nor used in licensed software without
  16.  *    permission of the author.
  17.  *    For everyone's benefit, please report bugs and improvements!
  18.  *     Copyright 1981 by Ken Harrenstien, SRI International.
  19.  *    (ARPANET: KLH @ SRI)
  20.  */
  21. #ifndef lint
  22. static char rcsid[]= "$Id: maketime.c,v 1.6 87/12/18 17:05:58 narten Exp $";
  23. #endif
  24. /* $Log:    maketime.c,v $
  25.  * Revision 1.6  87/12/18  17:05:58  narten
  26.  * include rcsparam.h
  27.  * 
  28.  * Revision 1.5  87/12/18  11:35:51  narten
  29.  * maketime.c: fixed USG code - you have tgo call "tzset" in order to have
  30.  * "timezone" set. ("localtime" calls it, but it's probably better not to 
  31.  * count on "localtime" having been called.)
  32.  * 
  33.  * Revision 1.4  87/10/18  10:26:57  narten
  34.  * Updating version numbers. Changes relative to 1.0 are actually 
  35.  * relative to 1.2
  36.  * 
  37.  * Revision 1.3  87/09/24  13:58:45  narten
  38.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  39.  * warnings)
  40.  * 
  41.  * Revision 1.2  87/03/27  14:21:48  jenkins
  42.  * Port to suns
  43.  * 
  44.  * Revision 1.1  84/01/23  14:50:04  kcs
  45.  * Initial revision
  46.  * 
  47.  * Revision 1.2  83/12/05  10:12:56  wft
  48.  * added cond. compilation for USG Unix; long timezone;
  49.  * 
  50.  * Revision 1.1  82/05/06  11:38:00  wft
  51.  * Initial revision
  52.  * 
  53.  */
  54.  
  55.  
  56. #include "rcsbase.h"
  57. #include "time.h"
  58.  
  59. int daytb[] = {   /* # days in year thus far, indexed by month (0-12!!) */
  60.     0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
  61. };
  62.  
  63. struct tm *localtime();
  64. long    time();
  65.  
  66. long maketime(atm)
  67. struct tm *atm;
  68. {    register struct tm *tp;
  69.     register int i;
  70.     int year, yday, mon, day, hour, min, sec, zone, dst, leap;
  71.     long tres, curtim;
  72.  
  73.     VOID time(&curtim);
  74.     tp = localtime(&curtim);        /* Get breakdowns of current time */
  75.     year = tp->tm_year;        /* Use to set up defaults */
  76.     mon = tp->tm_mon;
  77.     day = tp->tm_mday;
  78.  
  79.  
  80. #ifdef DEBUG
  81. printf("first YMD: %d %d %d, T=%ld\n",year,mon,day,tres);
  82. #endif DEBUG
  83.     tp = atm;
  84.  
  85.     /* First must find date, using specified year, month, day.
  86.      * If one of these is unspecified, it defaults either to the
  87.      * current date (if no more global spec was given) or to the
  88.      * zero-value for that spec (i.e. a more global spec was seen).
  89.      * Start with year... note 32 bits can only handle 135 years.
  90.      */
  91.     if(tp->tm_year != TMNULL)
  92.       {    if((year = tp->tm_year) >= 1900)    /* Allow full yr # */
  93.               year -= 1900;            /* by making kosher */
  94.         mon = 0;        /* Since year was given, default */
  95.         day = 1;        /* for remaining specs is zero */
  96.       }
  97.     if(year < 70 || 70+134 < year )    /* Check range */
  98.         return(0);        /* ERR: year out of range */
  99.     leap = year&03 ? 0 : 1;        /* See if leap year */
  100.     year -= 70;            /* UNIX time starts at 1970 */
  101.  
  102.     /*
  103.      * Find day of year.
  104.      * YDAY is used only if it exists and either the month or day-of-month
  105.      * is missing.
  106.      */
  107.     if (tp->tm_yday != TMNULL
  108.      && (tp->tm_mon == TMNULL || tp->tm_mday == TMNULL))
  109.         yday = tp->tm_yday;
  110.     else
  111.       {    if(tp->tm_mon  != TMNULL)
  112.           {    mon = tp->tm_mon;    /* Month was specified */
  113.             day = 1;        /* so set remaining default */
  114.           }
  115.         if(mon < 0 || 11 < mon) return(0);    /* ERR: bad month */
  116.         if(tp->tm_mday != TMNULL) day = tp->tm_mday;
  117.         if(day < 1
  118.          || (((daytb[mon+1]-daytb[mon]) < day)
  119.             && (day!=29 || mon!=1 || !leap) ))
  120.                 return(0);        /* ERR: bad day */
  121.         yday = daytb[mon]    /* Add # of days in months so far */
  122.           + ((leap        /* Leap year, and past Feb?  If */
  123.               && mon>1)? 1:0)    /* so, add leap day for this year */
  124.           + day-1;        /* And finally add # days this mon */
  125.  
  126.                 if (tp->tm_yday != TMNULL       /* Confirm that YDAY correct */
  127.                  && tp->tm_yday != yday) return(0);     /* ERR: conflict */
  128.       }
  129.     if(yday < 0 || (leap?366:365) <= yday)
  130.         return(0);        /* ERR: bad YDAY or maketime bug */
  131.  
  132.     tres = year*365            /* Get # days of years so far */
  133.         + ((year+1)>>2)        /* plus # of leap days since 1970 */
  134.         + yday;            /* and finally add # days this year */
  135.  
  136.         if((i = tp->tm_wday) != TMNULL) /* Check WDAY if present */
  137.                 if(i < 0 || 6 < i       /* Ensure within range */
  138.                   || i != (tres+4)%7)   /* Matches? Jan 1,1970 was Thu = 4 */
  139.                         return(0);      /* ERR: bad WDAY */
  140.  
  141. #ifdef DEBUG
  142. printf("YMD: %d %d %d, T=%ld\n",year,mon,day,tres);
  143. #endif DEBUG
  144.     /*
  145.      * Now determine time.  If not given, default to zeros
  146.      * (since time is always the least global spec)
  147.      */
  148.     tres *= 86400L;            /* Get # seconds (24*60*60) */
  149.     hour = min = sec = 0;
  150.     if(tp->tm_hour != TMNULL) hour = tp->tm_hour;
  151.     if(tp->tm_min  != TMNULL) min  = tp->tm_min;
  152.     if(tp->tm_sec  != TMNULL) sec  = tp->tm_sec;
  153.     if( min < 0 || 60 <= min
  154.      || sec < 0 || 60 <= sec) return(0);    /* ERR: MS out of range */
  155.     if(hour < 0 || 24 <= hour)
  156.         if(hour != 24 || (min+sec) !=0)    /* Allow 24:00 */
  157.             return(0);        /* ERR: H out of range */
  158.  
  159.     /* confirm AM/PM if there */
  160.     switch(tp->tm_ampm)
  161.       {    case 0: case TMNULL:    /* Ignore these values */
  162.             break;
  163.         case 1:            /* AM */
  164.         case 2:            /* PM */
  165.             if(hour > 12) return(0);  /* ERR: hrs 13-23 bad */
  166.             if(hour ==12) hour = 0;    /* Modulo 12 */
  167.             if(tp->tm_ampm == 2)    /* If PM, then */
  168.                 hour += 12;    /*   get 24-hour time */
  169.             break;
  170.         default: return(0);    /* ERR: illegal TM_AMPM value */
  171.       }
  172.  
  173.     tres += sec + 60L*(min + 60L*hour);    /* Add in # secs of time */
  174.  
  175. #ifdef DEBUG
  176. printf("HMS: %d %d %d T=%ld\n",hour,min,sec,tres);
  177. #endif DEBUG
  178.     /*
  179.      * We now have the GMT date/time and must make final
  180.      * adjustment for the specified time zone.  If none is specified,
  181.      * the local time-zone is assumed.
  182.      */
  183.     if((zone = tp->tm_zon) == TMNULL    /* If unspecified */
  184.      || (zone == 1))            /* or local-zone requested */
  185.         zone = localzone();        /* then set to local zone */
  186.     if(zone < 0 || 24*60 <= zone)
  187.         return(0);            /* ERR: zone out of range */
  188.  
  189.     /* See if must apply Daylight Saving Time shift.
  190.      * Note that if DST is specified, validity is not checked.
  191.      */
  192.     if((dst = tp->tm_isdst) == TMNULL)    /* Must we figure it out? */
  193.       {    curtim = tres +localzone()*60L;    /* Yuck.  Get equiv local */
  194.         dst = localtime(&curtim)->tm_isdst;     /* time, and ask. */
  195.       }
  196.     tres += zone*60L -(dst?3600:0);    /* Add in # seconds of zone adj */
  197.  
  198.     return(tres);
  199. }
  200.  
  201.  
  202. /* LOCALZONE        return local timezone in # mins west of GMT
  203.  *
  204.  */
  205. #ifdef OSK
  206. localzone()
  207. {
  208. return 0;
  209. }
  210. #else
  211.  
  212. #if defined(V6)
  213. extern long timezone;
  214. #else
  215. #ifdef USG
  216. extern long timezone;
  217. #else /* V7 */
  218. #include <sys/types.h>
  219. #include <sys/timeb.h>
  220. #endif USG
  221. #endif V6
  222.  
  223. int _lclzon = -1;
  224. localzone()
  225. {
  226. #ifdef V6
  227.     return(_lclzon >= 0 ? _lclzon : (_lclzon = timezone/60L));
  228. #else
  229. #ifdef USG
  230.     tzset();
  231.     return(_lclzon >= 0 ? _lclzon : (_lclzon = timezone/60L));
  232. #else /* V7 */
  233.     struct timeb tb;
  234.  
  235.     if(_lclzon < 0)
  236.       {    ftime(&tb);
  237.         _lclzon = tb.timezone;
  238.       }
  239.     return(_lclzon);
  240.  
  241. #endif USG
  242. #endif V6
  243. }
  244. #endif OSK
  245.  
  246.