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