2 ** dtime.c -- time/date routines
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
9 #include <h/mh.h> /* for snprintf() */
14 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF)
19 ** The number of days in the year, accounting for leap years
22 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
25 "Jan", "Feb", "Mar", "Apr",
26 "May", "Jun", "Jul", "Aug",
27 "Sep", "Oct", "Nov", "Dec",
38 "Sunday", "Monday", "Tuesday",
39 "Wednesday", "Thursday", "Friday",
49 static int dmsize[] = {
50 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
55 ** Get current time (adjusted for local time
56 ** zone and daylight savings time) expressed
57 ** as nmh "broken-down" time structure.
66 return dlocaltime(&clock);
71 ** Take clock value and return pointer to nmh time structure
72 ** containing "broken-down" time. The time is adjusted for
73 ** local time zone and daylight savings time.
77 dlocaltime(time_t *clock)
85 tm = localtime(clock);
87 tw.tw_sec = tm->tm_sec;
88 tw.tw_min = tm->tm_min;
89 tw.tw_hour = tm->tm_hour;
90 tw.tw_mday = tm->tm_mday;
91 tw.tw_mon = tm->tm_mon;
94 * tm_year is always "year - 1900".
95 * So we correct for this.
97 tw.tw_year = tm->tm_year + 1900;
98 tw.tw_wday = tm->tm_wday;
99 tw.tw_yday = tm->tm_yday;
101 tw.tw_flags = TW_NULL;
103 tw.tw_flags |= TW_DST;
105 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
106 tw.tw_zone = tm->tm_gmtoff / 60;
107 if (tm->tm_isdst) /* if DST is in effect */
108 tw.tw_zone -= 60; /* reset to normal offset */
111 tw.tw_zone = -(timezone / 60);
114 tw.tw_flags &= ~TW_SDAY;
115 tw.tw_flags |= TW_SEXP;
116 tw.tw_flags &= ~TW_SZONE;
117 tw.tw_flags |= TW_SZEXP;
119 tw.tw_clock = *clock;
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).
132 dgmtime(time_t *clock)
134 static struct tws tw;
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;
149 * tm_year is always "year - 1900"
150 * So we correct for this.
152 tw.tw_year = tm->tm_year + 1900;
153 tw.tw_wday = tm->tm_wday;
154 tw.tw_yday = tm->tm_yday;
156 tw.tw_flags = TW_NULL;
158 tw.tw_flags |= TW_DST;
162 tw.tw_flags &= ~TW_SDAY;
163 tw.tw_flags |= TW_SEXP;
164 tw.tw_flags &= ~TW_SZONE;
165 tw.tw_flags |= TW_SZEXP;
167 tw.tw_clock = *clock;
174 ** Using a nmh "broken-down" time structure,
175 ** produce a 26-byte date/time string, such as
177 ** Tue Jan 14 17:49:03 1992\n\0
181 dctime(struct tws *tw)
183 static char buffer[26];
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);
198 ** Produce a date/time string of the form
200 ** Mon, 16 Jun 1992 15:30:48 -700 (or)
201 ** Mon, 16 Jun 1992 15:30:48 EDT
203 ** for the current time, as specified by rfc822.
204 ** The first form is required by rfc1123.
208 dtimenow(int alpha_timezone)
213 return dtime(&clock, alpha_timezone);
218 ** Using a local calendar time value, produce
219 ** a date/time string of the form
221 ** Mon, 16 Jun 1992 15:30:48 -700 (or)
222 ** Mon, 16 Jun 1992 15:30:48 EDT
224 ** as specified by rfc822. The first form is required
225 ** by rfc1123 for outgoing messages.
229 dtime(time_t *clock, int alpha_timezone)
232 /* use alpha-numeric timezones */
233 return dasctime(dlocaltime(clock), TW_NULL);
235 /* use numeric timezones */
236 return dasctime(dlocaltime(clock), TW_ZONE);
241 ** Using a nmh "broken-down" time structure, produce
242 ** a date/time string of the form
244 ** Mon, 16 Jun 1992 15:30:48 -0700
246 ** as specified by rfc822 and rfc1123.
250 dasctime(struct tws *tw, int flags)
253 static char result[80];
258 /* Display timezone if known */
259 if ((tw->tw_flags & TW_SZONE) == TW_SZNIL)
262 snprintf(result, sizeof(result), " %s", dtimezone(tw->tw_zone, tw->tw_flags | flags));
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);
269 if ((tw->tw_flags & TW_SDAY) == TW_SEXP)
270 snprintf(result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
272 if ((tw->tw_flags & TW_SDAY) == TW_SNIL)
273 strncpy(result, buffer, sizeof(result));
275 snprintf(result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
282 ** Get the timezone for given offset
286 dtimezone(int offset, int flags)
289 static char buffer[10];
292 mins = -((-offset) % 60);
293 hours = -((-offset) / 60);
299 if (!(flags & TW_ZONE) && mins == 0) {
301 return ((flags & TW_DST) ? tzname[1] : tzname[0]);
304 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
307 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
308 snprintf(buffer, sizeof(buffer), "%s%02d%02d",
309 offset < 0 ? "-" : "+", abs(hours), abs(mins));
315 ** Convert nmh time structure for local "broken-down"
316 ** time to calendar time (clock value). This routine
317 ** is based on the gtime() routine written by Steven Shafer
318 ** at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
322 dmktime(struct tws *tw)
324 int i, sec, min, hour, mday, mon, year;
327 if (tw->tw_clock != 0)
330 if ((sec = tw->tw_sec) < 0 || sec > 61
331 || (min = tw->tw_min) < 0 || min > 59
332 || (hour = tw->tw_hour) < 0 || hour > 23
333 || (mday = tw->tw_mday) < 1 || mday > 31
334 || (mon = tw->tw_mon + 1) < 1 || mon > 12)
335 return (tw->tw_clock = (time_t) -1);
346 for (i = 1970; i < year; i++)
348 if (dysize(year) == 366 && mon >= 3)
351 result += dmsize[mon - 1];
353 result = 24 * result + hour;
354 result = 60 * result + min;
355 result = 60 * result + sec;
356 result -= 60 * tw->tw_zone;
357 if (tw->tw_flags & TW_DST)
360 return (tw->tw_clock = result);
365 ** Simple calculation of day of the week. Algorithm
366 ** used is Zeller's congruence. We assume that
367 ** if tw->tw_year < 100, then the century = 19.
371 set_dotw(struct tws *tw)
373 int month, day, year, century;
375 month = tw->tw_mon - 1;
377 year = tw->tw_year % 100;
378 century = tw->tw_year < 100 ? 19 : tw->tw_year / 100;
388 tw->tw_wday = ((26 * month - 2) / 10 + day + year + year / 4
389 - 3 * century / 4 + 1) % 7;
393 tw->tw_flags &= ~TW_SDAY, tw->tw_flags |= TW_SIMP;
398 ** Copy nmh time structure
402 twscopy(struct tws *tb, struct tws *tw)
404 *tb = *tw; /* struct copy */
409 ** Compare two nmh time structures
413 twsort(struct tws *tw1, struct tws *tw2)
417 if (tw1->tw_clock == 0)
419 if (tw2->tw_clock == 0)
422 return ((c1 = tw1->tw_clock) > (c2 = tw2->tw_clock) ? 1
423 : c1 == c2 ? 0 : -1);