]>
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() */
16 * The number of days in the year, accounting for leap years
19 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
22 "Jan", "Feb", "Mar", "Apr",
23 "May", "Jun", "Jul", "Aug",
24 "Sep", "Oct", "Nov", "Dec",
35 "Sunday", "Monday", "Tuesday",
36 "Wednesday", "Thursday", "Friday",
46 static int dmsize
[] = {
47 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
52 * Get current time (adjusted for local time
53 * zone and daylight savings time) expressed
54 * as nmh "broken-down" time structure.
63 return dlocaltime (&clock
);
68 * Take clock value and return pointer to nmh time structure
69 * containing "broken-down" time. The time is adjusted for
70 * local time zone and daylight savings time.
74 dlocaltime (time_t *clock
)
82 tm
= localtime (clock
);
84 tw
.tw_sec
= tm
->tm_sec
;
85 tw
.tw_min
= tm
->tm_min
;
86 tw
.tw_hour
= tm
->tm_hour
;
87 tw
.tw_mday
= tm
->tm_mday
;
88 tw
.tw_mon
= tm
->tm_mon
;
91 * tm_year is always "year - 1900".
92 * So we correct for this.
94 tw
.tw_year
= tm
->tm_year
+ 1900;
95 tw
.tw_wday
= tm
->tm_wday
;
96 tw
.tw_yday
= tm
->tm_yday
;
98 tw
.tw_flags
= TW_NULL
;
100 tw
.tw_flags
|= TW_DST
;
103 tw
.tw_zone
= -(timezone
/ 60);
105 tw
.tw_flags
&= ~TW_SDAY
;
106 tw
.tw_flags
|= TW_SEXP
;
107 tw
.tw_flags
&= ~TW_SZONE
;
108 tw
.tw_flags
|= TW_SZEXP
;
110 tw
.tw_clock
= *clock
;
117 * Take clock value and return pointer to nmh time
118 * structure containing "broken-down" time. Time is
119 * expressed in UTC (Coordinated Universal Time).
123 dgmtime (time_t *clock
)
125 static struct tws tw
;
133 tw
.tw_sec
= tm
->tm_sec
;
134 tw
.tw_min
= tm
->tm_min
;
135 tw
.tw_hour
= tm
->tm_hour
;
136 tw
.tw_mday
= tm
->tm_mday
;
137 tw
.tw_mon
= tm
->tm_mon
;
140 * tm_year is always "year - 1900"
141 * So we correct for this.
143 tw
.tw_year
= tm
->tm_year
+ 1900;
144 tw
.tw_wday
= tm
->tm_wday
;
145 tw
.tw_yday
= tm
->tm_yday
;
147 tw
.tw_flags
= TW_NULL
;
149 tw
.tw_flags
|= TW_DST
;
153 tw
.tw_flags
&= ~TW_SDAY
;
154 tw
.tw_flags
|= TW_SEXP
;
155 tw
.tw_flags
&= ~TW_SZONE
;
156 tw
.tw_flags
|= TW_SZEXP
;
158 tw
.tw_clock
= *clock
;
165 * Using a nmh "broken-down" time structure,
166 * produce a 26-byte date/time string, such as
168 * Tue Jan 14 17:49:03 1992\n\0
172 dctime (struct tws
*tw
)
174 static char buffer
[26];
179 snprintf (buffer
, sizeof(buffer
), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
180 tw_dotw
[tw
->tw_wday
], tw_moty
[tw
->tw_mon
], tw
->tw_mday
,
181 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
,
182 tw
->tw_year
< 100 ? tw
->tw_year
+ 1900 : tw
->tw_year
);
189 * Produce a date/time string of the form
191 * Mon, 16 Jun 1992 15:30:48 -700 (or)
192 * Mon, 16 Jun 1992 15:30:48 EDT
194 * for the current time, as specified by rfc822.
195 * The first form is required by rfc1123.
199 dtimenow (int alpha_timezone
)
204 return dtime (&clock
, alpha_timezone
);
209 * Using a local calendar time value, produce
210 * a date/time string of the form
212 * Mon, 16 Jun 1992 15:30:48 -700 (or)
213 * Mon, 16 Jun 1992 15:30:48 EDT
215 * as specified by rfc822. The first form is required
216 * by rfc1123 for outgoing messages.
220 dtime (time_t *clock
, int alpha_timezone
)
223 /* use alpha-numeric timezones */
224 return dasctime (dlocaltime (clock
), TW_NULL
);
226 /* use numeric timezones */
227 return dasctime (dlocaltime (clock
), TW_ZONE
);
232 * Using a nmh "broken-down" time structure, produce
233 * a date/time string of the form
235 * Mon, 16 Jun 1992 15:30:48 -0700
237 * as specified by rfc822 and rfc1123.
241 dasctime (struct tws
*tw
, int flags
)
244 static char result
[80];
249 /* Display timezone if known */
250 if ((tw
->tw_flags
& TW_SZONE
) == TW_SZNIL
)
253 snprintf(result
, sizeof(result
), " %s", dtimezone(tw
->tw_zone
, tw
->tw_flags
| flags
));
255 snprintf(buffer
, sizeof(buffer
), "%02d %s %0*d %02d:%02d:%02d%s",
256 tw
->tw_mday
, tw_moty
[tw
->tw_mon
],
257 tw
->tw_year
< 100 ? 2 : 4, tw
->tw_year
,
258 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
, result
);
260 if ((tw
->tw_flags
& TW_SDAY
) == TW_SEXP
)
261 snprintf (result
, sizeof(result
), "%s, %s", tw_dotw
[tw
->tw_wday
], buffer
);
263 if ((tw
->tw_flags
& TW_SDAY
) == TW_SNIL
)
264 strncpy (result
, buffer
, sizeof(result
));
266 snprintf (result
, sizeof(result
), "%s (%s)", buffer
, tw_dotw
[tw
->tw_wday
]);
273 * Get the timezone for given offset
277 dtimezone (int offset
, int flags
)
280 static char buffer
[10];
283 mins
= -((-offset
) % 60);
284 hours
= -((-offset
) / 60);
290 if (!(flags
& TW_ZONE
) && mins
== 0) {
292 return ((flags
& TW_DST
) ? tzname
[1] : tzname
[0]);
295 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
298 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
299 snprintf (buffer
, sizeof(buffer
), "%s%02d%02d",
300 offset
< 0 ? "-" : "+", abs (hours
), abs (mins
));
306 * Convert nmh time structure for local "broken-down"
307 * time to calendar time (clock value). This routine
308 * is based on the gtime() routine written by Steven Shafer
309 * at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
313 dmktime (struct tws
*tw
)
315 int i
, sec
, min
, hour
, mday
, mon
, year
;
318 if (tw
->tw_clock
!= 0)
321 if ((sec
= tw
->tw_sec
) < 0 || sec
> 61
322 || (min
= tw
->tw_min
) < 0 || min
> 59
323 || (hour
= tw
->tw_hour
) < 0 || hour
> 23
324 || (mday
= tw
->tw_mday
) < 1 || mday
> 31
325 || (mon
= tw
->tw_mon
+ 1) < 1 || mon
> 12)
326 return (tw
->tw_clock
= (time_t) -1);
337 for (i
= 1970; i
< year
; i
++)
338 result
+= dysize (i
);
339 if (dysize (year
) == 366 && mon
>= 3)
342 result
+= dmsize
[mon
- 1];
344 result
= 24 * result
+ hour
;
345 result
= 60 * result
+ min
;
346 result
= 60 * result
+ sec
;
347 result
-= 60 * tw
->tw_zone
;
348 if (tw
->tw_flags
& TW_DST
)
351 return (tw
->tw_clock
= result
);
356 * Simple calculation of day of the week. Algorithm
357 * used is Zeller's congruence. We assume that
358 * if tw->tw_year < 100, then the century = 19.
362 set_dotw (struct tws
*tw
)
364 int month
, day
, year
, century
;
366 month
= tw
->tw_mon
- 1;
368 year
= tw
->tw_year
% 100;
369 century
= tw
->tw_year
< 100 ? 19 : tw
->tw_year
/ 100;
380 ((26 * month
- 2) / 10 + day
+ year
+ year
/ 4
381 - 3 * century
/ 4 + 1) % 7;
385 tw
->tw_flags
&= ~TW_SDAY
, tw
->tw_flags
|= TW_SIMP
;
390 * Copy nmh time structure
394 twscopy (struct tws
*tb
, struct tws
*tw
)
396 *tb
= *tw
; /* struct copy */
399 tb
->tw_sec
= tw
->tw_sec
;
400 tb
->tw_min
= tw
->tw_min
;
401 tb
->tw_hour
= tw
->tw_hour
;
402 tb
->tw_mday
= tw
->tw_mday
;
403 tb
->tw_mon
= tw
->tw_mon
;
404 tb
->tw_year
= tw
->tw_year
;
405 tb
->tw_wday
= tw
->tw_wday
;
406 tb
->tw_yday
= tw
->tw_yday
;
407 tb
->tw_zone
= tw
->tw_zone
;
408 tb
->tw_clock
= tw
->tw_clock
;
409 tb
->tw_flags
= tw
->tw_flags
;
415 * Compare two nmh time structures
419 twsort (struct tws
*tw1
, struct tws
*tw2
)
423 if (tw1
->tw_clock
== 0)
425 if (tw2
->tw_clock
== 0)
428 return ((c1
= tw1
->tw_clock
) > (c2
= tw2
->tw_clock
) ? 1
429 : c1
== c2
? 0 : -1);