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