3 * dtime.c -- time/date routines
8 #include "h/mh.h" /* for snprintf() */
12 #if !defined(HAVE_TM_GMTOFF) && !defined(HAVE_TZSET)
13 # include <sys/timeb.h>
16 #ifdef TIME_WITH_SYS_TIME
17 # include <sys/time.h>
20 # ifdef HAVE_SYS_TIME_H
21 # include <sys/time.h>
27 #if !defined(HAVE_TM_GMTOFF) && defined(HAVE_TZSET)
30 extern char *tzname[];
34 # define abs(a) (a >= 0 ? a : -a)
38 * The number of days in the year, accounting for leap years
41 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
44 "Jan", "Feb", "Mar", "Apr",
45 "May", "Jun", "Jul", "Aug",
46 "Sep", "Oct", "Nov", "Dec",
57 "Sunday", "Monday", "Tuesday",
58 "Wednesday", "Thursday", "Friday",
68 static struct zone zones[] = {
75 /* RFC1123 specifies do not use military TZs */
108 static int dmsize[] = {
109 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
114 * Get current time (adjusted for local time
115 * zone and daylight savings time) expressed
116 * as nmh "broken-down" time structure.
125 return dlocaltime (&clock);
130 * Take clock value and return pointer to nmh time structure
131 * containing "broken-down" time. The time is adjusted for
132 * local time zone and daylight savings time.
136 dlocaltime (time_t *clock)
138 static struct tws tw;
141 #if !defined(HAVE_TM_GMTOFF) && !defined(HAVE_TZSET)
148 tm = localtime (clock);
150 tw.tw_sec = tm->tm_sec;
151 tw.tw_min = tm->tm_min;
152 tw.tw_hour = tm->tm_hour;
153 tw.tw_mday = tm->tm_mday;
154 tw.tw_mon = tm->tm_mon;
157 * tm_year is always "year - 1900".
158 * So we correct for this.
160 tw.tw_year = tm->tm_year + 1900;
161 tw.tw_wday = tm->tm_wday;
162 tw.tw_yday = tm->tm_yday;
164 tw.tw_flags = TW_NULL;
166 tw.tw_flags |= TW_DST;
168 #ifdef HAVE_TM_GMTOFF
169 tw.tw_zone = tm->tm_gmtoff / 60;
170 if (tm->tm_isdst) /* if DST is in effect */
171 tw.tw_zone -= 60; /* reset to normal offset */
175 tw.tw_zone = -(timezone / 60);
178 tw.tw_zone = -tb.timezone;
182 tw.tw_flags &= ~TW_SDAY;
183 tw.tw_flags |= TW_SEXP;
184 tw.tw_flags &= ~TW_SZONE;
185 tw.tw_flags |= TW_SZEXP;
187 tw.tw_clock = *clock;
194 * Take clock value and return pointer to nmh time
195 * structure containing "broken-down" time. Time is
196 * expressed in UTC (Coordinated Universal Time).
200 dgmtime (time_t *clock)
202 static struct tws tw;
210 tw.tw_sec = tm->tm_sec;
211 tw.tw_min = tm->tm_min;
212 tw.tw_hour = tm->tm_hour;
213 tw.tw_mday = tm->tm_mday;
214 tw.tw_mon = tm->tm_mon;
217 * tm_year is always "year - 1900"
218 * So we correct for this.
220 tw.tw_year = tm->tm_year + 1900;
221 tw.tw_wday = tm->tm_wday;
222 tw.tw_yday = tm->tm_yday;
224 tw.tw_flags = TW_NULL;
226 tw.tw_flags |= TW_DST;
230 tw.tw_flags &= ~TW_SDAY;
231 tw.tw_flags |= TW_SEXP;
232 tw.tw_flags &= ~TW_SZONE;
233 tw.tw_flags |= TW_SZEXP;
235 tw.tw_clock = *clock;
242 * Using a nmh "broken-down" time structure,
243 * produce a 26-byte date/time string, such as
245 * Tue Jan 14 17:49:03 1992\n\0
249 dctime (struct tws *tw)
251 static char buffer[26];
256 snprintf (buffer, sizeof(buffer), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
257 tw_dotw[tw->tw_wday], tw_moty[tw->tw_mon], tw->tw_mday,
258 tw->tw_hour, tw->tw_min, tw->tw_sec,
259 tw->tw_year < 100 ? tw->tw_year + 1900 : tw->tw_year);
266 * Produce a date/time string of the form
268 * Mon, 16 Jun 1992 15:30:48 -700 (or)
269 * Mon, 16 Jun 1992 15:30:48 EDT
271 * for the current time, as specified by rfc822.
272 * The first form is required by rfc1123.
276 dtimenow (int alpha_timezone)
281 return dtime (&clock, alpha_timezone);
286 * Using a local calendar time value, produce
287 * a date/time string of the form
289 * Mon, 16 Jun 1992 15:30:48 -700 (or)
290 * Mon, 16 Jun 1992 15:30:48 EDT
292 * as specified by rfc822. The first form is required
293 * by rfc1123 for outgoing messages.
297 dtime (time_t *clock, int alpha_timezone)
300 /* use alpha-numeric timezones */
301 return dasctime (dlocaltime (clock), TW_NULL);
303 /* use numeric timezones */
304 return dasctime (dlocaltime (clock), TW_ZONE);
309 * Using a nmh "broken-down" time structure, produce
310 * a date/time string of the form
312 * Mon, 16 Jun 1992 15:30:48 -0700
314 * as specified by rfc822 and rfc1123.
318 dasctime (struct tws *tw, int flags)
321 static char result[80];
326 /* Display timezone if known */
327 if ((tw->tw_flags & TW_SZONE) == TW_SZNIL)
330 snprintf(result, sizeof(result), " %s", dtimezone(tw->tw_zone, tw->tw_flags | flags));
332 snprintf(buffer, sizeof(buffer), "%02d %s %0*d %02d:%02d:%02d%s",
333 tw->tw_mday, tw_moty[tw->tw_mon],
334 tw->tw_year < 100 ? 2 : 4, tw->tw_year,
335 tw->tw_hour, tw->tw_min, tw->tw_sec, result);
337 if ((tw->tw_flags & TW_SDAY) == TW_SEXP)
338 snprintf (result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
340 if ((tw->tw_flags & TW_SDAY) == TW_SNIL)
341 strncpy (result, buffer, sizeof(result));
343 snprintf (result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
350 * Get the timezone for given offset
354 dtimezone (int offset, int flags)
358 static char buffer[10];
361 mins = -((-offset) % 60);
362 hours = -((-offset) / 60);
368 if (!(flags & TW_ZONE) && mins == 0) {
369 #if defined(HAVE_TZSET) && defined(HAVE_TZNAME)
371 return ((flags & TW_DST) ? tzname[1] : tzname[0]);
373 for (z = zones; z->std; z++)
374 if (z->shift == hours)
375 return (z->dst && (flags & TW_DST) ? z->dst : z->std);
382 #endif /* defined(DSTXXX) */
383 snprintf (buffer, sizeof(buffer), "%s%02d%02d",
384 offset < 0 ? "-" : "+", abs (hours), abs (mins));
390 * Convert nmh time structure for local "broken-down"
391 * time to calendar time (clock value). This routine
392 * is based on the gtime() routine written by Steven Shafer
393 * at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
397 dmktime (struct tws *tw)
399 int i, sec, min, hour, mday, mon, year;
402 if (tw->tw_clock != 0)
405 if ((sec = tw->tw_sec) < 0 || sec > 61
406 || (min = tw->tw_min) < 0 || min > 59
407 || (hour = tw->tw_hour) < 0 || hour > 23
408 || (mday = tw->tw_mday) < 1 || mday > 31
409 || (mon = tw->tw_mon + 1) < 1 || mon > 12)
410 return (tw->tw_clock = (time_t) -1);
418 for (i = 1970; i < year; i++)
419 result += dysize (i);
420 if (dysize (year) == 366 && mon >= 3)
423 result += dmsize[mon - 1];
425 result = 24 * result + hour;
426 result = 60 * result + min;
427 result = 60 * result + sec;
428 result -= 60 * tw->tw_zone;
429 if (tw->tw_flags & TW_DST)
432 return (tw->tw_clock = result);
437 * Simple calculation of day of the week. Algorithm
438 * used is Zeller's congruence. We assume that
439 * if tw->tw_year < 100, then the century = 19.
443 set_dotw (struct tws *tw)
445 int month, day, year, century;
447 month = tw->tw_mon - 1;
449 year = tw->tw_year % 100;
450 century = tw->tw_year < 100 ? 19 : tw->tw_year / 100;
461 ((26 * month - 2) / 10 + day + year + year / 4
462 - 3 * century / 4 + 1) % 7;
466 tw->tw_flags &= ~TW_SDAY, tw->tw_flags |= TW_SIMP;
471 * Copy nmh time structure
475 twscopy (struct tws *tb, struct tws *tw)
477 *tb = *tw; /* struct copy */
480 tb->tw_sec = tw->tw_sec;
481 tb->tw_min = tw->tw_min;
482 tb->tw_hour = tw->tw_hour;
483 tb->tw_mday = tw->tw_mday;
484 tb->tw_mon = tw->tw_mon;
485 tb->tw_year = tw->tw_year;
486 tb->tw_wday = tw->tw_wday;
487 tb->tw_yday = tw->tw_yday;
488 tb->tw_zone = tw->tw_zone;
489 tb->tw_clock = tw->tw_clock;
490 tb->tw_flags = tw->tw_flags;
496 * Compare two nmh time structures
500 twsort (struct tws *tw1, struct tws *tw2)
504 if (tw1->tw_clock == 0)
506 if (tw2->tw_clock == 0)
509 return ((c1 = tw1->tw_clock) > (c2 = tw2->tw_clock) ? 1
510 : c1 == c2 ? 0 : -1);