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