3 * dtime.c -- time/date routines
11 #if !defined(HAVE_TM_GMTOFF) && !defined(HAVE_TZSET)
12 # include <sys/timeb.h>
15 #ifdef TIME_WITH_SYS_TIME
16 # include <sys/time.h>
19 # ifdef HAVE_SYS_TIME_H
20 # include <sys/time.h>
26 #if !defined(HAVE_TM_GMTOFF) && defined(HAVE_TZSET)
29 extern char *tzname[];
33 # define abs(a) (a >= 0 ? a : -a)
37 * The number of days in the year, accounting for leap years
40 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
43 "Jan", "Feb", "Mar", "Apr",
44 "May", "Jun", "Jul", "Aug",
45 "Sep", "Oct", "Nov", "Dec",
56 "Sunday", "Monday", "Tuesday",
57 "Wednesday", "Thursday", "Friday",
67 static struct zone zones[] = {
74 /* RFC1123 specifies do not use military TZs */
107 static int dmsize[] = {
108 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
113 * Get current time (adjusted for local time
114 * zone and daylight savings time) expressed
115 * as nmh "broken-down" time structure.
124 return dlocaltime (&clock);
129 * Take clock value and return pointer to nmh time structure
130 * containing "broken-down" time. The time is adjusted for
131 * local time zone and daylight savings time.
135 dlocaltime (time_t *clock)
137 static struct tws tw;
140 #if !defined(HAVE_TM_GMTOFF) && !defined(HAVE_TZSET)
147 tm = localtime (clock);
149 tw.tw_sec = tm->tm_sec;
150 tw.tw_min = tm->tm_min;
151 tw.tw_hour = tm->tm_hour;
152 tw.tw_mday = tm->tm_mday;
153 tw.tw_mon = tm->tm_mon;
156 * tm_year is always "year - 1900".
157 * So we correct for this.
159 tw.tw_year = tm->tm_year + 1900;
160 tw.tw_wday = tm->tm_wday;
161 tw.tw_yday = tm->tm_yday;
163 tw.tw_flags = TW_NULL;
165 tw.tw_flags |= TW_DST;
167 #ifdef HAVE_TM_GMTOFF
168 tw.tw_zone = tm->tm_gmtoff / 60;
169 if (tm->tm_isdst) /* if DST is in effect */
170 tw.tw_zone -= 60; /* reset to normal offset */
174 tw.tw_zone = -(timezone / 60);
177 tw.tw_zone = -tb.timezone;
181 tw.tw_flags &= ~TW_SDAY;
182 tw.tw_flags |= TW_SEXP;
183 tw.tw_flags &= ~TW_SZONE;
184 tw.tw_flags |= TW_SZEXP;
186 tw.tw_clock = *clock;
193 * Take clock value and return pointer to nmh time
194 * structure containing "broken-down" time. Time is
195 * expressed in UTC (Coordinated Universal Time).
199 dgmtime (time_t *clock)
201 static struct tws tw;
209 tw.tw_sec = tm->tm_sec;
210 tw.tw_min = tm->tm_min;
211 tw.tw_hour = tm->tm_hour;
212 tw.tw_mday = tm->tm_mday;
213 tw.tw_mon = tm->tm_mon;
216 * tm_year is always "year - 1900"
217 * So we correct for this.
219 tw.tw_year = tm->tm_year + 1900;
220 tw.tw_wday = tm->tm_wday;
221 tw.tw_yday = tm->tm_yday;
223 tw.tw_flags = TW_NULL;
225 tw.tw_flags |= TW_DST;
229 tw.tw_flags &= ~TW_SDAY;
230 tw.tw_flags |= TW_SEXP;
231 tw.tw_flags &= ~TW_SZONE;
232 tw.tw_flags |= TW_SZEXP;
234 tw.tw_clock = *clock;
241 * Using a nmh "broken-down" time structure,
242 * produce a 26-byte date/time string, such as
244 * Tue Jan 14 17:49:03 1992\n\0
248 dctime (struct tws *tw)
250 static char buffer[26];
255 snprintf (buffer, sizeof(buffer), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
256 tw_dotw[tw->tw_wday], tw_moty[tw->tw_mon], tw->tw_mday,
257 tw->tw_hour, tw->tw_min, tw->tw_sec,
258 tw->tw_year < 100 ? tw->tw_year + 1900 : tw->tw_year);
265 * Produce a date/time string of the form
267 * Mon, 16 Jun 1992 15:30:48 -700 (or)
268 * Mon, 16 Jun 1992 15:30:48 EDT
270 * for the current time, as specified by rfc822.
271 * The first form is required by rfc1123.
275 dtimenow (int alpha_timezone)
280 return dtime (&clock, alpha_timezone);
285 * Using a local calendar time value, produce
286 * a date/time string of the form
288 * Mon, 16 Jun 1992 15:30:48 -700 (or)
289 * Mon, 16 Jun 1992 15:30:48 EDT
291 * as specified by rfc822. The first form is required
292 * by rfc1123 for outgoing messages.
296 dtime (time_t *clock, int alpha_timezone)
299 /* use alpha-numeric timezones */
300 return dasctime (dlocaltime (clock), TW_NULL);
302 /* use numeric timezones */
303 return dasctime (dlocaltime (clock), TW_ZONE);
308 * Using a nmh "broken-down" time structure, produce
309 * a date/time string of the form
311 * Mon, 16 Jun 1992 15:30:48 -0700
313 * as specified by rfc822 and rfc1123.
317 dasctime (struct tws *tw, int flags)
320 static char result[80];
325 /* Display timezone if known */
326 if ((tw->tw_flags & TW_SZONE) == TW_SZNIL)
329 snprintf(result, sizeof(result), " %s", dtimezone(tw->tw_zone, tw->tw_flags | flags));
331 snprintf(buffer, sizeof(buffer), "%02d %s %0*d %02d:%02d:%02d%s",
332 tw->tw_mday, tw_moty[tw->tw_mon],
333 tw->tw_year < 100 ? 2 : 4, tw->tw_year,
334 tw->tw_hour, tw->tw_min, tw->tw_sec, result);
336 if ((tw->tw_flags & TW_SDAY) == TW_SEXP)
337 snprintf (result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
339 if ((tw->tw_flags & TW_SDAY) == TW_SNIL)
340 strncpy (result, buffer, sizeof(result));
342 snprintf (result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
349 * Get the timezone for given offset
353 dtimezone (int offset, int flags)
357 static char buffer[10];
360 mins = -((-offset) % 60);
361 hours = -((-offset) / 60);
367 if (!(flags & TW_ZONE) && mins == 0) {
368 #if defined(HAVE_TZSET) && defined(HAVE_TZNAME)
370 return ((flags & TW_DST) ? tzname[1] : tzname[0]);
372 for (z = zones; z->std; z++)
373 if (z->shift == hours)
374 return (z->dst && (flags & TW_DST) ? z->dst : z->std);
381 #endif /* defined(DSTXXX) */
382 snprintf (buffer, sizeof(buffer), "%s%02d%02d",
383 offset < 0 ? "-" : "+", abs (hours), abs (mins));
389 * Convert nmh time structure for local "broken-down"
390 * time to calendar time (clock value). This routine
391 * is based on the gtime() routine written by Steven Shafer
392 * at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
396 dmktime (struct tws *tw)
398 int i, sec, min, hour, mday, mon, year;
401 if (tw->tw_clock != 0)
404 if ((sec = tw->tw_sec) < 0 || sec > 61
405 || (min = tw->tw_min) < 0 || min > 59
406 || (hour = tw->tw_hour) < 0 || hour > 23
407 || (mday = tw->tw_mday) < 1 || mday > 31
408 || (mon = tw->tw_mon + 1) < 1 || mon > 12)
409 return (tw->tw_clock = (time_t) -1);
417 for (i = 1970; i < year; i++)
418 result += dysize (i);
419 if (dysize (year) == 366 && mon >= 3)
422 result += dmsize[mon - 1];
424 result = 24 * result + hour;
425 result = 60 * result + min;
426 result = 60 * result + sec;
427 result -= 60 * tw->tw_zone;
428 if (tw->tw_flags & TW_DST)
431 return (tw->tw_clock = result);
436 * Simple calculation of day of the week. Algorithm
437 * used is Zeller's congruence. We assume that
438 * if tw->tw_year < 100, then the century = 19.
442 set_dotw (struct tws *tw)
444 int month, day, year, century;
446 month = tw->tw_mon - 1;
448 year = tw->tw_year % 100;
449 century = tw->tw_year < 100 ? 19 : tw->tw_year / 100;
460 ((26 * month - 2) / 10 + day + year + year / 4
461 - 3 * century / 4 + 1) % 7;
463 tw->tw_flags &= ~TW_SDAY, tw->tw_flags |= TW_SIMP;
468 * Copy nmh time structure
472 twscopy (struct tws *tb, struct tws *tw)
474 *tb = *tw; /* struct copy */
477 tb->tw_sec = tw->tw_sec;
478 tb->tw_min = tw->tw_min;
479 tb->tw_hour = tw->tw_hour;
480 tb->tw_mday = tw->tw_mday;
481 tb->tw_mon = tw->tw_mon;
482 tb->tw_year = tw->tw_year;
483 tb->tw_wday = tw->tw_wday;
484 tb->tw_yday = tw->tw_yday;
485 tb->tw_zone = tw->tw_zone;
486 tb->tw_clock = tw->tw_clock;
487 tb->tw_flags = tw->tw_flags;
493 * Compare two nmh time structures
497 twsort (struct tws *tw1, struct tws *tw2)
501 if (tw1->tw_clock == 0)
503 if (tw2->tw_clock == 0)
506 return ((c1 = tw1->tw_clock) > (c2 = tw2->tw_clock) ? 1
507 : c1 == c2 ? 0 : -1);