]> diplodocus.org Git - nmh/blob - sbr/dtime.c
mhbuildsbr.c: Flip logic, moving goto to then-block; no need for else.
[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/utils.h>
11 #include <h/tws.h>
12 #include <time.h>
13
14 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF)
15 extern long timezone;
16 #endif
17
18 /*
19 * The number of days in the year, accounting for leap years
20 */
21 #define dysize(y) \
22 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
23
24 char *tw_moty[] = {
25 "Jan", "Feb", "Mar", "Apr",
26 "May", "Jun", "Jul", "Aug",
27 "Sep", "Oct", "Nov", "Dec",
28 NULL
29 };
30
31 char *tw_dotw[] = {
32 "Sun", "Mon", "Tue",
33 "Wed", "Thu", "Fri",
34 "Sat", NULL
35 };
36
37 char *tw_ldotw[] = {
38 "Sunday", "Monday", "Tuesday",
39 "Wednesday", "Thursday", "Friday",
40 "Saturday", NULL
41 };
42
43 static int dmsize[] = {
44 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
45 };
46
47
48 /*
49 * Get current time (adjusted for local time
50 * zone and daylight savings time) expressed
51 * as nmh "broken-down" time structure.
52 */
53
54 struct tws *
55 dlocaltimenow (void)
56 {
57 time_t clock;
58
59 time (&clock);
60 return dlocaltime (&clock);
61 }
62
63
64 /*
65 * Take clock value and return pointer to nmh time structure
66 * containing "broken-down" time. The time is adjusted for
67 * local time zone and daylight savings time.
68 */
69
70 struct tws *
71 dlocaltime (time_t *clock)
72 {
73 static struct tws tw;
74 struct tm *tm;
75
76 if (!clock)
77 return NULL;
78
79 tm = localtime (clock);
80
81 tw.tw_sec = tm->tm_sec;
82 tw.tw_min = tm->tm_min;
83 tw.tw_hour = tm->tm_hour;
84 tw.tw_mday = tm->tm_mday;
85 tw.tw_mon = tm->tm_mon;
86
87 /*
88 * tm_year is always "year - 1900".
89 * So we correct for this.
90 */
91 tw.tw_year = tm->tm_year + 1900;
92 tw.tw_wday = tm->tm_wday;
93 tw.tw_yday = tm->tm_yday;
94
95 tw.tw_flags = TW_NULL;
96 if (tm->tm_isdst)
97 tw.tw_flags |= TW_DST;
98
99 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
100 tw.tw_zone = tm->tm_gmtoff / 60;
101 if (tm->tm_isdst) /* if DST is in effect */
102 tw.tw_zone -= 60; /* reset to normal offset */
103 #else
104 {
105 static bool deja_vu;
106
107 if (!deja_vu) {
108 deja_vu = true;
109 tzset();
110 }
111 }
112 tw.tw_zone = -(timezone / 60);
113 #endif
114
115 tw.tw_flags &= ~TW_SDAY;
116 tw.tw_flags |= TW_SEXP;
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_SZEXP;
165
166 tw.tw_clock = *clock;
167
168 return (&tw);
169 }
170
171
172 /*
173 * Using a nmh "broken-down" time structure,
174 * produce a 26-byte date/time string, such as
175 *
176 * Tue Jan 14 17:49:03 1992\n\0
177 */
178
179 char *
180 dctime (struct tws *tw)
181 {
182 static char buffer[26];
183
184 if (!tw)
185 return NULL;
186
187 snprintf (buffer, sizeof(buffer), "%.3s %.3s %02d %02d:%02d:%02d %s\n",
188 tw_dotw[tw->tw_wday], tw_moty[tw->tw_mon], tw->tw_mday,
189 tw->tw_hour, tw->tw_min, tw->tw_sec,
190 m_strn(tw->tw_year < 100 ? tw->tw_year + 1900 : tw->tw_year, 4));
191
192 return buffer;
193 }
194
195
196 /*
197 * Produce a date/time string of the form
198 *
199 * Mon, 16 Jun 1992 15:30:48 -700 (or)
200 * Mon, 16 Jun 1992 15:30:48 EDT
201 *
202 * for the current time, as specified by RFC 822.
203 * The first form is required by RFC 1123.
204 */
205
206 char *
207 dtimenow (int alpha_timezone)
208 {
209 time_t clock;
210
211 time (&clock);
212 return dtime (&clock, alpha_timezone);
213 }
214
215
216 /*
217 * Using a local calendar time value, produce
218 * a date/time string of the form
219 *
220 * Mon, 16 Jun 1992 15:30:48 -700 (or)
221 * Mon, 16 Jun 1992 15:30:48 EDT
222 *
223 * as specified by RFC 822. The first form is required
224 * by RFC 1123 for outgoing messages.
225 */
226
227 char *
228 dtime (time_t *clock, int alpha_timezone)
229 {
230 if (alpha_timezone)
231 /* use alpha-numeric timezones */
232 return dasctime (dlocaltime (clock), TW_NULL);
233
234 /* use numeric timezones */
235 return dasctime (dlocaltime (clock), TW_ZONE);
236 }
237
238
239 /*
240 * Using a nmh "broken-down" time structure, produce
241 * a date/time string of the form
242 *
243 * Mon, 16 Jun 1992 15:30:48 -0700
244 *
245 * as specified by RFC 822 and RFC 1123.
246 */
247
248 char *
249 dasctime (struct tws *tw, int flags)
250 {
251 char buffer[80];
252 static char result[80];
253 int twf;
254
255 if (!tw)
256 return NULL;
257
258 /* Display timezone if known */
259 if (tw->tw_flags & TW_SZEXP)
260 snprintf(result, sizeof(result), " %s", dtimezone(tw->tw_zone, tw->tw_flags | flags));
261 else
262 result[0] = '\0';
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 ((twf = tw->tw_flags & TW_SDAY)) {
270 if (twf == TW_SEXP)
271 snprintf(result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
272 else
273 snprintf(result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
274 } else
275 strncpy(result, buffer, sizeof(result));
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 *dtimezone(int offset, int flags)
289 {
290 static char buffer[sizeof "+3579139459"]; /* 2,147,483,648 / 60 = 35,791,394 */
291 bool pos;
292 unsigned os, hours, mins;
293
294 pos = offset >= 0;
295 os = pos ? offset : ~offset + 1; /* abs(3) undefined on INT_MIN. */
296 hours = os / 60;
297 mins = os % 60;
298
299 if (flags & TW_DST) /* Shift towards +infinity. */
300 hours += pos ? 1 : -1;
301
302 snprintf(buffer, sizeof(buffer), "%c%02u%02u",
303 pos ? '+' : '-', hours, mins);
304
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 }