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() */
14 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF) && !defined(HAVE_TZSET)
15 # include <sys/timeb.h>
18 #ifdef TIME_WITH_SYS_TIME
19 # include <sys/time.h>
22 # ifdef HAVE_SYS_TIME_H
23 # include <sys/time.h>
29 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF) && defined(HAVE_TZSET)
32 extern char *tzname[];
36 # define abs(a) (a >= 0 ? a : -a)
40 * The number of days in the year, accounting for leap years
43 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
46 "Jan", "Feb", "Mar", "Apr",
47 "May", "Jun", "Jul", "Aug",
48 "Sep", "Oct", "Nov", "Dec",
59 "Sunday", "Monday", "Tuesday",
60 "Wednesday", "Thursday", "Friday",
70 static struct zone zones[] = {
77 /* RFC1123 specifies do not use military TZs */
110 static int dmsize[] = {
111 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
116 * Get current time (adjusted for local time
117 * zone and daylight savings time) expressed
118 * as nmh "broken-down" time structure.
127 return dlocaltime (&clock);
132 * Take clock value and return pointer to nmh time structure
133 * containing "broken-down" time. The time is adjusted for
134 * local time zone and daylight savings time.
138 dlocaltime (time_t *clock)
140 static struct tws tw;
143 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF) && !defined(HAVE_TZSET)
150 tm = localtime (clock);
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;
170 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
171 tw.tw_zone = tm->tm_gmtoff / 60;
172 if (tm->tm_isdst) /* if DST is in effect */
173 tw.tw_zone -= 60; /* reset to normal offset */
177 tw.tw_zone = -(timezone / 60);
180 tw.tw_zone = -tb.timezone;
184 tw.tw_flags &= ~TW_SDAY;
185 tw.tw_flags |= TW_SEXP;
186 tw.tw_flags &= ~TW_SZONE;
187 tw.tw_flags |= TW_SZEXP;
189 tw.tw_clock = *clock;
196 * Take clock value and return pointer to nmh time
197 * structure containing "broken-down" time. Time is
198 * expressed in UTC (Coordinated Universal Time).
202 dgmtime (time_t *clock)
204 static struct tws tw;
212 tw.tw_sec = tm->tm_sec;
213 tw.tw_min = tm->tm_min;
214 tw.tw_hour = tm->tm_hour;
215 tw.tw_mday = tm->tm_mday;
216 tw.tw_mon = tm->tm_mon;
219 * tm_year is always "year - 1900"
220 * So we correct for this.
222 tw.tw_year = tm->tm_year + 1900;
223 tw.tw_wday = tm->tm_wday;
224 tw.tw_yday = tm->tm_yday;
226 tw.tw_flags = TW_NULL;
228 tw.tw_flags |= TW_DST;
232 tw.tw_flags &= ~TW_SDAY;
233 tw.tw_flags |= TW_SEXP;
234 tw.tw_flags &= ~TW_SZONE;
235 tw.tw_flags |= TW_SZEXP;
237 tw.tw_clock = *clock;
244 * Using a nmh "broken-down" time structure,
245 * produce a 26-byte date/time string, such as
247 * Tue Jan 14 17:49:03 1992\n\0
251 dctime (struct tws *tw)
253 static char buffer[26];
258 snprintf (buffer, sizeof(buffer), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
259 tw_dotw[tw->tw_wday], tw_moty[tw->tw_mon], tw->tw_mday,
260 tw->tw_hour, tw->tw_min, tw->tw_sec,
261 tw->tw_year < 100 ? tw->tw_year + 1900 : tw->tw_year);
268 * Produce a date/time string of the form
270 * Mon, 16 Jun 1992 15:30:48 -700 (or)
271 * Mon, 16 Jun 1992 15:30:48 EDT
273 * for the current time, as specified by rfc822.
274 * The first form is required by rfc1123.
278 dtimenow (int alpha_timezone)
283 return dtime (&clock, alpha_timezone);
288 * Using a local calendar time value, produce
289 * a date/time string of the form
291 * Mon, 16 Jun 1992 15:30:48 -700 (or)
292 * Mon, 16 Jun 1992 15:30:48 EDT
294 * as specified by rfc822. The first form is required
295 * by rfc1123 for outgoing messages.
299 dtime (time_t *clock, int alpha_timezone)
302 /* use alpha-numeric timezones */
303 return dasctime (dlocaltime (clock), TW_NULL);
305 /* use numeric timezones */
306 return dasctime (dlocaltime (clock), TW_ZONE);
311 * Using a nmh "broken-down" time structure, produce
312 * a date/time string of the form
314 * Mon, 16 Jun 1992 15:30:48 -0700
316 * as specified by rfc822 and rfc1123.
320 dasctime (struct tws *tw, int flags)
323 static char result[80];
328 /* Display timezone if known */
329 if ((tw->tw_flags & TW_SZONE) == TW_SZNIL)
332 snprintf(result, sizeof(result), " %s", dtimezone(tw->tw_zone, tw->tw_flags | flags));
334 snprintf(buffer, sizeof(buffer), "%02d %s %0*d %02d:%02d:%02d%s",
335 tw->tw_mday, tw_moty[tw->tw_mon],
336 tw->tw_year < 100 ? 2 : 4, tw->tw_year,
337 tw->tw_hour, tw->tw_min, tw->tw_sec, result);
339 if ((tw->tw_flags & TW_SDAY) == TW_SEXP)
340 snprintf (result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
342 if ((tw->tw_flags & TW_SDAY) == TW_SNIL)
343 strncpy (result, buffer, sizeof(result));
345 snprintf (result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
352 * Get the timezone for given offset
356 dtimezone (int offset, int flags)
360 static char buffer[10];
363 mins = -((-offset) % 60);
364 hours = -((-offset) / 60);
370 if (!(flags & TW_ZONE) && mins == 0) {
371 #if defined(HAVE_TZSET) && defined(HAVE_TZNAME)
373 return ((flags & TW_DST) ? tzname[1] : tzname[0]);
375 for (z = zones; z->std; z++)
376 if (z->shift == hours)
377 return (z->dst && (flags & TW_DST) ? z->dst : z->std);
381 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
384 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
385 snprintf (buffer, sizeof(buffer), "%s%02d%02d",
386 offset < 0 ? "-" : "+", abs (hours), abs (mins));
392 * Convert nmh time structure for local "broken-down"
393 * time to calendar time (clock value). This routine
394 * is based on the gtime() routine written by Steven Shafer
395 * at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
399 dmktime (struct tws *tw)
401 int i, sec, min, hour, mday, mon, year;
404 if (tw->tw_clock != 0)
407 if ((sec = tw->tw_sec) < 0 || sec > 61
408 || (min = tw->tw_min) < 0 || min > 59
409 || (hour = tw->tw_hour) < 0 || hour > 23
410 || (mday = tw->tw_mday) < 1 || mday > 31
411 || (mon = tw->tw_mon + 1) < 1 || mon > 12)
412 return (tw->tw_clock = (time_t) -1);
423 for (i = 1970; i < year; i++)
424 result += dysize (i);
425 if (dysize (year) == 366 && mon >= 3)
428 result += dmsize[mon - 1];
430 result = 24 * result + hour;
431 result = 60 * result + min;
432 result = 60 * result + sec;
433 result -= 60 * tw->tw_zone;
434 if (tw->tw_flags & TW_DST)
437 return (tw->tw_clock = result);
442 * Simple calculation of day of the week. Algorithm
443 * used is Zeller's congruence. We assume that
444 * if tw->tw_year < 100, then the century = 19.
448 set_dotw (struct tws *tw)
450 int month, day, year, century;
452 month = tw->tw_mon - 1;
454 year = tw->tw_year % 100;
455 century = tw->tw_year < 100 ? 19 : tw->tw_year / 100;
466 ((26 * month - 2) / 10 + day + year + year / 4
467 - 3 * century / 4 + 1) % 7;
471 tw->tw_flags &= ~TW_SDAY, tw->tw_flags |= TW_SIMP;
476 * Copy nmh time structure
480 twscopy (struct tws *tb, struct tws *tw)
482 *tb = *tw; /* struct copy */
485 tb->tw_sec = tw->tw_sec;
486 tb->tw_min = tw->tw_min;
487 tb->tw_hour = tw->tw_hour;
488 tb->tw_mday = tw->tw_mday;
489 tb->tw_mon = tw->tw_mon;
490 tb->tw_year = tw->tw_year;
491 tb->tw_wday = tw->tw_wday;
492 tb->tw_yday = tw->tw_yday;
493 tb->tw_zone = tw->tw_zone;
494 tb->tw_clock = tw->tw_clock;
495 tb->tw_flags = tw->tw_flags;
501 * Compare two nmh time structures
505 twsort (struct tws *tw1, struct tws *tw2)
509 if (tw1->tw_clock == 0)
511 if (tw2->tw_clock == 0)
514 return ((c1 = tw1->tw_clock) > (c2 = tw2->tw_clock) ? 1
515 : c1 == c2 ? 0 : -1);