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