8de4b69cf7e231bb1ee1a12e99bf10a3c4ebde63
[mmh] / sbr / dtime.c
1
2 /*
3  * dtime.c -- time/date routines
4  *
5  * $Id$
6  */
7
8 #include <h/mh.h>   /* for snprintf() */
9 #include <h/nmh.h>
10 #include <h/tws.h>
11
12 #if !defined(HAVE_TM_GMTOFF) && !defined(HAVE_TZSET)
13 # include <sys/timeb.h>
14 #endif
15
16 #ifdef TIME_WITH_SYS_TIME
17 # include <sys/time.h>
18 # include <time.h>
19 #else
20 # ifdef HAVE_SYS_TIME_H
21 # include <sys/time.h>
22 # else
23 #  include <time.h>
24 # endif
25 #endif
26
27 #if !defined(HAVE_TM_GMTOFF) && defined(HAVE_TZSET)
28 extern int daylight;
29 extern long timezone;
30 extern char *tzname[];
31 #endif
32
33 #ifndef abs
34 # define abs(a) (a >= 0 ? a : -a)
35 #endif
36
37 /*
38  * The number of days in the year, accounting for leap years
39  */
40 #define dysize(y)       \
41         (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
42
43 char *tw_moty[] = {
44     "Jan", "Feb", "Mar", "Apr",
45     "May", "Jun", "Jul", "Aug",
46     "Sep", "Oct", "Nov", "Dec",
47     NULL
48 };
49
50 char *tw_dotw[] = {
51     "Sun", "Mon", "Tue",
52     "Wed", "Thu", "Fri",
53     "Sat", NULL
54 };
55
56 char *tw_ldotw[] = {
57     "Sunday",    "Monday",   "Tuesday",
58     "Wednesday", "Thursday", "Friday",
59     "Saturday",  NULL
60 };
61
62 struct zone {
63     char *std;
64     char *dst;
65     int shift;
66 };
67
68 static struct zone zones[] = {
69     { "GMT", "BST", 0 },
70     { "EST", "EDT", -5 },
71     { "CST", "CDT", -6 },
72     { "MST", "MDT", -7 },
73     { "PST", "PDT", -8 },
74 #if 0
75 /* RFC1123 specifies do not use military TZs */
76     { "A", NULL, -1 },
77     { "B", NULL, -2 },
78     { "C", NULL, -3 },
79     { "D", NULL, -4 },
80     { "E", NULL, -5 },
81     { "F", NULL, -6 },
82     { "G", NULL, -7 },
83     { "H", NULL, -8 },
84     { "I", NULL, -9 },
85     { "K", NULL, -10 },
86     { "L", NULL, -11 },
87     { "M", NULL, -12 },
88     { "N", NULL, 1 },
89 #ifndef HUJI
90     { "O", NULL, 2 },
91 #else
92     { "JST", "JDT", 2 },
93 #endif
94     { "P", NULL, 3 },
95     { "Q", NULL, 4 },
96     { "R", NULL, 5 },
97     { "S", NULL, 6 },
98     { "T", NULL, 7 },
99     { "U", NULL, 8 },
100     { "V", NULL, 9 },
101     { "W", NULL, 10 },
102     { "X", NULL, 11 },
103     { "Y", NULL, 12 },
104 #endif
105     { NULL, NULL, 0 }
106 };
107
108 static int dmsize[] = {
109     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
110 };
111
112
113 /*
114  * Get current time (adjusted for local time
115  * zone and daylight savings time) expressed
116  * as nmh "broken-down" time structure.
117  */
118
119 struct tws *
120 dlocaltimenow (void)
121 {
122     time_t clock;
123
124     time (&clock);
125     return dlocaltime (&clock);
126 }
127
128
129 /*
130  * Take clock value and return pointer to nmh time structure
131  * containing "broken-down" time.  The time is adjusted for
132  * local time zone and daylight savings time.
133  */
134
135 struct tws *
136 dlocaltime (time_t *clock)
137 {
138     static struct tws tw;
139     struct tm *tm;
140
141 #if !defined(HAVE_TM_GMTOFF) && !defined(HAVE_TZSET)
142     struct timeb tb;
143 #endif
144
145     if (!clock)
146         return NULL;
147
148     tm = localtime (clock);
149
150     tw.tw_sec  = tm->tm_sec;
151     tw.tw_min  = tm->tm_min;
152     tw.tw_hour = tm->tm_hour;
153     tw.tw_mday = tm->tm_mday;
154     tw.tw_mon  = tm->tm_mon;
155
156     /*
157      * tm_year is always "year - 1900".
158      * So we correct for this.
159      */
160     tw.tw_year = tm->tm_year + 1900;
161     tw.tw_wday = tm->tm_wday;
162     tw.tw_yday = tm->tm_yday;
163
164     tw.tw_flags = TW_NULL;
165     if (tm->tm_isdst)
166         tw.tw_flags |= TW_DST;
167
168 #ifdef HAVE_TM_GMTOFF
169     tw.tw_zone = tm->tm_gmtoff / 60;
170     if (tm->tm_isdst)                   /* if DST is in effect */
171         tw.tw_zone -= 60;               /* reset to normal offset */
172 #else
173 # ifdef HAVE_TZSET
174     tzset();
175     tw.tw_zone = -(timezone / 60);
176 # else
177     ftime (&tb);
178     tw.tw_zone = -tb.timezone;
179 # endif
180 #endif
181
182     tw.tw_flags &= ~TW_SDAY;
183     tw.tw_flags |= TW_SEXP;
184     tw.tw_flags &= ~TW_SZONE;
185     tw.tw_flags |= TW_SZEXP;
186
187     tw.tw_clock = *clock;
188
189     return (&tw);
190 }
191
192
193 /*
194  * Take clock value and return pointer to nmh time
195  * structure containing "broken-down" time.  Time is
196  * expressed in UTC (Coordinated Universal Time).
197  */
198
199 struct tws *
200 dgmtime (time_t *clock)
201 {
202     static struct tws tw;
203     struct tm *tm;
204
205     if (!clock)
206         return NULL;
207
208     tm = gmtime (clock);
209
210     tw.tw_sec  = tm->tm_sec;
211     tw.tw_min  = tm->tm_min;
212     tw.tw_hour = tm->tm_hour;
213     tw.tw_mday = tm->tm_mday;
214     tw.tw_mon  = tm->tm_mon;
215
216     /*
217      * tm_year is always "year - 1900"
218      * So we correct for this.
219      */
220     tw.tw_year = tm->tm_year + 1900;
221     tw.tw_wday = tm->tm_wday;
222     tw.tw_yday = tm->tm_yday;
223
224     tw.tw_flags = TW_NULL;
225     if (tm->tm_isdst)
226         tw.tw_flags |= TW_DST;
227
228     tw.tw_zone = 0;
229
230     tw.tw_flags &= ~TW_SDAY;
231     tw.tw_flags |= TW_SEXP;
232     tw.tw_flags &= ~TW_SZONE;
233     tw.tw_flags |= TW_SZEXP;
234
235     tw.tw_clock = *clock;
236
237     return (&tw);
238 }
239
240
241 /*
242  * Using a nmh "broken-down" time structure,
243  * produce a 26-byte date/time string, such as
244  *
245  *      Tue Jan 14 17:49:03 1992\n\0
246  */
247
248 char *
249 dctime (struct tws *tw)
250 {
251     static char buffer[26];
252
253     if (!tw)
254         return NULL;
255
256     snprintf (buffer, sizeof(buffer), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
257             tw_dotw[tw->tw_wday], tw_moty[tw->tw_mon], tw->tw_mday,
258             tw->tw_hour, tw->tw_min, tw->tw_sec,
259             tw->tw_year < 100 ? tw->tw_year + 1900 : tw->tw_year);
260
261     return buffer;
262 }
263
264
265 /*
266  * Produce a date/time string of the form
267  *
268  *      Mon, 16 Jun 1992 15:30:48 -700 (or)
269  *      Mon, 16 Jun 1992 15:30:48 EDT
270  *
271  * for the current time, as specified by rfc822.
272  * The first form is required by rfc1123.
273  */
274
275 char *
276 dtimenow (int alpha_timezone)
277 {
278     time_t clock;
279
280     time (&clock);
281     return dtime (&clock, alpha_timezone);
282 }
283
284
285 /*
286  * Using a local calendar time value, produce
287  * a date/time string of the form
288  *
289  *      Mon, 16 Jun 1992 15:30:48 -700  (or)
290  *      Mon, 16 Jun 1992 15:30:48 EDT
291  *
292  * as specified by rfc822.  The first form is required
293  * by rfc1123 for outgoing messages.
294  */
295
296 char *
297 dtime (time_t *clock, int alpha_timezone)
298 {
299     if (alpha_timezone)
300         /* use alpha-numeric timezones */
301         return dasctime (dlocaltime (clock), TW_NULL);
302     else
303         /* use numeric timezones */
304         return dasctime (dlocaltime (clock), TW_ZONE);
305 }
306
307
308 /*
309  * Using a nmh "broken-down" time structure, produce
310  * a date/time string of the form
311  *
312  *      Mon, 16 Jun 1992 15:30:48 -0700
313  *
314  * as specified by rfc822 and rfc1123.
315  */
316
317 char *
318 dasctime (struct tws *tw, int flags)
319 {
320     char buffer[80];
321     static char result[80];
322
323     if (!tw)
324         return NULL;
325
326     /* Display timezone if known */
327     if ((tw->tw_flags & TW_SZONE) == TW_SZNIL)
328         result[0] = '\0';
329     else
330         snprintf(result, sizeof(result), " %s", dtimezone(tw->tw_zone, tw->tw_flags | flags));
331
332     snprintf(buffer, sizeof(buffer), "%02d %s %0*d %02d:%02d:%02d%s",
333             tw->tw_mday, tw_moty[tw->tw_mon],
334             tw->tw_year < 100 ? 2 : 4, tw->tw_year,
335             tw->tw_hour, tw->tw_min, tw->tw_sec, result);
336
337     if ((tw->tw_flags & TW_SDAY) == TW_SEXP)
338         snprintf (result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
339     else
340         if ((tw->tw_flags & TW_SDAY) == TW_SNIL)
341             strncpy (result, buffer, sizeof(result));
342         else
343             snprintf (result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
344
345     return result;
346 }
347
348
349 /*
350  * Get the timezone for given offset
351  */
352
353 char *
354 dtimezone (int offset, int flags)
355 {
356     int hours, mins;
357     struct zone *z;
358     static char buffer[10];
359
360     if (offset < 0) {
361         mins = -((-offset) % 60);
362         hours = -((-offset) / 60);
363     } else {
364         mins = offset % 60;
365         hours = offset / 60;
366     }
367
368     if (!(flags & TW_ZONE) && mins == 0) {
369 #if defined(HAVE_TZSET) && defined(HAVE_TZNAME)
370         tzset();
371         return ((flags & TW_DST) ? tzname[1] : tzname[0]);
372 #else
373         for (z = zones; z->std; z++)
374             if (z->shift == hours)
375                 return (z->dst && (flags & TW_DST) ? z->dst : z->std);
376 #endif
377     }
378
379 #if defined(DSTXXX)
380     if (flags & TW_DST)
381         hours += 1;
382 #endif /* defined(DSTXXX) */
383     snprintf (buffer, sizeof(buffer), "%s%02d%02d",
384                 offset < 0 ? "-" : "+", abs (hours), abs (mins));
385     return buffer;
386 }
387
388
389 /*
390  * Convert nmh time structure for local "broken-down"
391  * time to calendar time (clock value).  This routine
392  * is based on the gtime() routine written by Steven Shafer
393  * at CMU.  It was forwarded to MTR by Jay Lepreau at Utah-CS.
394  */
395
396 time_t
397 dmktime (struct tws *tw)
398 {
399     int i, sec, min, hour, mday, mon, year;
400     time_t result;
401
402     if (tw->tw_clock != 0)
403         return tw->tw_clock;
404
405     if ((sec = tw->tw_sec) < 0 || sec > 61
406             || (min = tw->tw_min) < 0 || min > 59
407             || (hour = tw->tw_hour) < 0 || hour > 23
408             || (mday = tw->tw_mday) < 1 || mday > 31
409             || (mon = tw->tw_mon + 1) < 1 || mon > 12)
410         return (tw->tw_clock = (time_t) -1);
411
412     year = tw->tw_year;
413
414     result = 0;
415     if (year < 100)
416         year += 1900;
417
418     for (i = 1970; i < year; i++)
419         result += dysize (i);
420     if (dysize (year) == 366 && mon >= 3)
421         result++;
422     while (--mon)
423         result += dmsize[mon - 1];
424     result += mday - 1;
425     result = 24 * result + hour;
426     result = 60 * result + min;
427     result = 60 * result + sec;
428     result -= 60 * tw->tw_zone;
429     if (tw->tw_flags & TW_DST)
430         result -= 60 * 60;
431
432     return (tw->tw_clock = result);
433 }
434
435
436 /*
437  * Simple calculation of day of the week.  Algorithm
438  * used is Zeller's congruence.  We assume that
439  * if tw->tw_year < 100, then the century = 19.
440  */
441
442 void
443 set_dotw (struct tws *tw)
444 {
445     int month, day, year, century;
446
447     month = tw->tw_mon - 1;
448     day = tw->tw_mday;
449     year = tw->tw_year % 100;
450     century = tw->tw_year < 100 ? 19 : tw->tw_year / 100;
451
452     if (month <= 0) {
453         month += 12;
454         if (--year < 0) {
455             year += 100;
456             century--;
457         }
458     }
459
460     tw->tw_wday =
461         ((26 * month - 2) / 10 + day + year + year / 4
462             - 3 * century / 4 + 1) % 7;
463     if (tw->tw_wday < 0)
464         tw->tw_wday += 7;
465
466     tw->tw_flags &= ~TW_SDAY, tw->tw_flags |= TW_SIMP;
467 }
468
469
470 /*
471  * Copy nmh time structure
472  */
473
474 void
475 twscopy (struct tws *tb, struct tws *tw)
476 {
477     *tb = *tw;  /* struct copy */
478
479 #if 0
480     tb->tw_sec   = tw->tw_sec;
481     tb->tw_min   = tw->tw_min;
482     tb->tw_hour  = tw->tw_hour;
483     tb->tw_mday  = tw->tw_mday;
484     tb->tw_mon   = tw->tw_mon;
485     tb->tw_year  = tw->tw_year;
486     tb->tw_wday  = tw->tw_wday;
487     tb->tw_yday  = tw->tw_yday;
488     tb->tw_zone  = tw->tw_zone;
489     tb->tw_clock = tw->tw_clock;
490     tb->tw_flags = tw->tw_flags;
491 #endif
492 }
493
494
495 /*
496  * Compare two nmh time structures
497  */
498
499 int
500 twsort (struct tws *tw1, struct tws *tw2)
501 {
502     time_t c1, c2;
503
504     if (tw1->tw_clock == 0)
505         dmktime (tw1);
506     if (tw2->tw_clock == 0)
507         dmktime (tw2);
508
509     return ((c1 = tw1->tw_clock) > (c2 = tw2->tw_clock) ? 1
510             : c1 == c2 ? 0 : -1);
511 }