Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / zotnet / tws / RCS / dtime.c,v
1 head    1.15;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
6
7
8 1.15
9 date    92.12.15.00.20.22;      author jromine; state Exp;
10 branches;
11 next    1.14;
12
13 1.14
14 date    92.05.19.21.24.56;      author jromine; state Exp;
15 branches;
16 next    1.13;
17
18 1.13
19 date    92.05.19.21.24.42;      author jromine; state Exp;
20 branches;
21 next    1.12;
22
23 1.12
24 date    92.05.15.17.56.29;      author jromine; state Exp;
25 branches;
26 next    1.11;
27
28 1.11
29 date    92.02.07.00.45.31;      author jromine; state Exp;
30 branches;
31 next    1.10;
32
33 1.10
34 date    92.02.07.00.27.17;      author jromine; state Exp;
35 branches;
36 next    1.9;
37
38 1.9
39 date    92.02.05.23.45.46;      author jromine; state Exp;
40 branches;
41 next    1.8;
42
43 1.8
44 date    92.02.05.22.39.47;      author jromine; state Exp;
45 branches;
46 next    1.7;
47
48 1.7
49 date    90.04.09.13.23.19;      author sources; state Exp;
50 branches;
51 next    1.6;
52
53 1.6
54 date    90.04.05.15.04.43;      author sources; state Exp;
55 branches;
56 next    1.5;
57
58 1.5
59 date    90.02.22.18.14.11;      author sources; state Exp;
60 branches;
61 next    1.4;
62
63 1.4
64 date    90.02.22.18.09.06;      author sources; state Exp;
65 branches;
66 next    1.3;
67
68 1.3
69 date    90.02.22.14.34.23;      author sources; state Exp;
70 branches;
71 next    1.2;
72
73 1.2
74 date    90.02.01.14.37.05;      author sources; state Exp;
75 branches;
76 next    1.1;
77
78 1.1
79 date    90.02.01.14.36.51;      author sources; state Exp;
80 branches;
81 next    ;
82
83
84 desc
85 @@
86
87
88 1.15
89 log
90 @endif sugar
91 @
92 text
93 @/* dtime.c - routines to do ``ARPA-style'' time structures */
94 #ifndef lint
95 static char ident[] = "@@(#)$Id: dtime.c,v 1.14 1992/05/19 21:24:56 jromine Exp jromine $";
96 #endif  /* lint */
97
98 /* LINTLIBRARY */
99
100
101 #include "tws.h"
102 #ifndef INETONLY
103 #include "../h/strings.h"
104 #else   /* INETONLY */
105 #include "strings.h"
106 #endif  /* INETONLY */
107 #include <stdio.h>
108 #include <sys/types.h>
109 #if     !defined(SYS5) && !defined(ZONEINFO)
110 #include <sys/timeb.h>
111 #endif  /* !defined(SYS5) && !defined(ZONEINFO) */
112 #ifdef  _AIX
113 #include <sys/time.h>
114 #include <time.h>
115 #else
116 #ifdef  BSD42
117 #include <sys/time.h>
118 #else   /* BSD42 */
119 #include <time.h>
120 #endif  /* BSD42 */
121 #endif
122
123 #ifdef  SYS5
124 extern int  daylight;
125 extern long timezone;
126 extern char *tzname[];
127 #endif  /* SYS5 */
128
129 /* \f */
130
131 #ifndef abs
132 #define abs(a)  (a >= 0 ? a : -a)
133 #endif
134
135 #define dysize(y)       \
136         (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
137
138 /* \f */
139
140 char *tw_moty[] = {
141     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
142     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL
143 };
144
145 char *tw_dotw[] = {
146     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
147 };
148
149 char *tw_ldotw[] = {
150     "Sunday", "Monday", "Tuesday", "Wednesday",
151     "Thursday", "Friday", "Saturday", NULL
152 };
153
154 /* \f */
155
156 static struct zone {
157     char   *std,
158            *dst;
159     int     shift;
160 }                   zones[] = {
161                         "GMT", "BST", 0,
162                         "EST", "EDT", -5,
163                         "CST", "CDT", -6,
164                         "MST", "MDT", -7,
165                         "PST", "PDT", -8,
166 #ifdef  notdef  /* RFC1123 specifies do not use military TZs */
167                         "A", NULL, -1,
168                         "B", NULL, -2,
169                         "C", NULL, -3,
170                         "D", NULL, -4,
171                         "E", NULL, -5,
172                         "F", NULL, -6,
173                         "G", NULL, -7,
174                         "H", NULL, -8,
175                         "I", NULL, -9,
176                         "K", NULL, -10,
177                         "L", NULL, -11,
178                         "M", NULL, -12,
179                         "N", NULL, 1,
180 #ifndef HUJI
181                         "O", NULL, 2,
182 #else   /* HUJI */
183                         "JST", "JDT", 2,
184 #endif  /* HUJI */
185                         "P", NULL, 3,
186                         "Q", NULL, 4,
187                         "R", NULL, 5,
188                         "S", NULL, 6,
189                         "T", NULL, 7,
190                         "U", NULL, 8,
191                         "V", NULL, 9,
192                         "W", NULL, 10,
193                         "X", NULL, 11,
194                         "Y", NULL, 12,
195 #endif  /* notdef */
196
197                         NULL
198 };
199
200 #define CENTURY 1900
201
202 long    time ();
203 struct tm *localtime ();
204
205 /* \f */
206
207 char *dtimenow () {
208     long    clock;
209
210     (void) time (&clock);
211     return dtime (&clock);
212 }
213
214
215 char   *dctime (tw)
216 register struct tws *tw;
217 {
218     static char buffer[25];
219
220     if (!tw)
221         return NULL;
222
223     (void) sprintf (buffer, "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
224             tw_dotw[tw -> tw_wday], tw_moty[tw -> tw_mon], tw -> tw_mday,
225             tw -> tw_hour, tw -> tw_min, tw -> tw_sec,
226             tw -> tw_year >= 100 ? tw -> tw_year : CENTURY + tw -> tw_year);
227
228     return buffer;
229 }
230
231 /* \f */
232
233 struct tws *dtwstime () {
234     long    clock;
235
236     (void) time (&clock);
237     return dlocaltime (&clock);
238 }
239
240
241 struct tws *dlocaltime (clock)
242 register long   *clock;
243 {
244     register struct tm *tm;
245 #if     !defined(SYS5) && !defined(ZONEINFO)
246     struct timeb    tb;
247 #endif  /* !defined(SYS5) && !defined(ZONEINFO) */
248     static struct tws   tw;
249
250     if (!clock)
251         return NULL;
252     tw.tw_flags = TW_NULL;
253
254     tm = localtime (clock);
255     tw.tw_sec = tm -> tm_sec;
256     tw.tw_min = tm -> tm_min;
257     tw.tw_hour = tm -> tm_hour;
258     tw.tw_mday = tm -> tm_mday;
259     tw.tw_mon = tm -> tm_mon;
260     tw.tw_year = tm -> tm_year + CENTURY;
261     tw.tw_wday = tm -> tm_wday;
262     tw.tw_yday = tm -> tm_yday;
263     if (tm -> tm_isdst)
264         tw.tw_flags |= TW_DST;
265 #if     defined(SYS5)
266     tzset ();
267     tw.tw_zone = -(timezone / 60);
268 #else   /* defined(SYS5) */
269 #if     defined(ZONEINFO)
270     tw.tw_zone = tm->tm_gmtoff / 60;
271     if (tm -> tm_isdst)                 /* if DST is in effect */
272         tw.tw_zone -= 60;               /* reset to normal offset */
273 #else   /* defined(ZONEINFO) */
274     ftime (&tb);
275     tw.tw_zone = -tb.timezone;
276 #endif  /* defined(ZONEINFO) */
277 #endif  /* defined(SYS5) */
278     tw.tw_flags &= ~TW_SDAY, tw.tw_flags |= TW_SEXP;
279     tw.tw_flags &= ~TW_SZONE, tw.tw_flags |= TW_SZEXP;
280     tw.tw_clock = *clock;
281
282     return (&tw);
283 }
284
285
286 struct tws *dgmtime (clock)
287 register long   *clock;
288 {
289     register struct tm *tm;
290     static struct tws   tw;
291
292     if (!clock)
293         return NULL;
294     tw.tw_flags = TW_NULL;
295
296     tm = gmtime (clock);
297     tw.tw_sec = tm -> tm_sec;
298     tw.tw_min = tm -> tm_min;
299     tw.tw_hour = tm -> tm_hour;
300     tw.tw_mday = tm -> tm_mday;
301     tw.tw_mon = tm -> tm_mon;
302     tw.tw_year = tm -> tm_year + CENTURY;
303     tw.tw_wday = tm -> tm_wday;
304     tw.tw_yday = tm -> tm_yday;
305     if (tm -> tm_isdst)
306         tw.tw_flags |= TW_DST;
307     tw.tw_zone = 0;
308     tw.tw_flags &= ~TW_SDAY, tw.tw_flags |= TW_SEXP;
309     tw.tw_flags &= ~TW_SZONE, tw.tw_flags |= TW_SZEXP;
310     tw.tw_clock = *clock;
311
312     return (&tw);
313 }
314
315 /* \f */
316
317 char   *dasctime (tw, flags)
318 register struct tws *tw;
319 int     flags;
320 {
321     char buffer[80];
322     static char result[80];
323
324     if (!tw)
325         return NULL;
326
327     /* Display timezone if known */
328     if ((tw->tw_flags & TW_SZONE) == TW_SZNIL)
329             result[0] = '\0';
330     else
331             (void) sprintf(result, " %s",
332                 dtimezone(tw -> tw_zone, tw->tw_flags | flags));
333     (void) sprintf(buffer, "%02d %s %0*d %02d:%02d:%02d%s",
334             tw->tw_mday, tw_moty[tw->tw_mon],
335             tw -> tw_year < 100 ? 2 : 4, tw -> tw_year,
336             tw->tw_hour, tw->tw_min, tw->tw_sec, result);
337
338     if ((tw -> tw_flags & TW_SDAY) == TW_SEXP)
339         (void) sprintf (result, "%s, %s", tw_dotw[tw -> tw_wday], buffer);
340     else
341         if ((tw -> tw_flags & TW_SDAY) == TW_SNIL)
342             (void) strcpy (result, buffer);
343         else
344             (void) sprintf (result, "%s (%s)", buffer, tw_dotw[tw -> tw_wday]);
345
346     return result;
347 }
348
349 /* \f */
350
351 char   *dtimezone (offset, flags)
352 register int     offset,
353                  flags;
354 {
355     register int    hours,
356                     mins;
357     register struct zone *z;
358     static char buffer[10];
359
360     if (offset < 0) {
361         mins = -((-offset) % 60);
362         hours = -((-offset) / 60);
363     }
364     else {
365         mins = offset % 60;
366         hours = offset / 60;
367     }
368
369     if (!(flags & TW_ZONE) && mins == 0)
370 #if     defined(SYS5) && defined(TZNAME)
371     {
372         tzset();
373         return ((flags & TW_DST) ? tzname[1] : tzname[0]);
374     }
375 #else
376         for (z = zones; z -> std; z++)
377             if (z -> shift == hours)
378                 return (z -> dst && (flags & TW_DST) ? z -> dst : z -> std);
379 #endif
380
381 #if     defined(DSTXXX)
382     if (flags & TW_DST)
383         hours += 1;
384 #endif  /* defined(DSTXXX) */
385     (void) sprintf (buffer, "%s%02d%02d",
386             offset < 0 ? "-" : "+", abs (hours), abs (mins));
387     return buffer;
388 }
389
390 /* \f */
391
392 void twscopy (tb, tw)
393 register struct tws *tb,
394                     *tw;
395 {
396 #ifdef  notdef
397     tb -> tw_sec = tw -> tw_sec;
398     tb -> tw_min = tw -> tw_min;
399     tb -> tw_hour = tw -> tw_hour;
400     tb -> tw_mday = tw -> tw_mday;
401     tb -> tw_mon = tw -> tw_mon;
402     tb -> tw_year = tw -> tw_year;
403     tb -> tw_wday = tw -> tw_wday;
404     tb -> tw_yday = tw -> tw_yday;
405     tb -> tw_zone = tw -> tw_zone;
406     tb -> tw_clock = tw -> tw_clock;
407     tb -> tw_flags = tw -> tw_flags;
408 #else   /* not notdef */
409     *tb = *tw;
410 #endif  /* not notdef */
411 }
412
413
414 int     twsort (tw1, tw2)
415 register struct tws *tw1,
416                     *tw2;
417 {
418     register long   c1,
419                     c2;
420
421     if (tw1 -> tw_clock == 0L)
422         (void) twclock (tw1);
423     if (tw2 -> tw_clock == 0L)
424         (void) twclock (tw2);
425
426     return ((c1 = tw1 -> tw_clock) > (c2 = tw2 -> tw_clock) ? 1
427             : c1 == c2 ? 0 : -1);
428 }
429
430 /* \f */
431
432 /* This routine is based on the gtime() routine written by Steven Shafer
433    (sas) at CMU.  It was forwarded to MTR by Jay Lepreau at Utah-CS.
434  */
435
436 static int  dmsize[] = {
437     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
438 };
439
440
441 long    twclock (tw)
442 register struct tws *tw;
443 {
444     register int    i,
445                     sec,
446                     min,
447                     hour,
448                     mday,
449                     mon,
450                     year;
451     register long   result;
452
453     if (tw -> tw_clock != 0L)
454         return tw -> tw_clock;
455
456     if ((sec = tw -> tw_sec) < 0 || sec > 59
457             || (min = tw -> tw_min) < 0 || min > 59
458             || (hour = tw -> tw_hour) < 0 || hour > 23
459             || (mday = tw -> tw_mday) < 1 || mday > 31
460             || (mon = tw -> tw_mon + 1) < 1 || mon > 12)
461         return (tw -> tw_clock = -1L);
462     year = tw -> tw_year;
463
464     result = 0L;
465     if (year < 100)
466         year += CENTURY;
467     for (i = 1970; i < year; i++)
468         result += dysize (i);
469     if (dysize (year) == 366 && mon >= 3)
470         result++;
471     while (--mon)
472         result += dmsize[mon - 1];
473     result += mday - 1;
474     result = 24 * result + hour;
475     result = 60 * result + min;
476     result = 60 * result + sec;
477     result -= 60 * tw -> tw_zone;
478     if (tw -> tw_flags & TW_DST)
479         result -= 60 * 60;
480
481     return (tw -> tw_clock = result);
482 }
483
484 /* \f */
485
486 /*
487  *    Simple calculation of day of the week.  Algorithm used is Zeller's
488  *    congruence.  Currently, we assume if tw -> tw_year < 100
489  *    then the century is CENTURY.
490  */
491
492 set_dotw (tw)
493 register struct tws *tw;
494 {
495     register int    month,
496                     day,
497                     year,
498                     century;
499
500     month = tw -> tw_mon - 1;
501     day = tw -> tw_mday;
502     year = tw -> tw_year % 100;
503     century = tw -> tw_year >= 100 ? tw -> tw_year / 100 : (CENTURY / 100);
504
505     if (month <= 0) {
506         month += 12;
507         if (--year < 0) {
508             year += 100;
509             century--;
510         }
511     }
512
513     tw -> tw_wday =
514         ((26 * month - 2) / 10 + day + year + year / 4
515             - 3 * century / 4 + 1) % 7;
516
517     tw -> tw_flags &= ~TW_SDAY, tw -> tw_flags |= TW_SIMP;
518 }
519 @
520
521
522 1.14
523 log
524 @AIX
525 @
526 text
527 @d3 2
528 a4 2
529 static char ident[] = "@@(#)$Id: dtime.c,v 1.13 1992/05/19 21:24:42 jromine Exp jromine $";
530 #endif  lint
531 d12 1
532 a12 1
533 #else   INETONLY
534 d14 1
535 a14 1
536 #endif  INETONLY
537 d26 1
538 a26 1
539 #else   BSD42
540 d28 1
541 a28 1
542 #endif  BSD42
543 d35 1
544 a35 1
545 #endif  SYS5
546 d90 1
547 a90 1
548 #else   HUJI
549 d92 1
550 a92 1
551 #endif  HUJI
552 d103 1
553 a103 1
554 #endif  notdef
555 d292 1
556 a292 1
557 #endif  defined(DSTXXX)
558 d316 1
559 a316 1
560 #else   not notdef
561 d318 1
562 a318 1
563 #endif  not notdef
564 @
565
566
567 1.13
568 log
569 @AIX
570 @
571 text
572 @d3 1
573 a3 1
574 static char ident[] = "@@(#)$Id: dtime.c,v 1.12 1992/05/15 17:56:29 jromine Exp jromine $";
575 d39 1
576 d41 1
577 @
578
579
580 1.12
581 log
582 @fix problem with ZONEINFO on Suns.
583 @
584 text
585 @d3 1
586 a3 1
587 static char ident[] = "@@(#)$Id: dtime.c,v 1.11 1992/02/07 00:45:31 jromine Exp jromine $";
588 d20 4
589 d29 1
590 @
591
592
593 1.11
594 log
595 @4 digit years
596 @
597 text
598 @d3 1
599 a3 1
600 static char ident[] = "@@(#)$Id: dtime.c,v 1.10 1992/02/07 00:27:17 jromine Exp jromine $";
601 d172 2
602 d282 1
603 a282 1
604 #if     defined(DSTXXX) && !defined(ZONEINFO)
605 d285 1
606 a285 1
607 #endif  defined(DSTXXX) && !defined(ZONEINFO)
608 @
609
610
611 1.10
612 log
613 @fixes
614 @
615 text
616 @d3 1
617 a3 1
618 static char ident[] = "@@(#)$Id: dtime.c,v 1.9 1992/02/05 23:45:46 jromine Exp jromine $";
619 d161 1
620 a161 1
621     tw.tw_year = tm -> tm_year;
622 d201 1
623 a201 1
624     tw.tw_year = tm -> tm_year;
625 @
626
627
628 1.9
629 log
630 @fix 4 digit dates
631 @
632 text
633 @d3 1
634 a3 1
635 static char ident[] = "@@(#)$Id: dtime.c,v 1.8 1992/02/05 22:39:47 jromine Exp jromine $";
636 d232 1
637 a232 1
638     (void) sprintf(buffer, "%02d %s %04d %02d:%02d:%02d%s",
639 d234 1
640 a234 1
641             tw -> tw_year >= 100 ? tw -> tw_year : CENTURY + tw -> tw_year,
642 d364 2
643 a365 1
644     year += CENTURY;
645 @
646
647
648 1.8
649 log
650 @start using 4 digit dates
651 @
652 text
653 @d3 1
654 a3 1
655 static char ident[] = "@@(#)$Id: dtime.c,v 1.7 1990/04/09 13:23:19 sources Exp jromine $";
656 d233 2
657 a234 1
658             tw->tw_mday, tw_moty[tw->tw_mon], tw->tw_year,
659 @
660
661
662 1.7
663 log
664 @*** empty log message ***
665 @
666 text
667 @d3 1
668 a3 1
669 static char ident[] = "@@(#)$Id: dtime.c,v 1.6 90/04/05 15:04:43 sources Exp Locker: sources $";
670 d101 1
671 a101 1
672 #define CENTURY 19
673 d127 1
674 a127 1
675             tw -> tw_year >= 100 ? tw -> tw_year : 1900 + tw -> tw_year);
676 d232 1
677 a232 1
678     (void) sprintf(buffer, "%02d %s %02d %02d:%02d:%02d%s",
679 d363 1
680 a363 1
681     year += 1900;
682 d400 1
683 a400 1
684     century = tw -> tw_year >= 100 ? tw -> tw_year / 100 : CENTURY;
685 @
686
687
688 1.6
689 log
690 @add ID
691 @
692 text
693 @d3 1
694 a3 1
695 static char ident[] = "@@(#)$Id:$";
696 d17 1
697 a17 1
698 #ifndef  SYS5
699 d19 4
700 a22 2
701 #endif   not SYS5
702 #ifndef BSD42
703 a23 2
704 #else   BSD42
705 #include <sys/time.h>
706 d146 1
707 a146 1
708 #ifndef SYS5
709 d148 1
710 a148 1
711 #endif  not SYS5
712 d166 7
713 a172 1
714 #ifndef  SYS5
715 d175 2
716 a176 4
717 #else   SYS5
718     tzset ();
719     tw.tw_zone = -(timezone / 60);
720 #endif  SYS5
721 d279 1
722 a279 1
723 #ifdef  DSTXXX
724 d282 1
725 a282 1
726 #endif  DSTXXX
727 @
728
729
730 1.5
731 log
732 @ignore military timezones per RFC1123
733 @
734 text
735 @d2 3
736 @
737
738
739 1.4
740 log
741 @Fixes from Van Jacobson
742 @
743 text
744 @d64 1
745 d93 1
746 @
747
748
749 1.3
750 log
751 @SYS5 stuff?
752 @
753 text
754 @d169 1
755 d199 1
756 d211 2
757 a212 2
758     static char buffer[80],
759                 result[80];
760 d217 9
761 a225 4
762     (void) sprintf (buffer, "%02d %s %02d %02d:%02d:%02d %s",
763             tw -> tw_mday, tw_moty[tw -> tw_mon], tw -> tw_year,
764             tw -> tw_hour, tw -> tw_min, tw -> tw_sec,
765             dtimezone (tw -> tw_zone, tw -> tw_flags | flags));
766 @
767
768
769 1.2
770 log
771 @add MDT
772 @
773 text
774 @d252 6
775 d261 1
776 @
777
778
779 1.1
780 log
781 @Initial revision
782 @
783 text
784 @d62 1
785 a62 1
786                         "MST", NULL, -7,
787 @