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