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