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.
10 #include <h/mh.h> /* for snprintf() */
13 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF)
18 ** The number of days in the year, accounting for leap years
21 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
24 "Jan", "Feb", "Mar", "Apr",
25 "May", "Jun", "Jul", "Aug",
26 "Sep", "Oct", "Nov", "Dec",
37 "Sunday", "Monday", "Tuesday",
38 "Wednesday", "Thursday", "Friday",
42 static int dmsize[] = {
43 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
48 ** Get current time (adjusted for local time
49 ** zone and daylight savings time) expressed
50 ** as nmh "broken-down" time structure.
59 return dlocaltime(&clock);
64 ** Take clock value and return pointer to nmh time structure
65 ** containing "broken-down" time. The time is adjusted for
66 ** local time zone and daylight savings time.
70 dlocaltime(time_t *clock)
78 tm = localtime(clock);
80 tw.tw_sec = tm->tm_sec;
81 tw.tw_min = tm->tm_min;
82 tw.tw_hour = tm->tm_hour;
83 tw.tw_mday = tm->tm_mday;
84 tw.tw_mon = tm->tm_mon;
86 /* tm_year is always "year - 1900". So we correct for this. */
87 tw.tw_year = tm->tm_year + 1900;
88 tw.tw_wday = tm->tm_wday;
89 tw.tw_yday = tm->tm_yday;
91 tw.tw_flags = TW_NULL;
93 tw.tw_flags |= TW_DST;
95 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
96 tw.tw_zone = tm->tm_gmtoff / 60;
97 if (tm->tm_isdst) /* if DST is in effect */
98 tw.tw_zone -= 60; /* reset to normal offset */
101 tw.tw_zone = -(timezone / 60);
104 tw.tw_flags &= ~TW_SDAY;
105 tw.tw_flags |= TW_SEXP;
106 tw.tw_flags &= ~TW_SZONE;
107 tw.tw_flags |= TW_SZEXP;
109 tw.tw_clock = *clock;
116 ** Take clock value and return pointer to nmh time
117 ** structure containing "broken-down" time. Time is
118 ** expressed in UTC (Coordinated Universal Time).
122 dgmtime(time_t *clock)
124 static struct tws tw;
132 tw.tw_sec = tm->tm_sec;
133 tw.tw_min = tm->tm_min;
134 tw.tw_hour = tm->tm_hour;
135 tw.tw_mday = tm->tm_mday;
136 tw.tw_mon = tm->tm_mon;
138 /* tm_year is always "year - 1900". So we correct for this. */
139 tw.tw_year = tm->tm_year + 1900;
140 tw.tw_wday = tm->tm_wday;
141 tw.tw_yday = tm->tm_yday;
143 tw.tw_flags = TW_NULL;
145 tw.tw_flags |= TW_DST;
149 tw.tw_flags &= ~TW_SDAY;
150 tw.tw_flags |= TW_SEXP;
151 tw.tw_flags &= ~TW_SZONE;
152 tw.tw_flags |= TW_SZEXP;
154 tw.tw_clock = *clock;
161 ** Using a nmh "broken-down" time structure,
162 ** produce a 26-byte date/time string, such as
164 ** Tue Jan 14 17:49:03 1992\n\0
168 dctime(struct tws *tw)
170 static char buffer[26];
175 snprintf(buffer, sizeof(buffer), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
176 tw_dotw[tw->tw_wday], tw_moty[tw->tw_mon], tw->tw_mday,
177 tw->tw_hour, tw->tw_min, tw->tw_sec,
178 tw->tw_year < 100 ? tw->tw_year + 1900 : tw->tw_year);
185 ** Produce a date/time string of the form
187 ** Mon, 16 Jun 1992 15:30:48 -0700
189 ** for the current time, as specified by rfc822 and rfc1123.
197 return dtime(&clock);
202 ** Using a local calendar time value, produce
203 ** a date/time string of the form
205 ** Mon, 16 Jun 1992 15:30:48 -0700
207 ** as specified by rfc822 and rfc1123.
212 return dasctime(dlocaltime(clock));
217 ** Using a nmh "broken-down" time structure, produce
218 ** a date/time string of the form
220 ** Mon, 16 Jun 1992 15:30:48 -0700
222 ** as specified by rfc822 and rfc1123.
225 dasctime(struct tws *tw)
228 static char result[80];
233 /* Display timezone if known */
234 if ((tw->tw_flags & TW_SZONE) == TW_SZNIL)
237 snprintf(result, sizeof(result), " %s", dtimezone(tw->tw_zone, tw->tw_flags));
239 snprintf(buffer, sizeof(buffer), "%02d %s %0*d %02d:%02d:%02d%s",
240 tw->tw_mday, tw_moty[tw->tw_mon],
241 tw->tw_year < 100 ? 2 : 4, tw->tw_year,
242 tw->tw_hour, tw->tw_min, tw->tw_sec, result);
244 if ((tw->tw_flags & TW_SDAY) == TW_SEXP)
245 snprintf(result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
247 if ((tw->tw_flags & TW_SDAY) == TW_SNIL)
248 strncpy(result, buffer, sizeof(result));
250 snprintf(result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
257 ** Get the timezone for given offset as numeric value.
260 dtimezone(int offset, int flags)
263 static char buffer[10];
266 mins = -((-offset) % 60);
267 hours = -((-offset) / 60);
273 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
276 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
277 snprintf(buffer, sizeof(buffer), "%s%02d%02d",
278 offset < 0 ? "-" : "+", abs(hours), abs(mins));
284 ** Convert nmh time structure for local "broken-down"
285 ** time to calendar time (clock value). This routine
286 ** is based on the gtime() routine written by Steven Shafer
287 ** at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
291 dmktime(struct tws *tw)
293 int i, sec, min, hour, mday, mon, year;
296 if (tw->tw_clock != 0)
299 if ((sec = tw->tw_sec) < 0 || sec > 61
300 || (min = tw->tw_min) < 0 || min > 59
301 || (hour = tw->tw_hour) < 0 || hour > 23
302 || (mday = tw->tw_mday) < 1 || mday > 31
303 || (mon = tw->tw_mon + 1) < 1 || mon > 12)
304 return (tw->tw_clock = (time_t) -1);
315 for (i = 1970; i < year; i++)
317 if (dysize(year) == 366 && mon >= 3)
320 result += dmsize[mon - 1];
322 result = 24 * result + hour;
323 result = 60 * result + min;
324 result = 60 * result + sec;
325 result -= 60 * tw->tw_zone;
326 if (tw->tw_flags & TW_DST)
329 return (tw->tw_clock = result);
334 ** Simple calculation of day of the week. Algorithm
335 ** used is Zeller's congruence. We assume that
336 ** if tw->tw_year < 100, then the century = 19.
340 set_dotw(struct tws *tw)
342 int month, day, year, century;
344 month = tw->tw_mon - 1;
346 year = tw->tw_year % 100;
347 century = tw->tw_year < 100 ? 19 : tw->tw_year / 100;
357 tw->tw_wday = ((26 * month - 2) / 10 + day + year + year / 4
358 - 3 * century / 4 + 1) % 7;
362 tw->tw_flags &= ~TW_SDAY, tw->tw_flags |= TW_SIMP;
367 ** Copy nmh time structure
371 twscopy(struct tws *tb, struct tws *tw)
373 *tb = *tw; /* struct copy */
378 ** Compare two nmh time structures
382 twsort(struct tws *tw1, struct tws *tw2)
386 if (tw1->tw_clock == 0)
388 if (tw2->tw_clock == 0)
391 return ((c1 = tw1->tw_clock) > (c2 = tw2->tw_clock) ? 1
392 : c1 == c2 ? 0 : -1);