home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / source / s4 / ctime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1975-05-13  |  4.3 KB  |  250 lines

  1. #
  2. /*
  3.  * This routine converts time as follows.
  4.  * The epoch is 0000 Jan 1 1970 GMT.
  5.  * The argument time is in seconds since then.
  6.  * The localtime(t) entry returns a pointer to an array
  7.  * containing
  8.  *  seconds (0-59)
  9.  *  minutes (0-59)
  10.  *  hours (0-23)
  11.  *  day of month (1-31)
  12.  *  month (0-11)
  13.  *  year-1970
  14.  *  weekday (0-6, Sun is 0)
  15.  *  day of the year
  16.  *  daylight savings flag
  17.  *
  18.  * The routine corrects for daylight saving
  19.  * time and will work in any time zone provided
  20.  * "timezone" is adjusted to the difference between
  21.  * Greenwich and local standard time (measured in seconds).
  22.  * In places like Michigan "daylight" must
  23.  * be initialized to 0 to prevent the conversion
  24.  * to daylight time.
  25.  * There is a table which accounts for the peculiarities
  26.  * undergone by daylight time in 1974-1975.
  27.  *
  28.  * The routine does not work
  29.  * in Saudi Arabia which runs on Solar time.
  30.  *
  31.  * asctime(tvec))
  32.  * where tvec is produced by localtime
  33.  * returns a ptr to a character string
  34.  * that has the ascii time in the form
  35.  *    Thu Jan 01 00:00:00 1970n0\\
  36.  *    01234567890123456789012345
  37.  *    0      1        2
  38.  *
  39.  * ctime(t) just calls localtime, then asctime.
  40.  */
  41. char    cbuf[26];
  42. int    dmsize[12]
  43. {
  44.     31,
  45.     28,
  46.     31,
  47.     30,
  48.     31,
  49.     30,
  50.     31,
  51.     31,
  52.     30,
  53.     31,
  54.     30,
  55.     31
  56. };
  57.  
  58. int timezone    5*60*60;
  59. int tzname[]
  60. {
  61.     "EST",
  62.     "EDT",
  63. };
  64. int    daylight 1;    /* Allow daylight conversion */
  65. /*
  66.  * The following table is used for 1974 and 1975 and
  67.  * gives the day number of the first day after the Sunday of the
  68.  * change.
  69.  */
  70. struct {
  71.     int    daylb;
  72.     int    dayle;
  73. } daytab[] {
  74.     5,    333,    /* 1974: Jan 6 - last Sun. in Nov */
  75.     58,    303,    /* 1975: Last Sun. in Feb - last Sun in Oct */
  76. };
  77.  
  78. #define    SEC    0
  79. #define    MIN    1
  80. #define    HOUR    2
  81. #define    MDAY    3
  82. #define    MON    4
  83. #define    YEAR    5
  84. #define    WDAY    6
  85. #define    YDAY    7
  86. #define    ISDAY    8
  87.  
  88. ctime(at)
  89. int *at;
  90. {
  91.     return(asctime(localtime(at)));
  92. }
  93.  
  94. localtime(tim)
  95. int tim[];
  96. {
  97.     register int *t, *ct, dayno;
  98.     int daylbegin, daylend;
  99.     int copyt[2];
  100.  
  101.     t = copyt;
  102.     t[0] = tim[0];
  103.     t[1] = tim[1];
  104.     dpadd(t, -timezone);
  105.     ct = gmtime(t);
  106.     dayno = ct[YDAY];
  107.     daylbegin = 119;    /* last Sun in Apr */
  108.     daylend = 303;        /* Last Sun in Oct */
  109.     if (ct[YEAR]==74 || ct[YEAR]==75) {
  110.         daylbegin = daytab[ct[YEAR]-74].daylb;
  111.         daylend = daytab[ct[YEAR]-74].dayle;
  112.     }
  113.     daylbegin = sunday(ct, daylbegin);
  114.     daylend = sunday(ct, daylend);
  115.     if (daylight &&
  116.         (dayno>daylbegin || (dayno==daylbegin && ct[HOUR]>=2)) &&
  117.         (dayno<daylend || (dayno==daylend && ct[HOUR]<1))) {
  118.         dpadd(t, 1*60*60);
  119.         ct = gmtime(t);
  120.         ct[ISDAY]++;
  121.     }
  122.     return(ct);
  123. }
  124.  
  125. /*
  126.  * The argument is a 0-origin day number.
  127.  * The value is the day number of the first
  128.  * Sunday on or after the day.
  129.  */
  130. sunday(at, ad)
  131. int *at;
  132. {
  133.     register int *t, d;
  134.  
  135.     t = at;
  136.     d = ad;
  137.     if (d >= 58)
  138.         d =+ dysize(t[YEAR]) - 365;
  139.     return(d - (d - t[YDAY] + t[WDAY] + 700) % 7);
  140. }
  141.  
  142. gmtime(tim)
  143. int tim[];
  144. {
  145.     register int d0, d1;
  146.     register *tp;
  147.     static xtime[9];
  148.     extern int ldivr;
  149.  
  150.     /*
  151.      * break initial number into
  152.      * multiples of 8 hours.
  153.      * (28800 = 60*60*8)
  154.      */
  155.  
  156.     d0 = ldiv(tim[0], tim[1], 28800);
  157.     d1 = ldivr;
  158.     tp = &xtime[0];
  159.  
  160.     /*
  161.      * generate hours:minutes:seconds
  162.      */
  163.  
  164.     *tp++ = d1%60;
  165.     d1 =/ 60;
  166.     *tp++ = d1%60;
  167.     d1 =/ 60;
  168.     d1 =+ (d0%3)*8;
  169.     d0 =/ 3;
  170.     *tp++ = d1;
  171.  
  172.     /*
  173.      * d0 is the day number.
  174.      * generate day of the week.
  175.      */
  176.  
  177.     xtime[WDAY] = (d0+4)%7;
  178.  
  179.     /*
  180.      * year number
  181.      */
  182.     for(d1=70; d0 >= dysize(d1); d1++)
  183.         d0 =- dysize(d1);
  184.     xtime[YEAR] = d1;
  185.     xtime[YDAY] = d0;
  186.  
  187.     /*
  188.      * generate month
  189.      */
  190.  
  191.     if (dysize(d1)==366)
  192.         dmsize[1] = 29;
  193.     for(d1=0; d0 >= dmsize[d1]; d1++)
  194.         d0 =- dmsize[d1];
  195.     dmsize[1] = 28;
  196.     *tp++ = d0+1;
  197.     *tp++ = d1;
  198.     xtime[ISDAY] = 0;
  199.     return(xtime);
  200. }
  201.  
  202. asctime(t)
  203. int *t;
  204. {
  205.     register char *cp, *ncp;
  206.     register int *tp;
  207.  
  208.     cp = cbuf;
  209.     for (ncp = "Day Mon 00 00:00:00 1900\n"; *cp++ = *ncp++;);
  210.     ncp = &"SunMonTueWedThuFriSat"[3*t[6]];
  211.     cp = cbuf;
  212.     *cp++ = *ncp++;
  213.     *cp++ = *ncp++;
  214.     *cp++ = *ncp++;
  215.     cp++;
  216.     tp = &t[4];
  217.     ncp = &"JanFebMarAprMayJunJulAugSepOctNovDec"[(*tp)*3];
  218.     *cp++ = *ncp++;
  219.     *cp++ = *ncp++;
  220.     *cp++ = *ncp++;
  221.     cp = ct_numb(cp, *--tp);
  222.     cp = ct_numb(cp, *--tp+100);
  223.     cp = ct_numb(cp, *--tp+100);
  224.     cp = ct_numb(cp, *--tp+100);
  225.     cp =+ 2;
  226.     cp = ct_numb(cp, t[YEAR]);
  227.     return(cbuf);
  228. }
  229.  
  230. dysize(y)
  231. {
  232.     if((y%4) == 0)
  233.         return(366);
  234.     return(365);
  235. }
  236.  
  237. ct_numb(acp, n)
  238. {
  239.     register char *cp;
  240.  
  241.     cp = acp;
  242.     cp++;
  243.     if (n>=10)
  244.         *cp++ = (n/10)%10 + '0';
  245.     else
  246.         *cp++ = ' ';
  247.     *cp++ = n%10 + '0';
  248.     return(cp);
  249. }
  250.