]>
diplodocus.org Git - nmh/blob - sbr/dtime.c
3 * dtime.c -- time/date routines
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
12 #include <h/mh.h> /* for snprintf() */
16 #if !defined(HAVE_TM_GMTOFF) && !defined(HAVE_TZSET)
17 # include <sys/timeb.h>
20 #ifdef TIME_WITH_SYS_TIME
21 # include <sys/time.h>
24 # ifdef HAVE_SYS_TIME_H
25 # include <sys/time.h>
31 #if !defined(HAVE_TM_GMTOFF) && defined(HAVE_TZSET)
34 extern char *tzname
[];
38 # define abs(a) (a >= 0 ? a : -a)
42 * The number of days in the year, accounting for leap years
45 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
48 "Jan", "Feb", "Mar", "Apr",
49 "May", "Jun", "Jul", "Aug",
50 "Sep", "Oct", "Nov", "Dec",
61 "Sunday", "Monday", "Tuesday",
62 "Wednesday", "Thursday", "Friday",
72 static struct zone zones
[] = {
79 /* RFC1123 specifies do not use military TZs */
112 static int dmsize
[] = {
113 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
118 * Get current time (adjusted for local time
119 * zone and daylight savings time) expressed
120 * as nmh "broken-down" time structure.
129 return dlocaltime (&clock
);
134 * Take clock value and return pointer to nmh time structure
135 * containing "broken-down" time. The time is adjusted for
136 * local time zone and daylight savings time.
140 dlocaltime (time_t *clock
)
142 static struct tws tw
;
145 #if !defined(HAVE_TM_GMTOFF) && !defined(HAVE_TZSET)
152 tm
= localtime (clock
);
154 tw
.tw_sec
= tm
->tm_sec
;
155 tw
.tw_min
= tm
->tm_min
;
156 tw
.tw_hour
= tm
->tm_hour
;
157 tw
.tw_mday
= tm
->tm_mday
;
158 tw
.tw_mon
= tm
->tm_mon
;
161 * tm_year is always "year - 1900".
162 * So we correct for this.
164 tw
.tw_year
= tm
->tm_year
+ 1900;
165 tw
.tw_wday
= tm
->tm_wday
;
166 tw
.tw_yday
= tm
->tm_yday
;
168 tw
.tw_flags
= TW_NULL
;
170 tw
.tw_flags
|= TW_DST
;
172 #ifdef HAVE_TM_GMTOFF
173 tw
.tw_zone
= tm
->tm_gmtoff
/ 60;
174 if (tm
->tm_isdst
) /* if DST is in effect */
175 tw
.tw_zone
-= 60; /* reset to normal offset */
179 tw
.tw_zone
= -(timezone
/ 60);
182 tw
.tw_zone
= -tb
.timezone
;
186 tw
.tw_flags
&= ~TW_SDAY
;
187 tw
.tw_flags
|= TW_SEXP
;
188 tw
.tw_flags
&= ~TW_SZONE
;
189 tw
.tw_flags
|= TW_SZEXP
;
191 tw
.tw_clock
= *clock
;
198 * Take clock value and return pointer to nmh time
199 * structure containing "broken-down" time. Time is
200 * expressed in UTC (Coordinated Universal Time).
204 dgmtime (time_t *clock
)
206 static struct tws tw
;
214 tw
.tw_sec
= tm
->tm_sec
;
215 tw
.tw_min
= tm
->tm_min
;
216 tw
.tw_hour
= tm
->tm_hour
;
217 tw
.tw_mday
= tm
->tm_mday
;
218 tw
.tw_mon
= tm
->tm_mon
;
221 * tm_year is always "year - 1900"
222 * So we correct for this.
224 tw
.tw_year
= tm
->tm_year
+ 1900;
225 tw
.tw_wday
= tm
->tm_wday
;
226 tw
.tw_yday
= tm
->tm_yday
;
228 tw
.tw_flags
= TW_NULL
;
230 tw
.tw_flags
|= TW_DST
;
234 tw
.tw_flags
&= ~TW_SDAY
;
235 tw
.tw_flags
|= TW_SEXP
;
236 tw
.tw_flags
&= ~TW_SZONE
;
237 tw
.tw_flags
|= TW_SZEXP
;
239 tw
.tw_clock
= *clock
;
246 * Using a nmh "broken-down" time structure,
247 * produce a 26-byte date/time string, such as
249 * Tue Jan 14 17:49:03 1992\n\0
253 dctime (struct tws
*tw
)
255 static char buffer
[26];
260 snprintf (buffer
, sizeof(buffer
), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
261 tw_dotw
[tw
->tw_wday
], tw_moty
[tw
->tw_mon
], tw
->tw_mday
,
262 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
,
263 tw
->tw_year
< 100 ? tw
->tw_year
+ 1900 : tw
->tw_year
);
270 * Produce a date/time string of the form
272 * Mon, 16 Jun 1992 15:30:48 -700 (or)
273 * Mon, 16 Jun 1992 15:30:48 EDT
275 * for the current time, as specified by rfc822.
276 * The first form is required by rfc1123.
280 dtimenow (int alpha_timezone
)
285 return dtime (&clock
, alpha_timezone
);
290 * Using a local calendar time value, produce
291 * a date/time string of the form
293 * Mon, 16 Jun 1992 15:30:48 -700 (or)
294 * Mon, 16 Jun 1992 15:30:48 EDT
296 * as specified by rfc822. The first form is required
297 * by rfc1123 for outgoing messages.
301 dtime (time_t *clock
, int alpha_timezone
)
304 /* use alpha-numeric timezones */
305 return dasctime (dlocaltime (clock
), TW_NULL
);
307 /* use numeric timezones */
308 return dasctime (dlocaltime (clock
), TW_ZONE
);
313 * Using a nmh "broken-down" time structure, produce
314 * a date/time string of the form
316 * Mon, 16 Jun 1992 15:30:48 -0700
318 * as specified by rfc822 and rfc1123.
322 dasctime (struct tws
*tw
, int flags
)
325 static char result
[80];
330 /* Display timezone if known */
331 if ((tw
->tw_flags
& TW_SZONE
) == TW_SZNIL
)
334 snprintf(result
, sizeof(result
), " %s", dtimezone(tw
->tw_zone
, tw
->tw_flags
| flags
));
336 snprintf(buffer
, sizeof(buffer
), "%02d %s %0*d %02d:%02d:%02d%s",
337 tw
->tw_mday
, tw_moty
[tw
->tw_mon
],
338 tw
->tw_year
< 100 ? 2 : 4, tw
->tw_year
,
339 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
, result
);
341 if ((tw
->tw_flags
& TW_SDAY
) == TW_SEXP
)
342 snprintf (result
, sizeof(result
), "%s, %s", tw_dotw
[tw
->tw_wday
], buffer
);
344 if ((tw
->tw_flags
& TW_SDAY
) == TW_SNIL
)
345 strncpy (result
, buffer
, sizeof(result
));
347 snprintf (result
, sizeof(result
), "%s (%s)", buffer
, tw_dotw
[tw
->tw_wday
]);
354 * Get the timezone for given offset
358 dtimezone (int offset
, int flags
)
362 static char buffer
[10];
365 mins
= -((-offset
) % 60);
366 hours
= -((-offset
) / 60);
372 if (!(flags
& TW_ZONE
) && mins
== 0) {
373 #if defined(HAVE_TZSET) && defined(HAVE_TZNAME)
375 return ((flags
& TW_DST
) ? tzname
[1] : tzname
[0]);
377 for (z
= zones
; z
->std
; z
++)
378 if (z
->shift
== hours
)
379 return (z
->dst
&& (flags
& TW_DST
) ? z
->dst
: z
->std
);
383 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
386 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
387 snprintf (buffer
, sizeof(buffer
), "%s%02d%02d",
388 offset
< 0 ? "-" : "+", abs (hours
), abs (mins
));
394 * Convert nmh time structure for local "broken-down"
395 * time to calendar time (clock value). This routine
396 * is based on the gtime() routine written by Steven Shafer
397 * at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
401 dmktime (struct tws
*tw
)
403 int i
, sec
, min
, hour
, mday
, mon
, year
;
406 if (tw
->tw_clock
!= 0)
409 if ((sec
= tw
->tw_sec
) < 0 || sec
> 61
410 || (min
= tw
->tw_min
) < 0 || min
> 59
411 || (hour
= tw
->tw_hour
) < 0 || hour
> 23
412 || (mday
= tw
->tw_mday
) < 1 || mday
> 31
413 || (mon
= tw
->tw_mon
+ 1) < 1 || mon
> 12)
414 return (tw
->tw_clock
= (time_t) -1);
425 for (i
= 1970; i
< year
; i
++)
426 result
+= dysize (i
);
427 if (dysize (year
) == 366 && mon
>= 3)
430 result
+= dmsize
[mon
- 1];
432 result
= 24 * result
+ hour
;
433 result
= 60 * result
+ min
;
434 result
= 60 * result
+ sec
;
435 result
-= 60 * tw
->tw_zone
;
436 if (tw
->tw_flags
& TW_DST
)
439 return (tw
->tw_clock
= result
);
444 * Simple calculation of day of the week. Algorithm
445 * used is Zeller's congruence. We assume that
446 * if tw->tw_year < 100, then the century = 19.
450 set_dotw (struct tws
*tw
)
452 int month
, day
, year
, century
;
454 month
= tw
->tw_mon
- 1;
456 year
= tw
->tw_year
% 100;
457 century
= tw
->tw_year
< 100 ? 19 : tw
->tw_year
/ 100;
468 ((26 * month
- 2) / 10 + day
+ year
+ year
/ 4
469 - 3 * century
/ 4 + 1) % 7;
473 tw
->tw_flags
&= ~TW_SDAY
, tw
->tw_flags
|= TW_SIMP
;
478 * Copy nmh time structure
482 twscopy (struct tws
*tb
, struct tws
*tw
)
484 *tb
= *tw
; /* struct copy */
487 tb
->tw_sec
= tw
->tw_sec
;
488 tb
->tw_min
= tw
->tw_min
;
489 tb
->tw_hour
= tw
->tw_hour
;
490 tb
->tw_mday
= tw
->tw_mday
;
491 tb
->tw_mon
= tw
->tw_mon
;
492 tb
->tw_year
= tw
->tw_year
;
493 tb
->tw_wday
= tw
->tw_wday
;
494 tb
->tw_yday
= tw
->tw_yday
;
495 tb
->tw_zone
= tw
->tw_zone
;
496 tb
->tw_clock
= tw
->tw_clock
;
497 tb
->tw_flags
= tw
->tw_flags
;
503 * Compare two nmh time structures
507 twsort (struct tws
*tw1
, struct tws
*tw2
)
511 if (tw1
->tw_clock
== 0)
513 if (tw2
->tw_clock
== 0)
516 return ((c1
= tw1
->tw_clock
) > (c2
= tw2
->tw_clock
) ? 1
517 : c1
== c2
? 0 : -1);