]>
diplodocus.org Git - nmh/blob - sbr/dtime.c
3 * dtime.c -- time/date routines
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
10 #include <h/mh.h> /* for snprintf() */
15 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF)
18 extern char *tzname
[];
22 # define abs(a) (a >= 0 ? a : -a)
26 * The number of days in the year, accounting for leap years
29 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
32 "Jan", "Feb", "Mar", "Apr",
33 "May", "Jun", "Jul", "Aug",
34 "Sep", "Oct", "Nov", "Dec",
45 "Sunday", "Monday", "Tuesday",
46 "Wednesday", "Thursday", "Friday",
56 static int dmsize
[] = {
57 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
62 * Get current time (adjusted for local time
63 * zone and daylight savings time) expressed
64 * as nmh "broken-down" time structure.
73 return dlocaltime (&clock
);
78 * Take clock value and return pointer to nmh time structure
79 * containing "broken-down" time. The time is adjusted for
80 * local time zone and daylight savings time.
84 dlocaltime (time_t *clock
)
92 tm
= localtime (clock
);
94 tw
.tw_sec
= tm
->tm_sec
;
95 tw
.tw_min
= tm
->tm_min
;
96 tw
.tw_hour
= tm
->tm_hour
;
97 tw
.tw_mday
= tm
->tm_mday
;
98 tw
.tw_mon
= tm
->tm_mon
;
101 * tm_year is always "year - 1900".
102 * So we correct for this.
104 tw
.tw_year
= tm
->tm_year
+ 1900;
105 tw
.tw_wday
= tm
->tm_wday
;
106 tw
.tw_yday
= tm
->tm_yday
;
108 tw
.tw_flags
= TW_NULL
;
110 tw
.tw_flags
|= TW_DST
;
112 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
113 tw
.tw_zone
= tm
->tm_gmtoff
/ 60;
114 if (tm
->tm_isdst
) /* if DST is in effect */
115 tw
.tw_zone
-= 60; /* reset to normal offset */
118 tw
.tw_zone
= -(timezone
/ 60);
121 tw
.tw_flags
&= ~TW_SDAY
;
122 tw
.tw_flags
|= TW_SEXP
;
123 tw
.tw_flags
&= ~TW_SZONE
;
124 tw
.tw_flags
|= TW_SZEXP
;
126 tw
.tw_clock
= *clock
;
133 * Take clock value and return pointer to nmh time
134 * structure containing "broken-down" time. Time is
135 * expressed in UTC (Coordinated Universal Time).
139 dgmtime (time_t *clock
)
141 static struct tws tw
;
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
;
169 tw
.tw_flags
&= ~TW_SDAY
;
170 tw
.tw_flags
|= TW_SEXP
;
171 tw
.tw_flags
&= ~TW_SZONE
;
172 tw
.tw_flags
|= TW_SZEXP
;
174 tw
.tw_clock
= *clock
;
181 * Using a nmh "broken-down" time structure,
182 * produce a 26-byte date/time string, such as
184 * Tue Jan 14 17:49:03 1992\n\0
188 dctime (struct tws
*tw
)
190 static char buffer
[26];
195 snprintf (buffer
, sizeof(buffer
), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
196 tw_dotw
[tw
->tw_wday
], tw_moty
[tw
->tw_mon
], tw
->tw_mday
,
197 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
,
198 tw
->tw_year
< 100 ? tw
->tw_year
+ 1900 : tw
->tw_year
);
205 * Produce a date/time string of the form
207 * Mon, 16 Jun 1992 15:30:48 -700 (or)
208 * Mon, 16 Jun 1992 15:30:48 EDT
210 * for the current time, as specified by rfc822.
211 * The first form is required by rfc1123.
215 dtimenow (int alpha_timezone
)
220 return dtime (&clock
, alpha_timezone
);
225 * Using a local calendar time value, produce
226 * a date/time string of the form
228 * Mon, 16 Jun 1992 15:30:48 -700 (or)
229 * Mon, 16 Jun 1992 15:30:48 EDT
231 * as specified by rfc822. The first form is required
232 * by rfc1123 for outgoing messages.
236 dtime (time_t *clock
, int alpha_timezone
)
239 /* use alpha-numeric timezones */
240 return dasctime (dlocaltime (clock
), TW_NULL
);
242 /* use numeric timezones */
243 return dasctime (dlocaltime (clock
), TW_ZONE
);
248 * Using a nmh "broken-down" time structure, produce
249 * a date/time string of the form
251 * Mon, 16 Jun 1992 15:30:48 -0700
253 * as specified by rfc822 and rfc1123.
257 dasctime (struct tws
*tw
, int flags
)
260 static char result
[80];
265 /* Display timezone if known */
266 if ((tw
->tw_flags
& TW_SZONE
) == TW_SZNIL
)
269 snprintf(result
, sizeof(result
), " %s", dtimezone(tw
->tw_zone
, tw
->tw_flags
| flags
));
271 snprintf(buffer
, sizeof(buffer
), "%02d %s %0*d %02d:%02d:%02d%s",
272 tw
->tw_mday
, tw_moty
[tw
->tw_mon
],
273 tw
->tw_year
< 100 ? 2 : 4, tw
->tw_year
,
274 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
, result
);
276 if ((tw
->tw_flags
& TW_SDAY
) == TW_SEXP
)
277 snprintf (result
, sizeof(result
), "%s, %s", tw_dotw
[tw
->tw_wday
], buffer
);
279 if ((tw
->tw_flags
& TW_SDAY
) == TW_SNIL
)
280 strncpy (result
, buffer
, sizeof(result
));
282 snprintf (result
, sizeof(result
), "%s (%s)", buffer
, tw_dotw
[tw
->tw_wday
]);
289 * Get the timezone for given offset
293 dtimezone (int offset
, int flags
)
296 static char buffer
[10];
299 mins
= -((-offset
) % 60);
300 hours
= -((-offset
) / 60);
306 if (!(flags
& TW_ZONE
) && mins
== 0) {
308 return ((flags
& TW_DST
) ? tzname
[1] : tzname
[0]);
311 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
314 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
315 snprintf (buffer
, sizeof(buffer
), "%s%02d%02d",
316 offset
< 0 ? "-" : "+", abs (hours
), abs (mins
));
322 * Convert nmh time structure for local "broken-down"
323 * time to calendar time (clock value). This routine
324 * is based on the gtime() routine written by Steven Shafer
325 * at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
329 dmktime (struct tws
*tw
)
331 int i
, sec
, min
, hour
, mday
, mon
, year
;
334 if (tw
->tw_clock
!= 0)
337 if ((sec
= tw
->tw_sec
) < 0 || sec
> 61
338 || (min
= tw
->tw_min
) < 0 || min
> 59
339 || (hour
= tw
->tw_hour
) < 0 || hour
> 23
340 || (mday
= tw
->tw_mday
) < 1 || mday
> 31
341 || (mon
= tw
->tw_mon
+ 1) < 1 || mon
> 12)
342 return (tw
->tw_clock
= (time_t) -1);
353 for (i
= 1970; i
< year
; i
++)
354 result
+= dysize (i
);
355 if (dysize (year
) == 366 && mon
>= 3)
358 result
+= dmsize
[mon
- 1];
360 result
= 24 * result
+ hour
;
361 result
= 60 * result
+ min
;
362 result
= 60 * result
+ sec
;
363 result
-= 60 * tw
->tw_zone
;
364 if (tw
->tw_flags
& TW_DST
)
367 return (tw
->tw_clock
= result
);
372 * Simple calculation of day of the week. Algorithm
373 * used is Zeller's congruence. We assume that
374 * if tw->tw_year < 100, then the century = 19.
378 set_dotw (struct tws
*tw
)
380 int month
, day
, year
, century
;
382 month
= tw
->tw_mon
- 1;
384 year
= tw
->tw_year
% 100;
385 century
= tw
->tw_year
< 100 ? 19 : tw
->tw_year
/ 100;
396 ((26 * month
- 2) / 10 + day
+ year
+ year
/ 4
397 - 3 * century
/ 4 + 1) % 7;
401 tw
->tw_flags
&= ~TW_SDAY
, tw
->tw_flags
|= TW_SIMP
;
406 * Copy nmh time structure
410 twscopy (struct tws
*tb
, struct tws
*tw
)
412 *tb
= *tw
; /* struct copy */
415 tb
->tw_sec
= tw
->tw_sec
;
416 tb
->tw_min
= tw
->tw_min
;
417 tb
->tw_hour
= tw
->tw_hour
;
418 tb
->tw_mday
= tw
->tw_mday
;
419 tb
->tw_mon
= tw
->tw_mon
;
420 tb
->tw_year
= tw
->tw_year
;
421 tb
->tw_wday
= tw
->tw_wday
;
422 tb
->tw_yday
= tw
->tw_yday
;
423 tb
->tw_zone
= tw
->tw_zone
;
424 tb
->tw_clock
= tw
->tw_clock
;
425 tb
->tw_flags
= tw
->tw_flags
;
431 * Compare two nmh time structures
435 twsort (struct tws
*tw1
, struct tws
*tw2
)
439 if (tw1
->tw_clock
== 0)
441 if (tw2
->tw_clock
== 0)
444 return ((c1
= tw1
->tw_clock
) > (c2
= tw2
->tw_clock
) ? 1
445 : c1
== c2
? 0 : -1);