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 # define abs(a) (a >= 0 ? a : -a)
23 ** The number of days in the year, accounting for leap years
26 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
29 "Jan", "Feb", "Mar", "Apr",
30 "May", "Jun", "Jul", "Aug",
31 "Sep", "Oct", "Nov", "Dec",
42 "Sunday", "Monday", "Tuesday",
43 "Wednesday", "Thursday", "Friday",
53 static int dmsize[] = {
54 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
59 ** Get current time (adjusted for local time
60 ** zone and daylight savings time) expressed
61 ** as nmh "broken-down" time structure.
70 return dlocaltime(&clock);
75 ** Take clock value and return pointer to nmh time structure
76 ** containing "broken-down" time. The time is adjusted for
77 ** local time zone and daylight savings time.
81 dlocaltime(time_t *clock)
89 tm = localtime(clock);
91 tw.tw_sec = tm->tm_sec;
92 tw.tw_min = tm->tm_min;
93 tw.tw_hour = tm->tm_hour;
94 tw.tw_mday = tm->tm_mday;
95 tw.tw_mon = tm->tm_mon;
98 * tm_year is always "year - 1900".
99 * So we correct for this.
101 tw.tw_year = tm->tm_year + 1900;
102 tw.tw_wday = tm->tm_wday;
103 tw.tw_yday = tm->tm_yday;
105 tw.tw_flags = TW_NULL;
107 tw.tw_flags |= TW_DST;
109 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
110 tw.tw_zone = tm->tm_gmtoff / 60;
111 if (tm->tm_isdst) /* if DST is in effect */
112 tw.tw_zone -= 60; /* reset to normal offset */
115 tw.tw_zone = -(timezone / 60);
118 tw.tw_flags &= ~TW_SDAY;
119 tw.tw_flags |= TW_SEXP;
120 tw.tw_flags &= ~TW_SZONE;
121 tw.tw_flags |= TW_SZEXP;
123 tw.tw_clock = *clock;
130 ** Take clock value and return pointer to nmh time
131 ** structure containing "broken-down" time. Time is
132 ** expressed in UTC (Coordinated Universal Time).
136 dgmtime(time_t *clock)
138 static struct tws tw;
146 tw.tw_sec = tm->tm_sec;
147 tw.tw_min = tm->tm_min;
148 tw.tw_hour = tm->tm_hour;
149 tw.tw_mday = tm->tm_mday;
150 tw.tw_mon = tm->tm_mon;
153 * tm_year is always "year - 1900"
154 * So we correct for this.
156 tw.tw_year = tm->tm_year + 1900;
157 tw.tw_wday = tm->tm_wday;
158 tw.tw_yday = tm->tm_yday;
160 tw.tw_flags = TW_NULL;
162 tw.tw_flags |= TW_DST;
166 tw.tw_flags &= ~TW_SDAY;
167 tw.tw_flags |= TW_SEXP;
168 tw.tw_flags &= ~TW_SZONE;
169 tw.tw_flags |= TW_SZEXP;
171 tw.tw_clock = *clock;
178 ** Using a nmh "broken-down" time structure,
179 ** produce a 26-byte date/time string, such as
181 ** Tue Jan 14 17:49:03 1992\n\0
185 dctime(struct tws *tw)
187 static char buffer[26];
192 snprintf(buffer, sizeof(buffer), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
193 tw_dotw[tw->tw_wday], tw_moty[tw->tw_mon], tw->tw_mday,
194 tw->tw_hour, tw->tw_min, tw->tw_sec,
195 tw->tw_year < 100 ? tw->tw_year + 1900 : tw->tw_year);
202 ** Produce a date/time string of the form
204 ** Mon, 16 Jun 1992 15:30:48 -700 (or)
205 ** Mon, 16 Jun 1992 15:30:48 EDT
207 ** for the current time, as specified by rfc822.
208 ** The first form is required by rfc1123.
212 dtimenow(int alpha_timezone)
217 return dtime(&clock, alpha_timezone);
222 ** Using a local calendar time value, produce
223 ** a date/time string of the form
225 ** Mon, 16 Jun 1992 15:30:48 -700 (or)
226 ** Mon, 16 Jun 1992 15:30:48 EDT
228 ** as specified by rfc822. The first form is required
229 ** by rfc1123 for outgoing messages.
233 dtime(time_t *clock, int alpha_timezone)
236 /* use alpha-numeric timezones */
237 return dasctime(dlocaltime(clock), TW_NULL);
239 /* use numeric timezones */
240 return dasctime(dlocaltime(clock), TW_ZONE);
245 ** Using a nmh "broken-down" time structure, produce
246 ** a date/time string of the form
248 ** Mon, 16 Jun 1992 15:30:48 -0700
250 ** as specified by rfc822 and rfc1123.
254 dasctime(struct tws *tw, int flags)
257 static char result[80];
262 /* Display timezone if known */
263 if ((tw->tw_flags & TW_SZONE) == TW_SZNIL)
266 snprintf(result, sizeof(result), " %s", dtimezone(tw->tw_zone, tw->tw_flags | flags));
268 snprintf(buffer, sizeof(buffer), "%02d %s %0*d %02d:%02d:%02d%s",
269 tw->tw_mday, tw_moty[tw->tw_mon],
270 tw->tw_year < 100 ? 2 : 4, tw->tw_year,
271 tw->tw_hour, tw->tw_min, tw->tw_sec, result);
273 if ((tw->tw_flags & TW_SDAY) == TW_SEXP)
274 snprintf(result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
276 if ((tw->tw_flags & TW_SDAY) == TW_SNIL)
277 strncpy(result, buffer, sizeof(result));
279 snprintf(result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
286 ** Get the timezone for given offset
290 dtimezone(int offset, int flags)
293 static char buffer[10];
296 mins = -((-offset) % 60);
297 hours = -((-offset) / 60);
303 if (!(flags & TW_ZONE) && mins == 0) {
305 return ((flags & TW_DST) ? tzname[1] : tzname[0]);
308 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
311 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
312 snprintf(buffer, sizeof(buffer), "%s%02d%02d",
313 offset < 0 ? "-" : "+", abs(hours), abs(mins));
319 ** Convert nmh time structure for local "broken-down"
320 ** time to calendar time (clock value). This routine
321 ** is based on the gtime() routine written by Steven Shafer
322 ** at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
326 dmktime(struct tws *tw)
328 int i, sec, min, hour, mday, mon, year;
331 if (tw->tw_clock != 0)
334 if ((sec = tw->tw_sec) < 0 || sec > 61
335 || (min = tw->tw_min) < 0 || min > 59
336 || (hour = tw->tw_hour) < 0 || hour > 23
337 || (mday = tw->tw_mday) < 1 || mday > 31
338 || (mon = tw->tw_mon + 1) < 1 || mon > 12)
339 return (tw->tw_clock = (time_t) -1);
350 for (i = 1970; i < year; i++)
352 if (dysize(year) == 366 && mon >= 3)
355 result += dmsize[mon - 1];
357 result = 24 * result + hour;
358 result = 60 * result + min;
359 result = 60 * result + sec;
360 result -= 60 * tw->tw_zone;
361 if (tw->tw_flags & TW_DST)
364 return (tw->tw_clock = result);
369 ** Simple calculation of day of the week. Algorithm
370 ** used is Zeller's congruence. We assume that
371 ** if tw->tw_year < 100, then the century = 19.
375 set_dotw(struct tws *tw)
377 int month, day, year, century;
379 month = tw->tw_mon - 1;
381 year = tw->tw_year % 100;
382 century = tw->tw_year < 100 ? 19 : tw->tw_year / 100;
392 tw->tw_wday = ((26 * month - 2) / 10 + day + year + year / 4
393 - 3 * century / 4 + 1) % 7;
397 tw->tw_flags &= ~TW_SDAY, tw->tw_flags |= TW_SIMP;
402 ** Copy nmh time structure
406 twscopy(struct tws *tb, struct tws *tw)
408 *tb = *tw; /* struct copy */
413 ** Compare two nmh time structures
417 twsort(struct tws *tw1, struct tws *tw2)
421 if (tw1->tw_clock == 0)
423 if (tw2->tw_clock == 0)
426 return ((c1 = tw1->tw_clock) > (c2 = tw2->tw_clock) ? 1
427 : c1 == c2 ? 0 : -1);