]>
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)
19 /* Size of largest possible int representing a time zone offset in minutes:
20 2,147,483,648 / 60 = 35,791,394 */
21 #define DTZ_BUFFER_SIZE (sizeof "+3579139459")
24 * 1 if leap year, 0 otherwise.
26 #define isleapyear01(y) \
27 (((y) % 4) ? 0 : (((y) % 100) ? 1 : (((y) % 400) ? 0 : 1)))
30 * The number of days in the year, accounting for leap years
33 (365 + isleapyear01(y))
36 "Jan", "Feb", "Mar", "Apr",
37 "May", "Jun", "Jul", "Aug",
38 "Sep", "Oct", "Nov", "Dec",
49 "Sunday", "Monday", "Tuesday",
50 "Wednesday", "Thursday", "Friday",
54 static int dmsize
[][12] = {
55 { /* Not a leap year */
56 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
58 { /* Leap year: February = 29 */
59 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
65 * Get current time (adjusted for local time
66 * zone and daylight savings time) expressed
67 * as nmh "broken-down" time structure.
76 return dlocaltime (&clock
);
81 * Take clock value and return pointer to nmh time structure
82 * containing "broken-down" time. The time is adjusted for
83 * local time zone and daylight savings time.
87 dlocaltime (time_t *clock
)
95 tm
= localtime (clock
);
97 tw
.tw_sec
= tm
->tm_sec
;
98 tw
.tw_min
= tm
->tm_min
;
99 tw
.tw_hour
= tm
->tm_hour
;
100 tw
.tw_mday
= tm
->tm_mday
;
101 tw
.tw_mon
= tm
->tm_mon
;
104 * tm_year is always "year - 1900".
105 * So we correct for this.
107 tw
.tw_year
= tm
->tm_year
+ 1900;
108 tw
.tw_wday
= tm
->tm_wday
;
109 tw
.tw_yday
= tm
->tm_yday
;
111 tw
.tw_flags
= TW_NULL
;
113 tw
.tw_flags
|= TW_DST
;
115 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
116 tw
.tw_zone
= tm
->tm_gmtoff
/ 60;
117 if (tm
->tm_isdst
) /* if DST is in effect */
118 tw
.tw_zone
-= 60; /* reset to normal offset */
128 tw
.tw_zone
= -(timezone
/ 60);
131 tw
.tw_flags
&= ~TW_SDAY
;
132 tw
.tw_flags
|= TW_SEXP
;
133 tw
.tw_flags
|= TW_SZEXP
;
135 tw
.tw_clock
= *clock
;
142 * Take clock value and return pointer to nmh time
143 * structure containing "broken-down" time. Time is
144 * expressed in UTC (Coordinated Universal Time).
148 dgmtime (time_t *clock
)
150 static struct tws tw
;
158 tw
.tw_sec
= tm
->tm_sec
;
159 tw
.tw_min
= tm
->tm_min
;
160 tw
.tw_hour
= tm
->tm_hour
;
161 tw
.tw_mday
= tm
->tm_mday
;
162 tw
.tw_mon
= tm
->tm_mon
;
165 * tm_year is always "year - 1900"
166 * So we correct for this.
168 tw
.tw_year
= tm
->tm_year
+ 1900;
169 tw
.tw_wday
= tm
->tm_wday
;
170 tw
.tw_yday
= tm
->tm_yday
;
172 tw
.tw_flags
= TW_NULL
;
174 tw
.tw_flags
|= TW_DST
;
178 tw
.tw_flags
&= ~TW_SDAY
;
179 tw
.tw_flags
|= TW_SEXP
;
180 tw
.tw_flags
|= TW_SZEXP
;
182 tw
.tw_clock
= *clock
;
189 * Using a nmh "broken-down" time structure,
190 * produce a 26-byte date/time string, such as
192 * Tue Jan 14 17:49:03 1992\n\0
196 dctime (struct tws
*tw
)
198 static char buffer
[26];
203 snprintf (buffer
, sizeof(buffer
), "%.3s %.3s %02d %02d:%02d:%02d %s\n",
204 tw_dotw
[tw
->tw_wday
], tw_moty
[tw
->tw_mon
], tw
->tw_mday
,
205 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
,
206 m_strn(tw
->tw_year
< 100 ? tw
->tw_year
+ 1900 : tw
->tw_year
, 4));
213 * Produce a date/time string of the form
215 * Mon, 16 Jun 1992 15:30:48 -700 (or)
216 * Mon, 16 Jun 1992 15:30:48 EDT
218 * for the current time, as specified by RFC 822.
219 * The first form is required by RFC 1123.
223 dtimenow (int alpha_timezone
)
228 return dtime (&clock
, alpha_timezone
);
233 * Using a local calendar time value, produce
234 * a date/time string of the form
236 * Mon, 16 Jun 1992 15:30:48 -700 (or)
237 * Mon, 16 Jun 1992 15:30:48 EDT
239 * as specified by RFC 822. The first form is required
240 * by RFC 1123 for outgoing messages.
244 dtime (time_t *clock
, int alpha_timezone
)
247 /* use alpha-numeric timezones */
248 return dasctime (dlocaltime (clock
), TW_NULL
);
250 /* use numeric timezones */
251 return dasctime (dlocaltime (clock
), TW_ZONE
);
256 * Using a nmh "broken-down" time structure, produce
257 * a date/time string of the form
259 * Mon, 16 Jun 1992 15:30:48 -0700
261 * as specified by RFC 822 and RFC 1123.
265 dasctime (struct tws
*tw
, int flags
)
268 char tzbuffer
[DTZ_BUFFER_SIZE
];
269 static char result
[80];
275 /* Display timezone if known */
276 if (tw
->tw_flags
& TW_SZEXP
)
277 snprintf(tzbuffer
, sizeof(tzbuffer
), " %s", dtimezone(tw
->tw_zone
, tw
->tw_flags
| flags
));
281 snprintf(buffer
, sizeof(buffer
), "%02d %s %0*d %02d:%02d:%02d%s",
282 tw
->tw_mday
, tw_moty
[tw
->tw_mon
],
283 tw
->tw_year
< 100 ? 2 : 4, tw
->tw_year
,
284 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
, tzbuffer
);
286 if ((twf
= tw
->tw_flags
& TW_SDAY
)) {
288 snprintf(result
, sizeof(result
), "%s, %s", tw_dotw
[tw
->tw_wday
], buffer
);
290 snprintf(result
, sizeof(result
), "%s (%s)", buffer
, tw_dotw
[tw
->tw_wday
]);
292 strncpy(result
, buffer
, sizeof(result
));
299 * Get the timezone for given offset.
300 * This used to return a three-letter abbreviation for some offset
301 * values. But not many. Until there's a good way to do that,
302 * return the string representation of the numeric offset.
306 dtimezone(int offset
, int flags
)
308 static char buffer
[DTZ_BUFFER_SIZE
];
310 unsigned os
, hours
, mins
;
313 os
= pos
? offset
: ~offset
+ 1; /* abs(3) undefined on INT_MIN. */
317 if (flags
& TW_DST
) /* Shift towards +infinity. */
318 hours
+= pos
? 1 : -1;
320 snprintf(buffer
, sizeof(buffer
), "%c%02u%02u",
321 pos
? '+' : '-', hours
, mins
);
328 * Last day of month (1-12) in given year.
332 dmlastday(int year
, int mon
)
334 return dmsize
[isleapyear01 (year
)][mon
- 1];
338 * Convert nmh time structure for local "broken-down"
339 * time to calendar time (clock value). This routine
340 * is based on the gtime() routine written by Steven Shafer
341 * at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
345 dmktime (struct tws
*tw
)
347 int i
, sec
, min
, hour
, mday
, mon
, year
, *msize
;
350 if (tw
->tw_clock
!= 0)
353 if ((sec
= tw
->tw_sec
) < 0 || sec
> 61
354 || (min
= tw
->tw_min
) < 0 || min
> 59
355 || (hour
= tw
->tw_hour
) < 0 || hour
> 23
356 || (mday
= tw
->tw_mday
) < 1 || mday
> 31
357 || (mon
= tw
->tw_mon
+ 1) < 1 || mon
> 12)
358 return (tw
->tw_clock
= (time_t) -1);
369 for (i
= 1970; i
< year
; i
++)
370 result
+= dysize (i
);
371 msize
= dmsize
[isleapyear01 (year
)];
373 result
+= msize
[mon
- 1];
375 result
*= 24; /* Days to hours. */
377 result
*= 60; /* Hours to minutes. */
379 result
*= 60; /* Minutes to seconds. */
381 result
-= 60 * tw
->tw_zone
;
382 if (tw
->tw_flags
& TW_DST
)
383 result
-= 60 * 60; /* One hour. */
385 return tw
->tw_clock
= result
;
390 * Simple calculation of day of the week. Algorithm
391 * used is Zeller's congruence. We assume that
392 * if tw->tw_year < 100, then the century = 19.
396 set_dotw (struct tws
*tw
)
398 int month
, day
, year
, century
;
400 month
= tw
->tw_mon
- 1;
402 year
= tw
->tw_year
% 100;
403 century
= tw
->tw_year
< 100 ? 19 : tw
->tw_year
/ 100;
414 ((26 * month
- 2) / 10 + day
+ year
+ year
/ 4
415 - 3 * century
/ 4 + 1) % 7;
419 tw
->tw_flags
&= ~TW_SDAY
;
420 tw
->tw_flags
|= TW_SIMP
;
425 * Compare two nmh time structures
429 twsort (struct tws
*tw1
, struct tws
*tw2
)
433 if (tw1
->tw_clock
== 0)
435 if (tw2
->tw_clock
== 0)
438 return (c1
= tw1
->tw_clock
) > (c2
= tw2
->tw_clock
) ? 1