]>
diplodocus.org Git - nmh/blob - sbr/dtime.c
1 /* dtime.c -- time/date routines
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
8 #include <h/mh.h> /* for snprintf() */
13 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF)
18 * The number of days in the year, accounting for leap years
21 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
24 "Jan", "Feb", "Mar", "Apr",
25 "May", "Jun", "Jul", "Aug",
26 "Sep", "Oct", "Nov", "Dec",
37 "Sunday", "Monday", "Tuesday",
38 "Wednesday", "Thursday", "Friday",
42 static int dmsize
[] = {
43 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
48 * Get current time (adjusted for local time
49 * zone and daylight savings time) expressed
50 * as nmh "broken-down" time structure.
59 return dlocaltime (&clock
);
64 * Take clock value and return pointer to nmh time structure
65 * containing "broken-down" time. The time is adjusted for
66 * local time zone and daylight savings time.
70 dlocaltime (time_t *clock
)
78 tm
= localtime (clock
);
80 tw
.tw_sec
= tm
->tm_sec
;
81 tw
.tw_min
= tm
->tm_min
;
82 tw
.tw_hour
= tm
->tm_hour
;
83 tw
.tw_mday
= tm
->tm_mday
;
84 tw
.tw_mon
= tm
->tm_mon
;
87 * tm_year is always "year - 1900".
88 * So we correct for this.
90 tw
.tw_year
= tm
->tm_year
+ 1900;
91 tw
.tw_wday
= tm
->tm_wday
;
92 tw
.tw_yday
= tm
->tm_yday
;
94 tw
.tw_flags
= TW_NULL
;
96 tw
.tw_flags
|= TW_DST
;
98 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
99 tw
.tw_zone
= tm
->tm_gmtoff
/ 60;
100 if (tm
->tm_isdst
) /* if DST is in effect */
101 tw
.tw_zone
-= 60; /* reset to normal offset */
111 tw
.tw_zone
= -(timezone
/ 60);
114 tw
.tw_flags
&= ~TW_SDAY
;
115 tw
.tw_flags
|= TW_SEXP
;
116 tw
.tw_flags
|= TW_SZEXP
;
118 tw
.tw_clock
= *clock
;
125 * Take clock value and return pointer to nmh time
126 * structure containing "broken-down" time. Time is
127 * expressed in UTC (Coordinated Universal Time).
131 dgmtime (time_t *clock
)
133 static struct tws tw
;
141 tw
.tw_sec
= tm
->tm_sec
;
142 tw
.tw_min
= tm
->tm_min
;
143 tw
.tw_hour
= tm
->tm_hour
;
144 tw
.tw_mday
= tm
->tm_mday
;
145 tw
.tw_mon
= tm
->tm_mon
;
148 * tm_year is always "year - 1900"
149 * So we correct for this.
151 tw
.tw_year
= tm
->tm_year
+ 1900;
152 tw
.tw_wday
= tm
->tm_wday
;
153 tw
.tw_yday
= tm
->tm_yday
;
155 tw
.tw_flags
= TW_NULL
;
157 tw
.tw_flags
|= TW_DST
;
161 tw
.tw_flags
&= ~TW_SDAY
;
162 tw
.tw_flags
|= TW_SEXP
;
163 tw
.tw_flags
|= TW_SZEXP
;
165 tw
.tw_clock
= *clock
;
172 * Using a nmh "broken-down" time structure,
173 * produce a 26-byte date/time string, such as
175 * Tue Jan 14 17:49:03 1992\n\0
179 dctime (struct tws
*tw
)
181 static char buffer
[26];
186 snprintf (buffer
, sizeof(buffer
), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
187 tw_dotw
[tw
->tw_wday
], tw_moty
[tw
->tw_mon
], tw
->tw_mday
,
188 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
,
189 tw
->tw_year
< 100 ? tw
->tw_year
+ 1900 : tw
->tw_year
);
196 * Produce a date/time string of the form
198 * Mon, 16 Jun 1992 15:30:48 -700 (or)
199 * Mon, 16 Jun 1992 15:30:48 EDT
201 * for the current time, as specified by RFC 822.
202 * The first form is required by RFC 1123.
206 dtimenow (int alpha_timezone
)
211 return dtime (&clock
, alpha_timezone
);
216 * Using a local calendar time value, produce
217 * a date/time string of the form
219 * Mon, 16 Jun 1992 15:30:48 -700 (or)
220 * Mon, 16 Jun 1992 15:30:48 EDT
222 * as specified by RFC 822. The first form is required
223 * by RFC 1123 for outgoing messages.
227 dtime (time_t *clock
, int alpha_timezone
)
230 /* use alpha-numeric timezones */
231 return dasctime (dlocaltime (clock
), TW_NULL
);
233 /* use numeric timezones */
234 return dasctime (dlocaltime (clock
), TW_ZONE
);
239 * Using a nmh "broken-down" time structure, produce
240 * a date/time string of the form
242 * Mon, 16 Jun 1992 15:30:48 -0700
244 * as specified by RFC 822 and RFC 1123.
248 dasctime (struct tws
*tw
, int flags
)
251 static char result
[80];
257 /* Display timezone if known */
258 if (tw
->tw_flags
& TW_SZEXP
)
259 snprintf(result
, sizeof(result
), " %s", dtimezone(tw
->tw_zone
, tw
->tw_flags
| flags
));
263 snprintf(buffer
, sizeof(buffer
), "%02d %s %0*d %02d:%02d:%02d%s",
264 tw
->tw_mday
, tw_moty
[tw
->tw_mon
],
265 tw
->tw_year
< 100 ? 2 : 4, tw
->tw_year
,
266 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
, result
);
268 if ((twf
= tw
->tw_flags
& TW_SDAY
)) {
270 snprintf(result
, sizeof(result
), "%s, %s", tw_dotw
[tw
->tw_wday
], buffer
);
272 snprintf(result
, sizeof(result
), "%s (%s)", buffer
, tw_dotw
[tw
->tw_wday
]);
274 strncpy(result
, buffer
, sizeof(result
));
281 * Get the timezone for given offset.
282 * This used to return a three-letter abbreviation for some offset
283 * values. But not many. Until there's a good way to do that,
284 * return the string representation of the numeric offset.
288 dtimezone (int offset
, int flags
)
291 static char buffer
[10];
294 mins
= -((-offset
) % 60);
295 hours
= -((-offset
) / 60);
303 snprintf (buffer
, sizeof(buffer
), "%s%02d%02d",
304 offset
< 0 ? "-" : "+", abs (hours
), abs (mins
));
310 * Convert nmh time structure for local "broken-down"
311 * time to calendar time (clock value). This routine
312 * is based on the gtime() routine written by Steven Shafer
313 * at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
317 dmktime (struct tws
*tw
)
319 int i
, sec
, min
, hour
, mday
, mon
, year
;
322 if (tw
->tw_clock
!= 0)
325 if ((sec
= tw
->tw_sec
) < 0 || sec
> 61
326 || (min
= tw
->tw_min
) < 0 || min
> 59
327 || (hour
= tw
->tw_hour
) < 0 || hour
> 23
328 || (mday
= tw
->tw_mday
) < 1 || mday
> 31
329 || (mon
= tw
->tw_mon
+ 1) < 1 || mon
> 12)
330 return (tw
->tw_clock
= (time_t) -1);
341 for (i
= 1970; i
< year
; i
++)
342 result
+= dysize (i
);
343 if (dysize (year
) == 366 && mon
>= 3)
346 result
+= dmsize
[mon
- 1];
348 result
= 24 * result
+ hour
;
349 result
= 60 * result
+ min
;
350 result
= 60 * result
+ sec
;
351 result
-= 60 * tw
->tw_zone
;
352 if (tw
->tw_flags
& TW_DST
)
355 return (tw
->tw_clock
= result
);
360 * Simple calculation of day of the week. Algorithm
361 * used is Zeller's congruence. We assume that
362 * if tw->tw_year < 100, then the century = 19.
366 set_dotw (struct tws
*tw
)
368 int month
, day
, year
, century
;
370 month
= tw
->tw_mon
- 1;
372 year
= tw
->tw_year
% 100;
373 century
= tw
->tw_year
< 100 ? 19 : tw
->tw_year
/ 100;
384 ((26 * month
- 2) / 10 + day
+ year
+ year
/ 4
385 - 3 * century
/ 4 + 1) % 7;
389 tw
->tw_flags
&= ~TW_SDAY
;
390 tw
->tw_flags
|= TW_SIMP
;
395 * Compare two nmh time structures
399 twsort (struct tws
*tw1
, struct tws
*tw2
)
403 if (tw1
->tw_clock
== 0)
405 if (tw2
->tw_clock
== 0)
408 return ((c1
= tw1
->tw_clock
) > (c2
= tw2
->tw_clock
) ? 1
409 : c1
== c2
? 0 : -1);