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