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