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",
48 static int dmsize[] = {
49 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
54 ** Get current time (adjusted for local time
55 ** zone and daylight savings time) expressed
56 ** as nmh "broken-down" time structure.
65 return dlocaltime(&clock);
70 ** Take clock value and return pointer to nmh time structure
71 ** containing "broken-down" time. The time is adjusted for
72 ** local time zone and daylight savings time.
76 dlocaltime(time_t *clock)
84 tm = localtime(clock);
86 tw.tw_sec = tm->tm_sec;
87 tw.tw_min = tm->tm_min;
88 tw.tw_hour = tm->tm_hour;
89 tw.tw_mday = tm->tm_mday;
90 tw.tw_mon = tm->tm_mon;
92 /* tm_year is always "year - 1900". So we correct for this. */
93 tw.tw_year = tm->tm_year + 1900;
94 tw.tw_wday = tm->tm_wday;
95 tw.tw_yday = tm->tm_yday;
97 tw.tw_flags = TW_NULL;
99 tw.tw_flags |= TW_DST;
101 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
102 tw.tw_zone = tm->tm_gmtoff / 60;
103 if (tm->tm_isdst) /* if DST is in effect */
104 tw.tw_zone -= 60; /* reset to normal offset */
107 tw.tw_zone = -(timezone / 60);
110 tw.tw_flags &= ~TW_SDAY;
111 tw.tw_flags |= TW_SEXP;
112 tw.tw_flags &= ~TW_SZONE;
113 tw.tw_flags |= TW_SZEXP;
115 tw.tw_clock = *clock;
122 ** Take clock value and return pointer to nmh time
123 ** structure containing "broken-down" time. Time is
124 ** expressed in UTC (Coordinated Universal Time).
128 dgmtime(time_t *clock)
130 static struct tws tw;
138 tw.tw_sec = tm->tm_sec;
139 tw.tw_min = tm->tm_min;
140 tw.tw_hour = tm->tm_hour;
141 tw.tw_mday = tm->tm_mday;
142 tw.tw_mon = tm->tm_mon;
144 /* tm_year is always "year - 1900". So we correct for this. */
145 tw.tw_year = tm->tm_year + 1900;
146 tw.tw_wday = tm->tm_wday;
147 tw.tw_yday = tm->tm_yday;
149 tw.tw_flags = TW_NULL;
151 tw.tw_flags |= TW_DST;
155 tw.tw_flags &= ~TW_SDAY;
156 tw.tw_flags |= TW_SEXP;
157 tw.tw_flags &= ~TW_SZONE;
158 tw.tw_flags |= TW_SZEXP;
160 tw.tw_clock = *clock;
167 ** Using a nmh "broken-down" time structure,
168 ** produce a 26-byte date/time string, such as
170 ** Tue Jan 14 17:49:03 1992\n\0
174 dctime(struct tws *tw)
176 static char buffer[26];
181 snprintf(buffer, sizeof(buffer), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
182 tw_dotw[tw->tw_wday], tw_moty[tw->tw_mon], tw->tw_mday,
183 tw->tw_hour, tw->tw_min, tw->tw_sec,
184 tw->tw_year < 100 ? tw->tw_year + 1900 : tw->tw_year);
191 ** Produce a date/time string of the form
193 ** Mon, 16 Jun 1992 15:30:48 -0700
195 ** for the current time, as specified by rfc822 and rfc1123.
203 return dtime(&clock);
208 ** Using a local calendar time value, produce
209 ** a date/time string of the form
211 ** Mon, 16 Jun 1992 15:30:48 -0700
213 ** as specified by rfc822 and rfc1123.
218 return dasctime(dlocaltime(clock));
223 ** Using a nmh "broken-down" time structure, produce
224 ** a date/time string of the form
226 ** Mon, 16 Jun 1992 15:30:48 -0700
228 ** as specified by rfc822 and rfc1123.
231 dasctime(struct tws *tw)
234 static char result[80];
239 /* Display timezone if known */
240 if ((tw->tw_flags & TW_SZONE) == TW_SZNIL)
243 snprintf(result, sizeof(result), " %s", dtimezone(tw->tw_zone, tw->tw_flags));
245 snprintf(buffer, sizeof(buffer), "%02d %s %0*d %02d:%02d:%02d%s",
246 tw->tw_mday, tw_moty[tw->tw_mon],
247 tw->tw_year < 100 ? 2 : 4, tw->tw_year,
248 tw->tw_hour, tw->tw_min, tw->tw_sec, result);
250 if ((tw->tw_flags & TW_SDAY) == TW_SEXP)
251 snprintf(result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
253 if ((tw->tw_flags & TW_SDAY) == TW_SNIL)
254 strncpy(result, buffer, sizeof(result));
256 snprintf(result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
263 ** Get the timezone for given offset as numeric value.
266 dtimezone(int offset, int flags)
269 static char buffer[10];
272 mins = -((-offset) % 60);
273 hours = -((-offset) / 60);
279 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
282 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
283 snprintf(buffer, sizeof(buffer), "%s%02d%02d",
284 offset < 0 ? "-" : "+", abs(hours), abs(mins));
290 ** Convert nmh time structure for local "broken-down"
291 ** time to calendar time (clock value). This routine
292 ** is based on the gtime() routine written by Steven Shafer
293 ** at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
297 dmktime(struct tws *tw)
299 int i, sec, min, hour, mday, mon, year;
302 if (tw->tw_clock != 0)
305 if ((sec = tw->tw_sec) < 0 || sec > 61
306 || (min = tw->tw_min) < 0 || min > 59
307 || (hour = tw->tw_hour) < 0 || hour > 23
308 || (mday = tw->tw_mday) < 1 || mday > 31
309 || (mon = tw->tw_mon + 1) < 1 || mon > 12)
310 return (tw->tw_clock = (time_t) -1);
321 for (i = 1970; i < year; i++)
323 if (dysize(year) == 366 && mon >= 3)
326 result += dmsize[mon - 1];
328 result = 24 * result + hour;
329 result = 60 * result + min;
330 result = 60 * result + sec;
331 result -= 60 * tw->tw_zone;
332 if (tw->tw_flags & TW_DST)
335 return (tw->tw_clock = result);
340 ** Simple calculation of day of the week. Algorithm
341 ** used is Zeller's congruence. We assume that
342 ** if tw->tw_year < 100, then the century = 19.
346 set_dotw(struct tws *tw)
348 int month, day, year, century;
350 month = tw->tw_mon - 1;
352 year = tw->tw_year % 100;
353 century = tw->tw_year < 100 ? 19 : tw->tw_year / 100;
363 tw->tw_wday = ((26 * month - 2) / 10 + day + year + year / 4
364 - 3 * century / 4 + 1) % 7;
368 tw->tw_flags &= ~TW_SDAY, tw->tw_flags |= TW_SIMP;
373 ** Copy nmh time structure
377 twscopy(struct tws *tb, struct tws *tw)
379 *tb = *tw; /* struct copy */
384 ** Compare two nmh time structures
388 twsort(struct tws *tw1, struct tws *tw2)
392 if (tw1->tw_clock == 0)
394 if (tw2->tw_clock == 0)
397 return ((c1 = tw1->tw_clock) > (c2 = tw2->tw_clock) ? 1
398 : c1 == c2 ? 0 : -1);