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