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