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() */
13 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF) && !defined(HAVE_TZSET)
14 # include <sys/timeb.h>
17 #ifdef TIME_WITH_SYS_TIME
18 # include <sys/time.h>
21 # ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
28 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF) && defined(HAVE_TZSET)
31 extern char *tzname[];
35 # define abs(a) (a >= 0 ? a : -a)
39 ** The number of days in the year, accounting for leap years
42 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
45 "Jan", "Feb", "Mar", "Apr",
46 "May", "Jun", "Jul", "Aug",
47 "Sep", "Oct", "Nov", "Dec",
58 "Sunday", "Monday", "Tuesday",
59 "Wednesday", "Thursday", "Friday",
69 static struct zone zones[] = {
76 /* RFC1123 specifies do not use military TZs */
109 static int dmsize[] = {
110 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
115 ** Get current time (adjusted for local time
116 ** zone and daylight savings time) expressed
117 ** as nmh "broken-down" time structure.
126 return dlocaltime(&clock);
131 ** Take clock value and return pointer to nmh time structure
132 ** containing "broken-down" time. The time is adjusted for
133 ** local time zone and daylight savings time.
137 dlocaltime(time_t *clock)
139 static struct tws tw;
142 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF) && !defined(HAVE_TZSET)
149 tm = localtime(clock);
151 tw.tw_sec = tm->tm_sec;
152 tw.tw_min = tm->tm_min;
153 tw.tw_hour = tm->tm_hour;
154 tw.tw_mday = tm->tm_mday;
155 tw.tw_mon = tm->tm_mon;
158 * tm_year is always "year - 1900".
159 * So we correct for this.
161 tw.tw_year = tm->tm_year + 1900;
162 tw.tw_wday = tm->tm_wday;
163 tw.tw_yday = tm->tm_yday;
165 tw.tw_flags = TW_NULL;
167 tw.tw_flags |= TW_DST;
169 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
170 tw.tw_zone = tm->tm_gmtoff / 60;
171 if (tm->tm_isdst) /* if DST is in effect */
172 tw.tw_zone -= 60; /* reset to normal offset */
176 tw.tw_zone = -(timezone / 60);
179 tw.tw_zone = -tb.timezone;
183 tw.tw_flags &= ~TW_SDAY;
184 tw.tw_flags |= TW_SEXP;
185 tw.tw_flags &= ~TW_SZONE;
186 tw.tw_flags |= TW_SZEXP;
188 tw.tw_clock = *clock;
195 ** Take clock value and return pointer to nmh time
196 ** structure containing "broken-down" time. Time is
197 ** expressed in UTC (Coordinated Universal Time).
201 dgmtime(time_t *clock)
203 static struct tws tw;
211 tw.tw_sec = tm->tm_sec;
212 tw.tw_min = tm->tm_min;
213 tw.tw_hour = tm->tm_hour;
214 tw.tw_mday = tm->tm_mday;
215 tw.tw_mon = tm->tm_mon;
218 * tm_year is always "year - 1900"
219 * So we correct for this.
221 tw.tw_year = tm->tm_year + 1900;
222 tw.tw_wday = tm->tm_wday;
223 tw.tw_yday = tm->tm_yday;
225 tw.tw_flags = TW_NULL;
227 tw.tw_flags |= TW_DST;
231 tw.tw_flags &= ~TW_SDAY;
232 tw.tw_flags |= TW_SEXP;
233 tw.tw_flags &= ~TW_SZONE;
234 tw.tw_flags |= TW_SZEXP;
236 tw.tw_clock = *clock;
243 ** Using a nmh "broken-down" time structure,
244 ** produce a 26-byte date/time string, such as
246 ** Tue Jan 14 17:49:03 1992\n\0
250 dctime(struct tws *tw)
252 static char buffer[26];
257 snprintf(buffer, sizeof(buffer), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
258 tw_dotw[tw->tw_wday], tw_moty[tw->tw_mon], tw->tw_mday,
259 tw->tw_hour, tw->tw_min, tw->tw_sec,
260 tw->tw_year < 100 ? tw->tw_year + 1900 : tw->tw_year);
267 ** Produce a date/time string of the form
269 ** Mon, 16 Jun 1992 15:30:48 -700 (or)
270 ** Mon, 16 Jun 1992 15:30:48 EDT
272 ** for the current time, as specified by rfc822.
273 ** The first form is required by rfc1123.
277 dtimenow(int alpha_timezone)
282 return dtime(&clock, alpha_timezone);
287 ** Using a local calendar time value, produce
288 ** a date/time string of the form
290 ** Mon, 16 Jun 1992 15:30:48 -700 (or)
291 ** Mon, 16 Jun 1992 15:30:48 EDT
293 ** as specified by rfc822. The first form is required
294 ** by rfc1123 for outgoing messages.
298 dtime(time_t *clock, int alpha_timezone)
301 /* use alpha-numeric timezones */
302 return dasctime(dlocaltime(clock), TW_NULL);
304 /* use numeric timezones */
305 return dasctime(dlocaltime(clock), TW_ZONE);
310 ** Using a nmh "broken-down" time structure, produce
311 ** a date/time string of the form
313 ** Mon, 16 Jun 1992 15:30:48 -0700
315 ** as specified by rfc822 and rfc1123.
319 dasctime(struct tws *tw, int flags)
322 static char result[80];
327 /* Display timezone if known */
328 if ((tw->tw_flags & TW_SZONE) == TW_SZNIL)
331 snprintf(result, sizeof(result), " %s", dtimezone(tw->tw_zone, tw->tw_flags | flags));
333 snprintf(buffer, sizeof(buffer), "%02d %s %0*d %02d:%02d:%02d%s",
334 tw->tw_mday, tw_moty[tw->tw_mon],
335 tw->tw_year < 100 ? 2 : 4, tw->tw_year,
336 tw->tw_hour, tw->tw_min, tw->tw_sec, result);
338 if ((tw->tw_flags & TW_SDAY) == TW_SEXP)
339 snprintf(result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
341 if ((tw->tw_flags & TW_SDAY) == TW_SNIL)
342 strncpy(result, buffer, sizeof(result));
344 snprintf(result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
351 ** Get the timezone for given offset
355 dtimezone(int offset, int flags)
359 static char buffer[10];
362 mins = -((-offset) % 60);
363 hours = -((-offset) / 60);
369 if (!(flags & TW_ZONE) && mins == 0) {
370 #if defined(HAVE_TZSET) && defined(HAVE_TZNAME)
372 return ((flags & TW_DST) ? tzname[1] : tzname[0]);
374 for (z = zones; z->std; z++)
375 if (z->shift == hours)
376 return (z->dst && (flags & TW_DST) ? z->dst : z->std);
380 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
383 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
384 snprintf(buffer, sizeof(buffer), "%s%02d%02d",
385 offset < 0 ? "-" : "+", abs(hours), abs(mins));
391 ** Convert nmh time structure for local "broken-down"
392 ** time to calendar time (clock value). This routine
393 ** is based on the gtime() routine written by Steven Shafer
394 ** at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
398 dmktime(struct tws *tw)
400 int i, sec, min, hour, mday, mon, year;
403 if (tw->tw_clock != 0)
406 if ((sec = tw->tw_sec) < 0 || sec > 61
407 || (min = tw->tw_min) < 0 || min > 59
408 || (hour = tw->tw_hour) < 0 || hour > 23
409 || (mday = tw->tw_mday) < 1 || mday > 31
410 || (mon = tw->tw_mon + 1) < 1 || mon > 12)
411 return (tw->tw_clock = (time_t) -1);
422 for (i = 1970; i < year; i++)
424 if (dysize(year) == 366 && mon >= 3)
427 result += dmsize[mon - 1];
429 result = 24 * result + hour;
430 result = 60 * result + min;
431 result = 60 * result + sec;
432 result -= 60 * tw->tw_zone;
433 if (tw->tw_flags & TW_DST)
436 return (tw->tw_clock = result);
441 ** Simple calculation of day of the week. Algorithm
442 ** used is Zeller's congruence. We assume that
443 ** if tw->tw_year < 100, then the century = 19.
447 set_dotw(struct tws *tw)
449 int month, day, year, century;
451 month = tw->tw_mon - 1;
453 year = tw->tw_year % 100;
454 century = tw->tw_year < 100 ? 19 : tw->tw_year / 100;
464 tw->tw_wday = ((26 * month - 2) / 10 + day + year + year / 4
465 - 3 * century / 4 + 1) % 7;
469 tw->tw_flags &= ~TW_SDAY, tw->tw_flags |= TW_SIMP;
474 ** Copy nmh time structure
478 twscopy(struct tws *tb, struct tws *tw)
480 *tb = *tw; /* struct copy */
483 tb->tw_sec = tw->tw_sec;
484 tb->tw_min = tw->tw_min;
485 tb->tw_hour = tw->tw_hour;
486 tb->tw_mday = tw->tw_mday;
487 tb->tw_mon = tw->tw_mon;
488 tb->tw_year = tw->tw_year;
489 tb->tw_wday = tw->tw_wday;
490 tb->tw_yday = tw->tw_yday;
491 tb->tw_zone = tw->tw_zone;
492 tb->tw_clock = tw->tw_clock;
493 tb->tw_flags = tw->tw_flags;
499 ** Compare two nmh time structures
503 twsort(struct tws *tw1, struct tws *tw2)
507 if (tw1->tw_clock == 0)
509 if (tw2->tw_clock == 0)
512 return ((c1 = tw1->tw_clock) > (c2 = tw2->tw_clock) ? 1
513 : c1 == c2 ? 0 : -1);