]> diplodocus.org Git - nmh/blob - sbr/dtime.c
9a1002652c5f6b80363a415653942854da95aa4b
[nmh] / sbr / dtime.c
1 /* dtime.c -- time/date routines
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "dtime.h"
10 #include "h/nmh.h"
11 #include "h/utils.h"
12 #include "h/tws.h"
13 #include <time.h>
14
15 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF)
16 extern long timezone;
17 #endif
18
19 /* Size of largest possible int representing a time zone offset in minutes:
20 2,147,483,648 / 60 = 35,791,394 */
21 #define DTZ_BUFFER_SIZE (sizeof "+3579139459")
22
23 /*
24 * The number of days in the year, accounting for leap years
25 */
26 #define dysize(y) \
27 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
28
29 char *tw_moty[] = {
30 "Jan", "Feb", "Mar", "Apr",
31 "May", "Jun", "Jul", "Aug",
32 "Sep", "Oct", "Nov", "Dec",
33 NULL
34 };
35
36 char *tw_dotw[] = {
37 "Sun", "Mon", "Tue",
38 "Wed", "Thu", "Fri",
39 "Sat", NULL
40 };
41
42 char *tw_ldotw[] = {
43 "Sunday", "Monday", "Tuesday",
44 "Wednesday", "Thursday", "Friday",
45 "Saturday", NULL
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 /*
93 * tm_year is always "year - 1900".
94 * So we correct for this.
95 */
96 tw.tw_year = tm->tm_year + 1900;
97 tw.tw_wday = tm->tm_wday;
98 tw.tw_yday = tm->tm_yday;
99
100 tw.tw_flags = TW_NULL;
101 if (tm->tm_isdst)
102 tw.tw_flags |= TW_DST;
103
104 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
105 tw.tw_zone = tm->tm_gmtoff / 60;
106 if (tm->tm_isdst) /* if DST is in effect */
107 tw.tw_zone -= 60; /* reset to normal offset */
108 #else
109 {
110 static bool deja_vu;
111
112 if (!deja_vu) {
113 deja_vu = true;
114 tzset();
115 }
116 }
117 tw.tw_zone = -(timezone / 60);
118 #endif
119
120 tw.tw_flags &= ~TW_SDAY;
121 tw.tw_flags |= TW_SEXP;
122 tw.tw_flags |= TW_SZEXP;
123
124 tw.tw_clock = *clock;
125
126 return &tw;
127 }
128
129
130 /*
131 * Take clock value and return pointer to nmh time
132 * structure containing "broken-down" time. Time is
133 * expressed in UTC (Coordinated Universal Time).
134 */
135
136 struct tws *
137 dgmtime (time_t *clock)
138 {
139 static struct tws tw;
140 struct tm *tm;
141
142 if (!clock)
143 return NULL;
144
145 tm = gmtime (clock);
146
147 tw.tw_sec = tm->tm_sec;
148 tw.tw_min = tm->tm_min;
149 tw.tw_hour = tm->tm_hour;
150 tw.tw_mday = tm->tm_mday;
151 tw.tw_mon = tm->tm_mon;
152
153 /*
154 * tm_year is always "year - 1900"
155 * So we correct for this.
156 */
157 tw.tw_year = tm->tm_year + 1900;
158 tw.tw_wday = tm->tm_wday;
159 tw.tw_yday = tm->tm_yday;
160
161 tw.tw_flags = TW_NULL;
162 if (tm->tm_isdst)
163 tw.tw_flags |= TW_DST;
164
165 tw.tw_zone = 0;
166
167 tw.tw_flags &= ~TW_SDAY;
168 tw.tw_flags |= TW_SEXP;
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 %s\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 m_strn(tw->tw_year < 100 ? tw->tw_year + 1900 : tw->tw_year, 4));
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 RFC 822.
208 * The first form is required by RFC 1123.
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 RFC 822. The first form is required
229 * by RFC 1123 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
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 RFC 822 and RFC 1123.
251 */
252
253 char *
254 dasctime (struct tws *tw, int flags)
255 {
256 char buffer[77];
257 char tzbuffer[DTZ_BUFFER_SIZE];
258 static char result[80];
259 int twf;
260
261 if (!tw)
262 return NULL;
263
264 /* Display timezone if known */
265 if (tw->tw_flags & TW_SZEXP)
266 snprintf(tzbuffer, sizeof(tzbuffer), " %s", dtimezone(tw->tw_zone, tw->tw_flags | flags));
267 else
268 tzbuffer[0] = '\0';
269
270 snprintf(buffer, sizeof(buffer), "%02d %s %0*d %02d:%02d:%02d%s",
271 tw->tw_mday, tw_moty[tw->tw_mon],
272 tw->tw_year < 100 ? 2 : 4, tw->tw_year,
273 tw->tw_hour, tw->tw_min, tw->tw_sec, tzbuffer);
274
275 if ((twf = tw->tw_flags & TW_SDAY)) {
276 if (twf == TW_SEXP)
277 snprintf(result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
278 else
279 snprintf(result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
280 } else
281 strncpy(result, buffer, sizeof(result));
282
283 return result;
284 }
285
286
287 /*
288 * Get the timezone for given offset.
289 * This used to return a three-letter abbreviation for some offset
290 * values. But not many. Until there's a good way to do that,
291 * return the string representation of the numeric offset.
292 */
293
294 char *
295 dtimezone(int offset, int flags)
296 {
297 static char buffer[DTZ_BUFFER_SIZE];
298 bool pos;
299 unsigned os, hours, mins;
300
301 pos = offset >= 0;
302 os = pos ? offset : ~offset + 1; /* abs(3) undefined on INT_MIN. */
303 hours = os / 60;
304 mins = os % 60;
305
306 if (flags & TW_DST) /* Shift towards +infinity. */
307 hours += pos ? 1 : -1;
308
309 snprintf(buffer, sizeof(buffer), "%c%02u%02u",
310 pos ? '+' : '-', hours, mins);
311
312 return buffer;
313 }
314
315
316 /*
317 * Convert nmh time structure for local "broken-down"
318 * time to calendar time (clock value). This routine
319 * is based on the gtime() routine written by Steven Shafer
320 * at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
321 */
322
323 time_t
324 dmktime (struct tws *tw)
325 {
326 int i, sec, min, hour, mday, mon, year;
327 time_t result;
328
329 if (tw->tw_clock != 0)
330 return tw->tw_clock;
331
332 if ((sec = tw->tw_sec) < 0 || sec > 61
333 || (min = tw->tw_min) < 0 || min > 59
334 || (hour = tw->tw_hour) < 0 || hour > 23
335 || (mday = tw->tw_mday) < 1 || mday > 31
336 || (mon = tw->tw_mon + 1) < 1 || mon > 12)
337 return (tw->tw_clock = (time_t) -1);
338
339 year = tw->tw_year;
340
341 result = 0;
342 if (year < 1970)
343 year += 1900;
344
345 if (year < 1970)
346 year += 100;
347
348 for (i = 1970; i < year; i++)
349 result += dysize (i);
350 if (dysize (year) == 366 && mon >= 3)
351 result++;
352 while (--mon)
353 result += dmsize[mon - 1];
354 result += mday - 1;
355 result *= 24; /* Days to hours. */
356 result += hour;
357 result *= 60; /* Hours to minutes. */
358 result += min;
359 result *= 60; /* Minutes to seconds. */
360 result += sec;
361 result -= 60 * tw->tw_zone;
362 if (tw->tw_flags & TW_DST)
363 result -= 60 * 60; /* One hour. */
364
365 return tw->tw_clock = result;
366 }
367
368
369 /*
370 * Simple calculation of day of the week. Algorithm
371 * used is Zeller's congruence. We assume that
372 * if tw->tw_year < 100, then the century = 19.
373 */
374
375 void
376 set_dotw (struct tws *tw)
377 {
378 int month, day, year, century;
379
380 month = tw->tw_mon - 1;
381 day = tw->tw_mday;
382 year = tw->tw_year % 100;
383 century = tw->tw_year < 100 ? 19 : tw->tw_year / 100;
384
385 if (month <= 0) {
386 month += 12;
387 if (--year < 0) {
388 year += 100;
389 century--;
390 }
391 }
392
393 tw->tw_wday =
394 ((26 * month - 2) / 10 + day + year + year / 4
395 - 3 * century / 4 + 1) % 7;
396 if (tw->tw_wday < 0)
397 tw->tw_wday += 7;
398
399 tw->tw_flags &= ~TW_SDAY;
400 tw->tw_flags |= TW_SIMP;
401 }
402
403
404 /*
405 * Compare two nmh time structures
406 */
407
408 int
409 twsort (struct tws *tw1, struct tws *tw2)
410 {
411 time_t c1, c2;
412
413 if (tw1->tw_clock == 0)
414 dmktime (tw1);
415 if (tw2->tw_clock == 0)
416 dmktime (tw2);
417
418 return (c1 = tw1->tw_clock) > (c2 = tw2->tw_clock) ? 1
419 : c1 == c2 ? 0 : -1;
420 }