]>
diplodocus.org Git - nmh/blob - sbr/dtime.c
9a1002652c5f6b80363a415653942854da95aa4b
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 * The number of days in the year, accounting for leap years
27 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
30 "Jan", "Feb", "Mar", "Apr",
31 "May", "Jun", "Jul", "Aug",
32 "Sep", "Oct", "Nov", "Dec",
43 "Sunday", "Monday", "Tuesday",
44 "Wednesday", "Thursday", "Friday",
48 static int dmsize
[] = {
49 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
54 * Get current time (adjusted for local time
55 * zone and daylight savings time) expressed
56 * as nmh "broken-down" time structure.
65 return dlocaltime (&clock
);
70 * Take clock value and return pointer to nmh time structure
71 * containing "broken-down" time. The time is adjusted for
72 * local time zone and daylight savings time.
76 dlocaltime (time_t *clock
)
84 tm
= localtime (clock
);
86 tw
.tw_sec
= tm
->tm_sec
;
87 tw
.tw_min
= tm
->tm_min
;
88 tw
.tw_hour
= tm
->tm_hour
;
89 tw
.tw_mday
= tm
->tm_mday
;
90 tw
.tw_mon
= tm
->tm_mon
;
93 * tm_year is always "year - 1900".
94 * So we correct for this.
96 tw
.tw_year
= tm
->tm_year
+ 1900;
97 tw
.tw_wday
= tm
->tm_wday
;
98 tw
.tw_yday
= tm
->tm_yday
;
100 tw
.tw_flags
= TW_NULL
;
102 tw
.tw_flags
|= TW_DST
;
104 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
105 tw
.tw_zone
= tm
->tm_gmtoff
/ 60;
106 if (tm
->tm_isdst
) /* if DST is in effect */
107 tw
.tw_zone
-= 60; /* reset to normal offset */
117 tw
.tw_zone
= -(timezone
/ 60);
120 tw
.tw_flags
&= ~TW_SDAY
;
121 tw
.tw_flags
|= TW_SEXP
;
122 tw
.tw_flags
|= TW_SZEXP
;
124 tw
.tw_clock
= *clock
;
131 * Take clock value and return pointer to nmh time
132 * structure containing "broken-down" time. Time is
133 * expressed in UTC (Coordinated Universal Time).
137 dgmtime (time_t *clock
)
139 static struct tws tw
;
147 tw
.tw_sec
= tm
->tm_sec
;
148 tw
.tw_min
= tm
->tm_min
;
149 tw
.tw_hour
= tm
->tm_hour
;
150 tw
.tw_mday
= tm
->tm_mday
;
151 tw
.tw_mon
= tm
->tm_mon
;
154 * tm_year is always "year - 1900"
155 * So we correct for this.
157 tw
.tw_year
= tm
->tm_year
+ 1900;
158 tw
.tw_wday
= tm
->tm_wday
;
159 tw
.tw_yday
= tm
->tm_yday
;
161 tw
.tw_flags
= TW_NULL
;
163 tw
.tw_flags
|= TW_DST
;
167 tw
.tw_flags
&= ~TW_SDAY
;
168 tw
.tw_flags
|= TW_SEXP
;
169 tw
.tw_flags
|= TW_SZEXP
;
171 tw
.tw_clock
= *clock
;
178 * Using a nmh "broken-down" time structure,
179 * produce a 26-byte date/time string, such as
181 * Tue Jan 14 17:49:03 1992\n\0
185 dctime (struct tws
*tw
)
187 static char buffer
[26];
192 snprintf (buffer
, sizeof(buffer
), "%.3s %.3s %02d %02d:%02d:%02d %s\n",
193 tw_dotw
[tw
->tw_wday
], tw_moty
[tw
->tw_mon
], tw
->tw_mday
,
194 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
,
195 m_strn(tw
->tw_year
< 100 ? tw
->tw_year
+ 1900 : tw
->tw_year
, 4));
202 * Produce a date/time string of the form
204 * Mon, 16 Jun 1992 15:30:48 -700 (or)
205 * Mon, 16 Jun 1992 15:30:48 EDT
207 * for the current time, as specified by RFC 822.
208 * The first form is required by RFC 1123.
212 dtimenow (int alpha_timezone
)
217 return dtime (&clock
, alpha_timezone
);
222 * Using a local calendar time value, produce
223 * a date/time string of the form
225 * Mon, 16 Jun 1992 15:30:48 -700 (or)
226 * Mon, 16 Jun 1992 15:30:48 EDT
228 * as specified by RFC 822. The first form is required
229 * by RFC 1123 for outgoing messages.
233 dtime (time_t *clock
, int alpha_timezone
)
236 /* use alpha-numeric timezones */
237 return dasctime (dlocaltime (clock
), TW_NULL
);
239 /* use numeric timezones */
240 return dasctime (dlocaltime (clock
), TW_ZONE
);
245 * Using a nmh "broken-down" time structure, produce
246 * a date/time string of the form
248 * Mon, 16 Jun 1992 15:30:48 -0700
250 * as specified by RFC 822 and RFC 1123.
254 dasctime (struct tws
*tw
, int flags
)
257 char tzbuffer
[DTZ_BUFFER_SIZE
];
258 static char result
[80];
264 /* Display timezone if known */
265 if (tw
->tw_flags
& TW_SZEXP
)
266 snprintf(tzbuffer
, sizeof(tzbuffer
), " %s", dtimezone(tw
->tw_zone
, tw
->tw_flags
| flags
));
270 snprintf(buffer
, sizeof(buffer
), "%02d %s %0*d %02d:%02d:%02d%s",
271 tw
->tw_mday
, tw_moty
[tw
->tw_mon
],
272 tw
->tw_year
< 100 ? 2 : 4, tw
->tw_year
,
273 tw
->tw_hour
, tw
->tw_min
, tw
->tw_sec
, tzbuffer
);
275 if ((twf
= tw
->tw_flags
& TW_SDAY
)) {
277 snprintf(result
, sizeof(result
), "%s, %s", tw_dotw
[tw
->tw_wday
], buffer
);
279 snprintf(result
, sizeof(result
), "%s (%s)", buffer
, tw_dotw
[tw
->tw_wday
]);
281 strncpy(result
, buffer
, sizeof(result
));
288 * Get the timezone for given offset.
289 * This used to return a three-letter abbreviation for some offset
290 * values. But not many. Until there's a good way to do that,
291 * return the string representation of the numeric offset.
295 dtimezone(int offset
, int flags
)
297 static char buffer
[DTZ_BUFFER_SIZE
];
299 unsigned os
, hours
, mins
;
302 os
= pos
? offset
: ~offset
+ 1; /* abs(3) undefined on INT_MIN. */
306 if (flags
& TW_DST
) /* Shift towards +infinity. */
307 hours
+= pos
? 1 : -1;
309 snprintf(buffer
, sizeof(buffer
), "%c%02u%02u",
310 pos
? '+' : '-', hours
, mins
);
317 * Convert nmh time structure for local "broken-down"
318 * time to calendar time (clock value). This routine
319 * is based on the gtime() routine written by Steven Shafer
320 * at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
324 dmktime (struct tws
*tw
)
326 int i
, sec
, min
, hour
, mday
, mon
, year
;
329 if (tw
->tw_clock
!= 0)
332 if ((sec
= tw
->tw_sec
) < 0 || sec
> 61
333 || (min
= tw
->tw_min
) < 0 || min
> 59
334 || (hour
= tw
->tw_hour
) < 0 || hour
> 23
335 || (mday
= tw
->tw_mday
) < 1 || mday
> 31
336 || (mon
= tw
->tw_mon
+ 1) < 1 || mon
> 12)
337 return (tw
->tw_clock
= (time_t) -1);
348 for (i
= 1970; i
< year
; i
++)
349 result
+= dysize (i
);
350 if (dysize (year
) == 366 && mon
>= 3)
353 result
+= dmsize
[mon
- 1];
355 result
*= 24; /* Days to hours. */
357 result
*= 60; /* Hours to minutes. */
359 result
*= 60; /* Minutes to seconds. */
361 result
-= 60 * tw
->tw_zone
;
362 if (tw
->tw_flags
& TW_DST
)
363 result
-= 60 * 60; /* One hour. */
365 return tw
->tw_clock
= result
;
370 * Simple calculation of day of the week. Algorithm
371 * used is Zeller's congruence. We assume that
372 * if tw->tw_year < 100, then the century = 19.
376 set_dotw (struct tws
*tw
)
378 int month
, day
, year
, century
;
380 month
= tw
->tw_mon
- 1;
382 year
= tw
->tw_year
% 100;
383 century
= tw
->tw_year
< 100 ? 19 : tw
->tw_year
/ 100;
394 ((26 * month
- 2) / 10 + day
+ year
+ year
/ 4
395 - 3 * century
/ 4 + 1) % 7;
399 tw
->tw_flags
&= ~TW_SDAY
;
400 tw
->tw_flags
|= TW_SIMP
;
405 * Compare two nmh time structures
409 twsort (struct tws
*tw1
, struct tws
*tw2
)
413 if (tw1
->tw_clock
== 0)
415 if (tw2
->tw_clock
== 0)
418 return (c1
= tw1
->tw_clock
) > (c2
= tw2
->tw_clock
) ? 1