]>
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.
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",
44 static int dmsize
[] = {
45 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
50 * Get current time (adjusted for local time
51 * zone and daylight savings time) expressed
52 * as nmh "broken-down" time structure.
61 return dlocaltime (&clock
);
66 * Take clock value and return pointer to nmh time structure
67 * containing "broken-down" time. The time is adjusted for
68 * local time zone and daylight savings time.
72 dlocaltime (time_t *clock
)
80 tm
= localtime (clock
);
82 tw
.tw_sec
= tm
->tm_sec
;
83 tw
.tw_min
= tm
->tm_min
;
84 tw
.tw_hour
= tm
->tm_hour
;
85 tw
.tw_mday
= tm
->tm_mday
;
86 tw
.tw_mon
= tm
->tm_mon
;
89 * tm_year is always "year - 1900".
90 * So we correct for this.
92 tw
.tw_year
= tm
->tm_year
+ 1900;
93 tw
.tw_wday
= tm
->tm_wday
;
94 tw
.tw_yday
= tm
->tm_yday
;
96 tw
.tw_flags
= TW_NULL
;
98 tw
.tw_flags
|= TW_DST
;
100 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
101 tw
.tw_zone
= tm
->tm_gmtoff
/ 60;
102 if (tm
->tm_isdst
) /* if DST is in effect */
103 tw
.tw_zone
-= 60; /* reset to normal offset */
113 tw
.tw_zone
= -(timezone
/ 60);
116 tw
.tw_flags
&= ~TW_SDAY
;
117 tw
.tw_flags
|= TW_SEXP
;
118 tw
.tw_flags
|= TW_SZEXP
;
120 tw
.tw_clock
= *clock
;
127 * Take clock value and return pointer to nmh time
128 * structure containing "broken-down" time. Time is
129 * expressed in UTC (Coordinated Universal Time).
133 dgmtime (time_t *clock
)
135 static struct tws tw
;
143 tw
.tw_sec
= tm
->tm_sec
;
144 tw
.tw_min
= tm
->tm_min
;
145 tw
.tw_hour
= tm
->tm_hour
;
146 tw
.tw_mday
= tm
->tm_mday
;
147 tw
.tw_mon
= tm
->tm_mon
;
150 * tm_year is always "year - 1900"
151 * So we correct for this.
153 tw
.tw_year
= tm
->tm_year
+ 1900;
154 tw
.tw_wday
= tm
->tm_wday
;
155 tw
.tw_yday
= tm
->tm_yday
;
157 tw
.tw_flags
= TW_NULL
;
159 tw
.tw_flags
|= TW_DST
;
163 tw
.tw_flags
&= ~TW_SDAY
;
164 tw
.tw_flags
|= TW_SEXP
;
165 tw
.tw_flags
|= TW_SZEXP
;
167 tw
.tw_clock
= *clock
;
174 * Using a nmh "broken-down" time structure,
175 * produce a 26-byte date/time string, such as
177 * Tue Jan 14 17:49:03 1992\n\0
181 dctime (struct tws
*tw
)
183 static char buffer
[26];
188 snprintf (buffer
, sizeof(buffer
), "%.3s %.3s %02d %02d:%02d:%02d %s\n",
189 tw_dotw
[tw
->tw_wday
], tw_moty
[tw
->tw_mon
], tw
->tw_mday
,
190 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
,
191 m_strn(tw
->tw_year
< 100 ? tw
->tw_year
+ 1900 : tw
->tw_year
, 4));
198 * Produce a date/time string of the form
200 * Mon, 16 Jun 1992 15:30:48 -700 (or)
201 * Mon, 16 Jun 1992 15:30:48 EDT
203 * for the current time, as specified by RFC 822.
204 * The first form is required by RFC 1123.
208 dtimenow (int alpha_timezone
)
213 return dtime (&clock
, alpha_timezone
);
218 * Using a local calendar time value, produce
219 * a date/time string of the form
221 * Mon, 16 Jun 1992 15:30:48 -700 (or)
222 * Mon, 16 Jun 1992 15:30:48 EDT
224 * as specified by RFC 822. The first form is required
225 * by RFC 1123 for outgoing messages.
229 dtime (time_t *clock
, int alpha_timezone
)
232 /* use alpha-numeric timezones */
233 return dasctime (dlocaltime (clock
), TW_NULL
);
235 /* use numeric timezones */
236 return dasctime (dlocaltime (clock
), TW_ZONE
);
241 * Using a nmh "broken-down" time structure, produce
242 * a date/time string of the form
244 * Mon, 16 Jun 1992 15:30:48 -0700
246 * as specified by RFC 822 and RFC 1123.
250 dasctime (struct tws
*tw
, int flags
)
253 static char result
[80];
259 /* Display timezone if known */
260 if (tw
->tw_flags
& TW_SZEXP
)
261 snprintf(result
, sizeof(result
), " %s", dtimezone(tw
->tw_zone
, tw
->tw_flags
| flags
));
265 snprintf(buffer
, sizeof(buffer
), "%02d %s %0*d %02d:%02d:%02d%s",
266 tw
->tw_mday
, tw_moty
[tw
->tw_mon
],
267 tw
->tw_year
< 100 ? 2 : 4, tw
->tw_year
,
268 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
, result
);
270 if ((twf
= tw
->tw_flags
& TW_SDAY
)) {
272 snprintf(result
, sizeof(result
), "%s, %s", tw_dotw
[tw
->tw_wday
], buffer
);
274 snprintf(result
, sizeof(result
), "%s (%s)", buffer
, tw_dotw
[tw
->tw_wday
]);
276 strncpy(result
, buffer
, sizeof(result
));
283 * Get the timezone for given offset.
284 * This used to return a three-letter abbreviation for some offset
285 * values. But not many. Until there's a good way to do that,
286 * return the string representation of the numeric offset.
290 dtimezone(int offset
, int flags
)
292 static char buffer
[sizeof "+3579139459"]; /* 2,147,483,648 / 60 = 35,791,394 */
294 unsigned os
, hours
, mins
;
297 os
= pos
? offset
: ~offset
+ 1; /* abs(3) undefined on INT_MIN. */
301 if (flags
& TW_DST
) /* Shift towards +infinity. */
302 hours
+= pos
? 1 : -1;
304 snprintf(buffer
, sizeof(buffer
), "%c%02u%02u",
305 pos
? '+' : '-', hours
, mins
);
312 * Convert nmh time structure for local "broken-down"
313 * time to calendar time (clock value). This routine
314 * is based on the gtime() routine written by Steven Shafer
315 * at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
319 dmktime (struct tws
*tw
)
321 int i
, sec
, min
, hour
, mday
, mon
, year
;
324 if (tw
->tw_clock
!= 0)
327 if ((sec
= tw
->tw_sec
) < 0 || sec
> 61
328 || (min
= tw
->tw_min
) < 0 || min
> 59
329 || (hour
= tw
->tw_hour
) < 0 || hour
> 23
330 || (mday
= tw
->tw_mday
) < 1 || mday
> 31
331 || (mon
= tw
->tw_mon
+ 1) < 1 || mon
> 12)
332 return (tw
->tw_clock
= (time_t) -1);
343 for (i
= 1970; i
< year
; i
++)
344 result
+= dysize (i
);
345 if (dysize (year
) == 366 && mon
>= 3)
348 result
+= dmsize
[mon
- 1];
350 result
*= 24; /* Days to hours. */
352 result
*= 60; /* Hours to minutes. */
354 result
*= 60; /* Minutes to seconds. */
356 result
-= 60 * tw
->tw_zone
;
357 if (tw
->tw_flags
& TW_DST
)
358 result
-= 60 * 60; /* One hour. */
360 return tw
->tw_clock
= result
;
365 * Simple calculation of day of the week. Algorithm
366 * used is Zeller's congruence. We assume that
367 * if tw->tw_year < 100, then the century = 19.
371 set_dotw (struct tws
*tw
)
373 int month
, day
, year
, century
;
375 month
= tw
->tw_mon
- 1;
377 year
= tw
->tw_year
% 100;
378 century
= tw
->tw_year
< 100 ? 19 : tw
->tw_year
/ 100;
389 ((26 * month
- 2) / 10 + day
+ year
+ year
/ 4
390 - 3 * century
/ 4 + 1) % 7;
394 tw
->tw_flags
&= ~TW_SDAY
;
395 tw
->tw_flags
|= TW_SIMP
;
400 * Compare two nmh time structures
404 twsort (struct tws
*tw1
, struct tws
*tw2
)
408 if (tw1
->tw_clock
== 0)
410 if (tw2
->tw_clock
== 0)
413 return (c1
= tw1
->tw_clock
) > (c2
= tw2
->tw_clock
) ? 1