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