]>
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() */
14 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF)
19 * The number of days in the year, accounting for leap years
22 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
25 "Jan", "Feb", "Mar", "Apr",
26 "May", "Jun", "Jul", "Aug",
27 "Sep", "Oct", "Nov", "Dec",
38 "Sunday", "Monday", "Tuesday",
39 "Wednesday", "Thursday", "Friday",
43 static int dmsize
[] = {
44 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
49 * Get current time (adjusted for local time
50 * zone and daylight savings time) expressed
51 * as nmh "broken-down" time structure.
60 return dlocaltime (&clock
);
65 * Take clock value and return pointer to nmh time structure
66 * containing "broken-down" time. The time is adjusted for
67 * local time zone and daylight savings time.
71 dlocaltime (time_t *clock
)
79 tm
= localtime (clock
);
81 tw
.tw_sec
= tm
->tm_sec
;
82 tw
.tw_min
= tm
->tm_min
;
83 tw
.tw_hour
= tm
->tm_hour
;
84 tw
.tw_mday
= tm
->tm_mday
;
85 tw
.tw_mon
= tm
->tm_mon
;
88 * tm_year is always "year - 1900".
89 * So we correct for this.
91 tw
.tw_year
= tm
->tm_year
+ 1900;
92 tw
.tw_wday
= tm
->tm_wday
;
93 tw
.tw_yday
= tm
->tm_yday
;
95 tw
.tw_flags
= TW_NULL
;
97 tw
.tw_flags
|= TW_DST
;
99 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
100 tw
.tw_zone
= tm
->tm_gmtoff
/ 60;
101 if (tm
->tm_isdst
) /* if DST is in effect */
102 tw
.tw_zone
-= 60; /* reset to normal offset */
112 tw
.tw_zone
= -(timezone
/ 60);
115 tw
.tw_flags
&= ~TW_SDAY
;
116 tw
.tw_flags
|= TW_SEXP
;
117 tw
.tw_flags
|= TW_SZEXP
;
119 tw
.tw_clock
= *clock
;
126 * Take clock value and return pointer to nmh time
127 * structure containing "broken-down" time. Time is
128 * expressed in UTC (Coordinated Universal Time).
132 dgmtime (time_t *clock
)
134 static struct tws tw
;
142 tw
.tw_sec
= tm
->tm_sec
;
143 tw
.tw_min
= tm
->tm_min
;
144 tw
.tw_hour
= tm
->tm_hour
;
145 tw
.tw_mday
= tm
->tm_mday
;
146 tw
.tw_mon
= tm
->tm_mon
;
149 * tm_year is always "year - 1900"
150 * So we correct for this.
152 tw
.tw_year
= tm
->tm_year
+ 1900;
153 tw
.tw_wday
= tm
->tm_wday
;
154 tw
.tw_yday
= tm
->tm_yday
;
156 tw
.tw_flags
= TW_NULL
;
158 tw
.tw_flags
|= TW_DST
;
162 tw
.tw_flags
&= ~TW_SDAY
;
163 tw
.tw_flags
|= TW_SEXP
;
164 tw
.tw_flags
|= TW_SZEXP
;
166 tw
.tw_clock
= *clock
;
173 * Using a nmh "broken-down" time structure,
174 * produce a 26-byte date/time string, such as
176 * Tue Jan 14 17:49:03 1992\n\0
180 dctime (struct tws
*tw
)
182 static char buffer
[26];
187 snprintf (buffer
, sizeof(buffer
), "%.3s %.3s %02d %02d:%02d:%02d %s\n",
188 tw_dotw
[tw
->tw_wday
], tw_moty
[tw
->tw_mon
], tw
->tw_mday
,
189 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
,
190 m_strn(tw
->tw_year
< 100 ? tw
->tw_year
+ 1900 : tw
->tw_year
, 4));
197 * Produce a date/time string of the form
199 * Mon, 16 Jun 1992 15:30:48 -700 (or)
200 * Mon, 16 Jun 1992 15:30:48 EDT
202 * for the current time, as specified by RFC 822.
203 * The first form is required by RFC 1123.
207 dtimenow (int alpha_timezone
)
212 return dtime (&clock
, alpha_timezone
);
217 * Using a local calendar time value, produce
218 * a date/time string of the form
220 * Mon, 16 Jun 1992 15:30:48 -700 (or)
221 * Mon, 16 Jun 1992 15:30:48 EDT
223 * as specified by RFC 822. The first form is required
224 * by RFC 1123 for outgoing messages.
228 dtime (time_t *clock
, int alpha_timezone
)
231 /* use alpha-numeric timezones */
232 return dasctime (dlocaltime (clock
), TW_NULL
);
234 /* use numeric timezones */
235 return dasctime (dlocaltime (clock
), TW_ZONE
);
240 * Using a nmh "broken-down" time structure, produce
241 * a date/time string of the form
243 * Mon, 16 Jun 1992 15:30:48 -0700
245 * as specified by RFC 822 and RFC 1123.
249 dasctime (struct tws
*tw
, int flags
)
252 static char result
[80];
258 /* Display timezone if known */
259 if (tw
->tw_flags
& TW_SZEXP
)
260 snprintf(result
, sizeof(result
), " %s", dtimezone(tw
->tw_zone
, tw
->tw_flags
| flags
));
264 snprintf(buffer
, sizeof(buffer
), "%02d %s %0*d %02d:%02d:%02d%s",
265 tw
->tw_mday
, tw_moty
[tw
->tw_mon
],
266 tw
->tw_year
< 100 ? 2 : 4, tw
->tw_year
,
267 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
, result
);
269 if ((twf
= tw
->tw_flags
& TW_SDAY
)) {
271 snprintf(result
, sizeof(result
), "%s, %s", tw_dotw
[tw
->tw_wday
], buffer
);
273 snprintf(result
, sizeof(result
), "%s (%s)", buffer
, tw_dotw
[tw
->tw_wday
]);
275 strncpy(result
, buffer
, sizeof(result
));
282 * Get the timezone for given offset.
283 * This used to return a three-letter abbreviation for some offset
284 * values. But not many. Until there's a good way to do that,
285 * return the string representation of the numeric offset.
288 char *dtimezone(int offset
, int flags
)
290 static char buffer
[sizeof "+3579139459"]; /* 2,147,483,648 / 60 = 35,791,394 */
292 unsigned os
, hours
, mins
;
295 os
= pos
? offset
: ~offset
+ 1; /* abs(3) undefined on INT_MIN. */
299 if (flags
& TW_DST
) /* Shift towards +infinity. */
300 hours
+= pos
? 1 : -1;
302 snprintf(buffer
, sizeof(buffer
), "%c%02u%02u",
303 pos
? '+' : '-', hours
, 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);