]>
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)
20 * The number of days in the year, accounting for leap years
23 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
26 "Jan", "Feb", "Mar", "Apr",
27 "May", "Jun", "Jul", "Aug",
28 "Sep", "Oct", "Nov", "Dec",
39 "Sunday", "Monday", "Tuesday",
40 "Wednesday", "Thursday", "Friday",
50 static int dmsize
[] = {
51 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
56 * Get current time (adjusted for local time
57 * zone and daylight savings time) expressed
58 * as nmh "broken-down" time structure.
67 return dlocaltime (&clock
);
72 * Take clock value and return pointer to nmh time structure
73 * containing "broken-down" time. The time is adjusted for
74 * local time zone and daylight savings time.
78 dlocaltime (time_t *clock
)
86 tm
= localtime (clock
);
88 tw
.tw_sec
= tm
->tm_sec
;
89 tw
.tw_min
= tm
->tm_min
;
90 tw
.tw_hour
= tm
->tm_hour
;
91 tw
.tw_mday
= tm
->tm_mday
;
92 tw
.tw_mon
= tm
->tm_mon
;
95 * tm_year is always "year - 1900".
96 * So we correct for this.
98 tw
.tw_year
= tm
->tm_year
+ 1900;
99 tw
.tw_wday
= tm
->tm_wday
;
100 tw
.tw_yday
= tm
->tm_yday
;
102 tw
.tw_flags
= TW_NULL
;
104 tw
.tw_flags
|= TW_DST
;
106 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
107 tw
.tw_zone
= tm
->tm_gmtoff
/ 60;
108 if (tm
->tm_isdst
) /* if DST is in effect */
109 tw
.tw_zone
-= 60; /* reset to normal offset */
119 tw
.tw_zone
= -(timezone
/ 60);
122 tw
.tw_flags
&= ~TW_SDAY
;
123 tw
.tw_flags
|= TW_SEXP
;
124 tw
.tw_flags
&= ~TW_SZONE
;
125 tw
.tw_flags
|= TW_SZEXP
;
127 tw
.tw_clock
= *clock
;
134 * Take clock value and return pointer to nmh time
135 * structure containing "broken-down" time. Time is
136 * expressed in UTC (Coordinated Universal Time).
140 dgmtime (time_t *clock
)
142 static struct tws tw
;
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
;
170 tw
.tw_flags
&= ~TW_SDAY
;
171 tw
.tw_flags
|= TW_SEXP
;
172 tw
.tw_flags
&= ~TW_SZONE
;
173 tw
.tw_flags
|= TW_SZEXP
;
175 tw
.tw_clock
= *clock
;
182 * Using a nmh "broken-down" time structure,
183 * produce a 26-byte date/time string, such as
185 * Tue Jan 14 17:49:03 1992\n\0
189 dctime (struct tws
*tw
)
191 static char buffer
[26];
196 snprintf (buffer
, sizeof(buffer
), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
197 tw_dotw
[tw
->tw_wday
], tw_moty
[tw
->tw_mon
], tw
->tw_mday
,
198 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
,
199 tw
->tw_year
< 100 ? tw
->tw_year
+ 1900 : tw
->tw_year
);
206 * Produce a date/time string of the form
208 * Mon, 16 Jun 1992 15:30:48 -700 (or)
209 * Mon, 16 Jun 1992 15:30:48 EDT
211 * for the current time, as specified by rfc822.
212 * The first form is required by rfc1123.
216 dtimenow (int alpha_timezone
)
221 return dtime (&clock
, alpha_timezone
);
226 * Using a local calendar time value, produce
227 * a date/time string of the form
229 * Mon, 16 Jun 1992 15:30:48 -700 (or)
230 * Mon, 16 Jun 1992 15:30:48 EDT
232 * as specified by rfc822. The first form is required
233 * by rfc1123 for outgoing messages.
237 dtime (time_t *clock
, int alpha_timezone
)
240 /* use alpha-numeric timezones */
241 return dasctime (dlocaltime (clock
), TW_NULL
);
243 /* use numeric timezones */
244 return dasctime (dlocaltime (clock
), TW_ZONE
);
249 * Using a nmh "broken-down" time structure, produce
250 * a date/time string of the form
252 * Mon, 16 Jun 1992 15:30:48 -0700
254 * as specified by rfc822 and rfc1123.
258 dasctime (struct tws
*tw
, int flags
)
261 static char result
[80];
266 /* Display timezone if known */
267 if ((tw
->tw_flags
& TW_SZONE
) == TW_SZNIL
)
270 snprintf(result
, sizeof(result
), " %s", dtimezone(tw
->tw_zone
, tw
->tw_flags
| flags
));
272 snprintf(buffer
, sizeof(buffer
), "%02d %s %0*d %02d:%02d:%02d%s",
273 tw
->tw_mday
, tw_moty
[tw
->tw_mon
],
274 tw
->tw_year
< 100 ? 2 : 4, tw
->tw_year
,
275 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
, result
);
277 if ((tw
->tw_flags
& TW_SDAY
) == TW_SEXP
)
278 snprintf (result
, sizeof(result
), "%s, %s", tw_dotw
[tw
->tw_wday
], buffer
);
280 if ((tw
->tw_flags
& TW_SDAY
) == TW_SNIL
)
281 strncpy (result
, buffer
, sizeof(result
));
283 snprintf (result
, sizeof(result
), "%s (%s)", buffer
, tw_dotw
[tw
->tw_wday
]);
290 * Get the timezone for given offset.
291 * This used to return a three-letter abbreviation for some offset
292 * values. But not many. Until there's a good way to do that,
293 * return the string representation of the numeric offset.
297 dtimezone (int offset
, int flags
)
300 static char buffer
[10];
303 mins
= -((-offset
) % 60);
304 hours
= -((-offset
) / 60);
310 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
313 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
314 snprintf (buffer
, sizeof(buffer
), "%s%02d%02d",
315 offset
< 0 ? "-" : "+", abs (hours
), abs (mins
));
321 * Convert nmh time structure for local "broken-down"
322 * time to calendar time (clock value). This routine
323 * is based on the gtime() routine written by Steven Shafer
324 * at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
328 dmktime (struct tws
*tw
)
330 int i
, sec
, min
, hour
, mday
, mon
, year
;
333 if (tw
->tw_clock
!= 0)
336 if ((sec
= tw
->tw_sec
) < 0 || sec
> 61
337 || (min
= tw
->tw_min
) < 0 || min
> 59
338 || (hour
= tw
->tw_hour
) < 0 || hour
> 23
339 || (mday
= tw
->tw_mday
) < 1 || mday
> 31
340 || (mon
= tw
->tw_mon
+ 1) < 1 || mon
> 12)
341 return (tw
->tw_clock
= (time_t) -1);
352 for (i
= 1970; i
< year
; i
++)
353 result
+= dysize (i
);
354 if (dysize (year
) == 366 && mon
>= 3)
357 result
+= dmsize
[mon
- 1];
359 result
= 24 * result
+ hour
;
360 result
= 60 * result
+ min
;
361 result
= 60 * result
+ sec
;
362 result
-= 60 * tw
->tw_zone
;
363 if (tw
->tw_flags
& TW_DST
)
366 return (tw
->tw_clock
= result
);
371 * Simple calculation of day of the week. Algorithm
372 * used is Zeller's congruence. We assume that
373 * if tw->tw_year < 100, then the century = 19.
377 set_dotw (struct tws
*tw
)
379 int month
, day
, year
, century
;
381 month
= tw
->tw_mon
- 1;
383 year
= tw
->tw_year
% 100;
384 century
= tw
->tw_year
< 100 ? 19 : tw
->tw_year
/ 100;
395 ((26 * month
- 2) / 10 + day
+ year
+ year
/ 4
396 - 3 * century
/ 4 + 1) % 7;
400 tw
->tw_flags
&= ~TW_SDAY
, tw
->tw_flags
|= TW_SIMP
;
405 * Copy nmh time structure
409 twscopy (struct tws
*tb
, struct tws
*tw
)
411 *tb
= *tw
; /* struct copy */
416 * Compare two nmh time structures
420 twsort (struct tws
*tw1
, struct tws
*tw2
)
424 if (tw1
->tw_clock
== 0)
426 if (tw2
->tw_clock
== 0)
429 return ((c1
= tw1
->tw_clock
) > (c2
= tw2
->tw_clock
) ? 1
430 : c1
== c2
? 0 : -1);