3 * dtime.c -- time/date routines
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.
10 #include <h/mh.h> /* for snprintf() */
15 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF)
18 extern char *tzname[];
22 # define abs(a) (a >= 0 ? a : -a)
26 * The number of days in the year, accounting for leap years
29 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
32 "Jan", "Feb", "Mar", "Apr",
33 "May", "Jun", "Jul", "Aug",
34 "Sep", "Oct", "Nov", "Dec",
45 "Sunday", "Monday", "Tuesday",
46 "Wednesday", "Thursday", "Friday",
56 static struct zone zones[] = {
63 /* RFC1123 specifies do not use military TZs */
96 static int dmsize[] = {
97 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
102 * Get current time (adjusted for local time
103 * zone and daylight savings time) expressed
104 * as nmh "broken-down" time structure.
113 return dlocaltime (&clock);
118 * Take clock value and return pointer to nmh time structure
119 * containing "broken-down" time. The time is adjusted for
120 * local time zone and daylight savings time.
124 dlocaltime (time_t *clock)
126 static struct tws tw;
132 tm = localtime (clock);
134 tw.tw_sec = tm->tm_sec;
135 tw.tw_min = tm->tm_min;
136 tw.tw_hour = tm->tm_hour;
137 tw.tw_mday = tm->tm_mday;
138 tw.tw_mon = tm->tm_mon;
141 * tm_year is always "year - 1900".
142 * So we correct for this.
144 tw.tw_year = tm->tm_year + 1900;
145 tw.tw_wday = tm->tm_wday;
146 tw.tw_yday = tm->tm_yday;
148 tw.tw_flags = TW_NULL;
150 tw.tw_flags |= TW_DST;
152 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
153 tw.tw_zone = tm->tm_gmtoff / 60;
154 if (tm->tm_isdst) /* if DST is in effect */
155 tw.tw_zone -= 60; /* reset to normal offset */
158 tw.tw_zone = -(timezone / 60);
161 tw.tw_flags &= ~TW_SDAY;
162 tw.tw_flags |= TW_SEXP;
163 tw.tw_flags &= ~TW_SZONE;
164 tw.tw_flags |= TW_SZEXP;
166 tw.tw_clock = *clock;
173 * Take clock value and return pointer to nmh time
174 * structure containing "broken-down" time. Time is
175 * expressed in UTC (Coordinated Universal Time).
179 dgmtime (time_t *clock)
181 static struct tws tw;
189 tw.tw_sec = tm->tm_sec;
190 tw.tw_min = tm->tm_min;
191 tw.tw_hour = tm->tm_hour;
192 tw.tw_mday = tm->tm_mday;
193 tw.tw_mon = tm->tm_mon;
196 * tm_year is always "year - 1900"
197 * So we correct for this.
199 tw.tw_year = tm->tm_year + 1900;
200 tw.tw_wday = tm->tm_wday;
201 tw.tw_yday = tm->tm_yday;
203 tw.tw_flags = TW_NULL;
205 tw.tw_flags |= TW_DST;
209 tw.tw_flags &= ~TW_SDAY;
210 tw.tw_flags |= TW_SEXP;
211 tw.tw_flags &= ~TW_SZONE;
212 tw.tw_flags |= TW_SZEXP;
214 tw.tw_clock = *clock;
221 * Using a nmh "broken-down" time structure,
222 * produce a 26-byte date/time string, such as
224 * Tue Jan 14 17:49:03 1992\n\0
228 dctime (struct tws *tw)
230 static char buffer[26];
235 snprintf (buffer, sizeof(buffer), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
236 tw_dotw[tw->tw_wday], tw_moty[tw->tw_mon], tw->tw_mday,
237 tw->tw_hour, tw->tw_min, tw->tw_sec,
238 tw->tw_year < 100 ? tw->tw_year + 1900 : tw->tw_year);
245 * Produce a date/time string of the form
247 * Mon, 16 Jun 1992 15:30:48 -700 (or)
248 * Mon, 16 Jun 1992 15:30:48 EDT
250 * for the current time, as specified by rfc822.
251 * The first form is required by rfc1123.
255 dtimenow (int alpha_timezone)
260 return dtime (&clock, alpha_timezone);
265 * Using a local calendar time value, produce
266 * a date/time string of the form
268 * Mon, 16 Jun 1992 15:30:48 -700 (or)
269 * Mon, 16 Jun 1992 15:30:48 EDT
271 * as specified by rfc822. The first form is required
272 * by rfc1123 for outgoing messages.
276 dtime (time_t *clock, int alpha_timezone)
279 /* use alpha-numeric timezones */
280 return dasctime (dlocaltime (clock), TW_NULL);
282 /* use numeric timezones */
283 return dasctime (dlocaltime (clock), TW_ZONE);
288 * Using a nmh "broken-down" time structure, produce
289 * a date/time string of the form
291 * Mon, 16 Jun 1992 15:30:48 -0700
293 * as specified by rfc822 and rfc1123.
297 dasctime (struct tws *tw, int flags)
300 static char result[80];
305 /* Display timezone if known */
306 if ((tw->tw_flags & TW_SZONE) == TW_SZNIL)
309 snprintf(result, sizeof(result), " %s", dtimezone(tw->tw_zone, tw->tw_flags | flags));
311 snprintf(buffer, sizeof(buffer), "%02d %s %0*d %02d:%02d:%02d%s",
312 tw->tw_mday, tw_moty[tw->tw_mon],
313 tw->tw_year < 100 ? 2 : 4, tw->tw_year,
314 tw->tw_hour, tw->tw_min, tw->tw_sec, result);
316 if ((tw->tw_flags & TW_SDAY) == TW_SEXP)
317 snprintf (result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
319 if ((tw->tw_flags & TW_SDAY) == TW_SNIL)
320 strncpy (result, buffer, sizeof(result));
322 snprintf (result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
329 * Get the timezone for given offset
333 dtimezone (int offset, int flags)
337 static char buffer[10];
340 mins = -((-offset) % 60);
341 hours = -((-offset) / 60);
347 if (!(flags & TW_ZONE) && mins == 0) {
349 return ((flags & TW_DST) ? tzname[1] : tzname[0]);
352 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
355 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
356 snprintf (buffer, sizeof(buffer), "%s%02d%02d",
357 offset < 0 ? "-" : "+", abs (hours), abs (mins));
363 * Convert nmh time structure for local "broken-down"
364 * time to calendar time (clock value). This routine
365 * is based on the gtime() routine written by Steven Shafer
366 * at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
370 dmktime (struct tws *tw)
372 int i, sec, min, hour, mday, mon, year;
375 if (tw->tw_clock != 0)
378 if ((sec = tw->tw_sec) < 0 || sec > 61
379 || (min = tw->tw_min) < 0 || min > 59
380 || (hour = tw->tw_hour) < 0 || hour > 23
381 || (mday = tw->tw_mday) < 1 || mday > 31
382 || (mon = tw->tw_mon + 1) < 1 || mon > 12)
383 return (tw->tw_clock = (time_t) -1);
394 for (i = 1970; i < year; i++)
395 result += dysize (i);
396 if (dysize (year) == 366 && mon >= 3)
399 result += dmsize[mon - 1];
401 result = 24 * result + hour;
402 result = 60 * result + min;
403 result = 60 * result + sec;
404 result -= 60 * tw->tw_zone;
405 if (tw->tw_flags & TW_DST)
408 return (tw->tw_clock = result);
413 * Simple calculation of day of the week. Algorithm
414 * used is Zeller's congruence. We assume that
415 * if tw->tw_year < 100, then the century = 19.
419 set_dotw (struct tws *tw)
421 int month, day, year, century;
423 month = tw->tw_mon - 1;
425 year = tw->tw_year % 100;
426 century = tw->tw_year < 100 ? 19 : tw->tw_year / 100;
437 ((26 * month - 2) / 10 + day + year + year / 4
438 - 3 * century / 4 + 1) % 7;
442 tw->tw_flags &= ~TW_SDAY, tw->tw_flags |= TW_SIMP;
447 * Copy nmh time structure
451 twscopy (struct tws *tb, struct tws *tw)
453 *tb = *tw; /* struct copy */
456 tb->tw_sec = tw->tw_sec;
457 tb->tw_min = tw->tw_min;
458 tb->tw_hour = tw->tw_hour;
459 tb->tw_mday = tw->tw_mday;
460 tb->tw_mon = tw->tw_mon;
461 tb->tw_year = tw->tw_year;
462 tb->tw_wday = tw->tw_wday;
463 tb->tw_yday = tw->tw_yday;
464 tb->tw_zone = tw->tw_zone;
465 tb->tw_clock = tw->tw_clock;
466 tb->tw_flags = tw->tw_flags;
472 * Compare two nmh time structures
476 twsort (struct tws *tw1, struct tws *tw2)
480 if (tw1->tw_clock == 0)
482 if (tw2->tw_clock == 0)
485 return ((c1 = tw1->tw_clock) > (c2 = tw2->tw_clock) ? 1
486 : c1 == c2 ? 0 : -1);