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