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)
17 extern char *tzname[];
21 # define abs(a) (a >= 0 ? a : -a)
25 ** The number of days in the year, accounting for leap years
28 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
31 "Jan", "Feb", "Mar", "Apr",
32 "May", "Jun", "Jul", "Aug",
33 "Sep", "Oct", "Nov", "Dec",
44 "Sunday", "Monday", "Tuesday",
45 "Wednesday", "Thursday", "Friday",
55 static int dmsize[] = {
56 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
61 ** Get current time (adjusted for local time
62 ** zone and daylight savings time) expressed
63 ** as nmh "broken-down" time structure.
72 return dlocaltime(&clock);
77 ** Take clock value and return pointer to nmh time structure
78 ** containing "broken-down" time. The time is adjusted for
79 ** local time zone and daylight savings time.
83 dlocaltime(time_t *clock)
88 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF)
95 tm = localtime(clock);
97 tw.tw_sec = tm->tm_sec;
98 tw.tw_min = tm->tm_min;
99 tw.tw_hour = tm->tm_hour;
100 tw.tw_mday = tm->tm_mday;
101 tw.tw_mon = tm->tm_mon;
104 * tm_year is always "year - 1900".
105 * So we correct for this.
107 tw.tw_year = tm->tm_year + 1900;
108 tw.tw_wday = tm->tm_wday;
109 tw.tw_yday = tm->tm_yday;
111 tw.tw_flags = TW_NULL;
113 tw.tw_flags |= TW_DST;
115 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
116 tw.tw_zone = tm->tm_gmtoff / 60;
117 if (tm->tm_isdst) /* if DST is in effect */
118 tw.tw_zone -= 60; /* reset to normal offset */
121 tw.tw_zone = -(timezone / 60);
124 tw.tw_flags &= ~TW_SDAY;
125 tw.tw_flags |= TW_SEXP;
126 tw.tw_flags &= ~TW_SZONE;
127 tw.tw_flags |= TW_SZEXP;
129 tw.tw_clock = *clock;
136 ** Take clock value and return pointer to nmh time
137 ** structure containing "broken-down" time. Time is
138 ** expressed in UTC (Coordinated Universal Time).
142 dgmtime(time_t *clock)
144 static struct tws tw;
152 tw.tw_sec = tm->tm_sec;
153 tw.tw_min = tm->tm_min;
154 tw.tw_hour = tm->tm_hour;
155 tw.tw_mday = tm->tm_mday;
156 tw.tw_mon = tm->tm_mon;
159 * tm_year is always "year - 1900"
160 * So we correct for this.
162 tw.tw_year = tm->tm_year + 1900;
163 tw.tw_wday = tm->tm_wday;
164 tw.tw_yday = tm->tm_yday;
166 tw.tw_flags = TW_NULL;
168 tw.tw_flags |= TW_DST;
172 tw.tw_flags &= ~TW_SDAY;
173 tw.tw_flags |= TW_SEXP;
174 tw.tw_flags &= ~TW_SZONE;
175 tw.tw_flags |= TW_SZEXP;
177 tw.tw_clock = *clock;
184 ** Using a nmh "broken-down" time structure,
185 ** produce a 26-byte date/time string, such as
187 ** Tue Jan 14 17:49:03 1992\n\0
191 dctime(struct tws *tw)
193 static char buffer[26];
198 snprintf(buffer, sizeof(buffer), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
199 tw_dotw[tw->tw_wday], tw_moty[tw->tw_mon], tw->tw_mday,
200 tw->tw_hour, tw->tw_min, tw->tw_sec,
201 tw->tw_year < 100 ? tw->tw_year + 1900 : tw->tw_year);
208 ** Produce a date/time string of the form
210 ** Mon, 16 Jun 1992 15:30:48 -700 (or)
211 ** Mon, 16 Jun 1992 15:30:48 EDT
213 ** for the current time, as specified by rfc822.
214 ** The first form is required by rfc1123.
218 dtimenow(int alpha_timezone)
223 return dtime(&clock, alpha_timezone);
228 ** Using a local calendar time value, produce
229 ** a date/time string of the form
231 ** Mon, 16 Jun 1992 15:30:48 -700 (or)
232 ** Mon, 16 Jun 1992 15:30:48 EDT
234 ** as specified by rfc822. The first form is required
235 ** by rfc1123 for outgoing messages.
239 dtime(time_t *clock, int alpha_timezone)
242 /* use alpha-numeric timezones */
243 return dasctime(dlocaltime(clock), TW_NULL);
245 /* use numeric timezones */
246 return dasctime(dlocaltime(clock), TW_ZONE);
251 ** Using a nmh "broken-down" time structure, produce
252 ** a date/time string of the form
254 ** Mon, 16 Jun 1992 15:30:48 -0700
256 ** as specified by rfc822 and rfc1123.
260 dasctime(struct tws *tw, int flags)
263 static char result[80];
268 /* Display timezone if known */
269 if ((tw->tw_flags & TW_SZONE) == TW_SZNIL)
272 snprintf(result, sizeof(result), " %s", dtimezone(tw->tw_zone, tw->tw_flags | flags));
274 snprintf(buffer, sizeof(buffer), "%02d %s %0*d %02d:%02d:%02d%s",
275 tw->tw_mday, tw_moty[tw->tw_mon],
276 tw->tw_year < 100 ? 2 : 4, tw->tw_year,
277 tw->tw_hour, tw->tw_min, tw->tw_sec, result);
279 if ((tw->tw_flags & TW_SDAY) == TW_SEXP)
280 snprintf(result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
282 if ((tw->tw_flags & TW_SDAY) == TW_SNIL)
283 strncpy(result, buffer, sizeof(result));
285 snprintf(result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
292 ** Get the timezone for given offset
296 dtimezone(int offset, int flags)
299 static char buffer[10];
302 mins = -((-offset) % 60);
303 hours = -((-offset) / 60);
309 if (!(flags & TW_ZONE) && mins == 0) {
311 return ((flags & TW_DST) ? tzname[1] : tzname[0]);
314 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
317 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
318 snprintf(buffer, sizeof(buffer), "%s%02d%02d",
319 offset < 0 ? "-" : "+", abs(hours), abs(mins));
325 ** Convert nmh time structure for local "broken-down"
326 ** time to calendar time (clock value). This routine
327 ** is based on the gtime() routine written by Steven Shafer
328 ** at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
332 dmktime(struct tws *tw)
334 int i, sec, min, hour, mday, mon, year;
337 if (tw->tw_clock != 0)
340 if ((sec = tw->tw_sec) < 0 || sec > 61
341 || (min = tw->tw_min) < 0 || min > 59
342 || (hour = tw->tw_hour) < 0 || hour > 23
343 || (mday = tw->tw_mday) < 1 || mday > 31
344 || (mon = tw->tw_mon + 1) < 1 || mon > 12)
345 return (tw->tw_clock = (time_t) -1);
356 for (i = 1970; i < year; i++)
358 if (dysize(year) == 366 && mon >= 3)
361 result += dmsize[mon - 1];
363 result = 24 * result + hour;
364 result = 60 * result + min;
365 result = 60 * result + sec;
366 result -= 60 * tw->tw_zone;
367 if (tw->tw_flags & TW_DST)
370 return (tw->tw_clock = result);
375 ** Simple calculation of day of the week. Algorithm
376 ** used is Zeller's congruence. We assume that
377 ** if tw->tw_year < 100, then the century = 19.
381 set_dotw(struct tws *tw)
383 int month, day, year, century;
385 month = tw->tw_mon - 1;
387 year = tw->tw_year % 100;
388 century = tw->tw_year < 100 ? 19 : tw->tw_year / 100;
398 tw->tw_wday = ((26 * month - 2) / 10 + day + year + year / 4
399 - 3 * century / 4 + 1) % 7;
403 tw->tw_flags &= ~TW_SDAY, tw->tw_flags |= TW_SIMP;
408 ** Copy nmh time structure
412 twscopy(struct tws *tb, struct tws *tw)
414 *tb = *tw; /* struct copy */
419 ** Compare two nmh time structures
423 twsort(struct tws *tw1, struct tws *tw2)
427 if (tw1->tw_clock == 0)
429 if (tw2->tw_clock == 0)
432 return ((c1 = tw1->tw_clock) > (c2 = tw2->tw_clock) ? 1
433 : c1 == c2 ? 0 : -1);