]> diplodocus.org Git - nmh/blob - sbr/dtime.c
uip/sortm.c: Remove unused variable and dlocaltimenow() call.
[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_SZONE;
117 tw.tw_flags |= TW_SZEXP;
118
119 tw.tw_clock = *clock;
120
121 return (&tw);
122 }
123
124
125 /*
126 * Take clock value and return pointer to nmh time
127 * structure containing "broken-down" time. Time is
128 * expressed in UTC (Coordinated Universal Time).
129 */
130
131 struct tws *
132 dgmtime (time_t *clock)
133 {
134 static struct tws tw;
135 struct tm *tm;
136
137 if (!clock)
138 return NULL;
139
140 tm = gmtime (clock);
141
142 tw.tw_sec = tm->tm_sec;
143 tw.tw_min = tm->tm_min;
144 tw.tw_hour = tm->tm_hour;
145 tw.tw_mday = tm->tm_mday;
146 tw.tw_mon = tm->tm_mon;
147
148 /*
149 * tm_year is always "year - 1900"
150 * So we correct for this.
151 */
152 tw.tw_year = tm->tm_year + 1900;
153 tw.tw_wday = tm->tm_wday;
154 tw.tw_yday = tm->tm_yday;
155
156 tw.tw_flags = TW_NULL;
157 if (tm->tm_isdst)
158 tw.tw_flags |= TW_DST;
159
160 tw.tw_zone = 0;
161
162 tw.tw_flags &= ~TW_SDAY;
163 tw.tw_flags |= TW_SEXP;
164 tw.tw_flags &= ~TW_SZONE;
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 %.4d\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 tw->tw_year < 100 ? tw->tw_year + 1900 : tw->tw_year);
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 rfc822.
204 * The first form is required by rfc1123.
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 rfc822. The first form is required
225 * by rfc1123 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 rfc822 and rfc1123.
247 */
248
249 char *
250 dasctime (struct tws *tw, int flags)
251 {
252 char buffer[80];
253 static char result[80];
254
255 if (!tw)
256 return NULL;
257
258 /* Display timezone if known */
259 if ((tw->tw_flags & TW_SZONE) == TW_SZNIL)
260 result[0] = '\0';
261 else
262 snprintf(result, sizeof(result), " %s", dtimezone(tw->tw_zone, tw->tw_flags | flags));
263
264 snprintf(buffer, sizeof(buffer), "%02d %s %0*d %02d:%02d:%02d%s",
265 tw->tw_mday, tw_moty[tw->tw_mon],
266 tw->tw_year < 100 ? 2 : 4, tw->tw_year,
267 tw->tw_hour, tw->tw_min, tw->tw_sec, result);
268
269 if ((tw->tw_flags & TW_SDAY) == TW_SEXP)
270 snprintf (result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
271 else
272 if ((tw->tw_flags & TW_SDAY) == TW_SNIL)
273 strncpy (result, buffer, sizeof(result));
274 else
275 snprintf (result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
276
277 return result;
278 }
279
280
281 /*
282 * Get the timezone for given offset.
283 * This used to return a three-letter abbreviation for some offset
284 * values. But not many. Until there's a good way to do that,
285 * return the string representation of the numeric offset.
286 */
287
288 char *
289 dtimezone (int offset, int flags)
290 {
291 int hours, mins;
292 static char buffer[10];
293
294 if (offset < 0) {
295 mins = -((-offset) % 60);
296 hours = -((-offset) / 60);
297 } else {
298 mins = offset % 60;
299 hours = offset / 60;
300 }
301
302 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
303 if (flags & TW_DST)
304 hours += 1;
305 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
306 snprintf (buffer, sizeof(buffer), "%s%02d%02d",
307 offset < 0 ? "-" : "+", abs (hours), abs (mins));
308 return buffer;
309 }
310
311
312 /*
313 * Convert nmh time structure for local "broken-down"
314 * time to calendar time (clock value). This routine
315 * is based on the gtime() routine written by Steven Shafer
316 * at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
317 */
318
319 time_t
320 dmktime (struct tws *tw)
321 {
322 int i, sec, min, hour, mday, mon, year;
323 time_t result;
324
325 if (tw->tw_clock != 0)
326 return tw->tw_clock;
327
328 if ((sec = tw->tw_sec) < 0 || sec > 61
329 || (min = tw->tw_min) < 0 || min > 59
330 || (hour = tw->tw_hour) < 0 || hour > 23
331 || (mday = tw->tw_mday) < 1 || mday > 31
332 || (mon = tw->tw_mon + 1) < 1 || mon > 12)
333 return (tw->tw_clock = (time_t) -1);
334
335 year = tw->tw_year;
336
337 result = 0;
338 if (year < 1970)
339 year += 1900;
340
341 if (year < 1970)
342 year += 100;
343
344 for (i = 1970; i < year; i++)
345 result += dysize (i);
346 if (dysize (year) == 366 && mon >= 3)
347 result++;
348 while (--mon)
349 result += dmsize[mon - 1];
350 result += mday - 1;
351 result = 24 * result + hour;
352 result = 60 * result + min;
353 result = 60 * result + sec;
354 result -= 60 * tw->tw_zone;
355 if (tw->tw_flags & TW_DST)
356 result -= 60 * 60;
357
358 return (tw->tw_clock = result);
359 }
360
361
362 /*
363 * Simple calculation of day of the week. Algorithm
364 * used is Zeller's congruence. We assume that
365 * if tw->tw_year < 100, then the century = 19.
366 */
367
368 void
369 set_dotw (struct tws *tw)
370 {
371 int month, day, year, century;
372
373 month = tw->tw_mon - 1;
374 day = tw->tw_mday;
375 year = tw->tw_year % 100;
376 century = tw->tw_year < 100 ? 19 : tw->tw_year / 100;
377
378 if (month <= 0) {
379 month += 12;
380 if (--year < 0) {
381 year += 100;
382 century--;
383 }
384 }
385
386 tw->tw_wday =
387 ((26 * month - 2) / 10 + day + year + year / 4
388 - 3 * century / 4 + 1) % 7;
389 if (tw->tw_wday < 0)
390 tw->tw_wday += 7;
391
392 tw->tw_flags &= ~TW_SDAY, tw->tw_flags |= TW_SIMP;
393 }
394
395
396 /*
397 * Copy nmh time structure
398 */
399
400 void
401 twscopy (struct tws *tb, struct tws *tw)
402 {
403 *tb = *tw; /* struct copy */
404 }
405
406
407 /*
408 * Compare two nmh time structures
409 */
410
411 int
412 twsort (struct tws *tw1, struct tws *tw2)
413 {
414 time_t c1, c2;
415
416 if (tw1->tw_clock == 0)
417 dmktime (tw1);
418 if (tw2->tw_clock == 0)
419 dmktime (tw2);
420
421 return ((c1 = tw1->tw_clock) > (c2 = tw2->tw_clock) ? 1
422 : c1 == c2 ? 0 : -1);
423 }