1 /*
2 ** dtime.c -- time/date routines
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <time.h>
10 #include <h/mh.h> /* for snprintf() */
11 #include <h/tws.h>
12
13 #if !defined(HAVE_STRUCT_TM_TM_GMTOFF)
14 extern long timezone;
15 #endif
16
17 /*
18 ** The number of days in the year, accounting for leap years
19 */
20 #define dysize(y) \
21 (((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
22
23 char *tw_moty[] = {
24 "Jan", "Feb", "Mar", "Apr",
25 "May", "Jun", "Jul", "Aug",
26 "Sep", "Oct", "Nov", "Dec",
27 NULL
28 };
29
30 char *tw_dotw[] = {
31 "Sun", "Mon", "Tue",
32 "Wed", "Thu", "Fri",
33 "Sat", NULL
34 };
35
36 char *tw_ldotw[] = {
37 "Sunday", "Monday", "Tuesday",
38 "Wednesday", "Thursday", "Friday",
39 "Saturday", NULL
40 };
41
42 static int dmsize[] = {
43 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
44 };
45
46
47 /*
48 ** Get current time (adjusted for local time
49 ** zone and daylight savings time) expressed
50 ** as nmh "broken-down" time structure.
51 */
52
53 struct tws *
dlocaltimenow(void)54 dlocaltimenow(void)
55 {
56 time_t clock;
57
58 time(&clock);
59 return dlocaltime(&clock);
60 }
61
62
63 /*
64 ** Take clock value and return pointer to nmh time structure
65 ** containing "broken-down" time. The time is adjusted for
66 ** local time zone and daylight savings time.
67 */
68
69 struct tws *
dlocaltime(time_t * clock)70 dlocaltime(time_t *clock)
71 {
72 static struct tws tw;
73 struct tm *tm;
74
75 if (!clock)
76 return NULL;
77
78 tm = localtime(clock);
79
80 tw.tw_sec = tm->tm_sec;
81 tw.tw_min = tm->tm_min;
82 tw.tw_hour = tm->tm_hour;
83 tw.tw_mday = tm->tm_mday;
84 tw.tw_mon = tm->tm_mon;
85
86 /* tm_year is always "year - 1900". So we correct for this. */
87 tw.tw_year = tm->tm_year + 1900;
88 tw.tw_wday = tm->tm_wday;
89 tw.tw_yday = tm->tm_yday;
90
91 tw.tw_flags = TW_NULL;
92 if (tm->tm_isdst)
93 tw.tw_flags |= TW_DST;
94
95 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
96 tw.tw_zone = tm->tm_gmtoff / 60;
97 if (tm->tm_isdst) /* if DST is in effect */
98 tw.tw_zone -= 60; /* reset to normal offset */
99 #else
100 tzset();
101 tw.tw_zone = -(timezone / 60);
102 #endif
103
104 tw.tw_flags &= ~TW_SDAY;
105 tw.tw_flags |= TW_SEXP;
106 tw.tw_flags &= ~TW_SZONE;
107 tw.tw_flags |= TW_SZEXP;
108
109 tw.tw_clock = *clock;
110
111 return (&tw);
112 }
113
114
115 /*
116 ** Take clock value and return pointer to nmh time
117 ** structure containing "broken-down" time. Time is
118 ** expressed in UTC (Coordinated Universal Time).
119 */
120
121 struct tws *
dgmtime(time_t * clock)122 dgmtime(time_t *clock)
123 {
124 static struct tws tw;
125 struct tm *tm;
126
127 if (!clock)
128 return NULL;
129
130 tm = gmtime(clock);
131
132 tw.tw_sec = tm->tm_sec;
133 tw.tw_min = tm->tm_min;
134 tw.tw_hour = tm->tm_hour;
135 tw.tw_mday = tm->tm_mday;
136 tw.tw_mon = tm->tm_mon;
137
138 /* tm_year is always "year - 1900". So we correct for this. */
139 tw.tw_year = tm->tm_year + 1900;
140 tw.tw_wday = tm->tm_wday;
141 tw.tw_yday = tm->tm_yday;
142
143 tw.tw_flags = TW_NULL;
144 if (tm->tm_isdst)
145 tw.tw_flags |= TW_DST;
146
147 tw.tw_zone = 0;
148
149 tw.tw_flags &= ~TW_SDAY;
150 tw.tw_flags |= TW_SEXP;
151 tw.tw_flags &= ~TW_SZONE;
152 tw.tw_flags |= TW_SZEXP;
153
154 tw.tw_clock = *clock;
155
156 return (&tw);
157 }
158
159
160 /*
161 ** Using a nmh "broken-down" time structure,
162 ** produce a 26-byte date/time string, such as
163 **
164 ** Tue Jan 14 17:49:03 1992\n\0
165 */
166
167 char *
dctime(struct tws * tw)168 dctime(struct tws *tw)
169 {
170 static char buffer[26];
171
172 if (!tw)
173 return NULL;
174
175 snprintf(buffer, sizeof(buffer), "%.3s %.3s %02d %02d:%02d:%02d %.4d\n",
176 tw_dotw[tw->tw_wday], tw_moty[tw->tw_mon], tw->tw_mday,
177 tw->tw_hour, tw->tw_min, tw->tw_sec,
178 tw->tw_year < 100 ? tw->tw_year + 1900 : tw->tw_year);
179
180 return buffer;
181 }
182
183
184 /*
185 ** Produce a date/time string of the form
186 **
187 ** Mon, 16 Jun 1992 15:30:48 -0700
188 **
189 ** for the current time, as specified by rfc822 and rfc1123.
190 */
191 char *
dtimenow(void)192 dtimenow(void)
193 {
194 time_t clock;
195
196 time(&clock);
197 return dtime(&clock);
198 }
199
200
201 /*
202 ** Using a local calendar time value, produce
203 ** a date/time string of the form
204 **
205 ** Mon, 16 Jun 1992 15:30:48 -0700
206 **
207 ** as specified by rfc822 and rfc1123.
208 */
209 char *
dtime(time_t * clock)210 dtime(time_t *clock)
211 {
212 return dasctime(dlocaltime(clock));
213 }
214
215
216 /*
217 ** Using a nmh "broken-down" time structure, produce
218 ** a date/time string of the form
219 **
220 ** Mon, 16 Jun 1992 15:30:48 -0700
221 **
222 ** as specified by rfc822 and rfc1123.
223 */
224 char *
dasctime(struct tws * tw)225 dasctime(struct tws *tw)
226 {
227 char buffer[80];
228 static char result[80];
229
230 if (!tw)
231 return NULL;
232
233 /* Display timezone if known */
234 if ((tw->tw_flags & TW_SZONE) == TW_SZNIL)
235 result[0] = '\0';
236 else
237 snprintf(result, sizeof(result), " %s", dtimezone(tw->tw_zone, tw->tw_flags));
238
239 snprintf(buffer, sizeof(buffer), "%02d %s %0*d %02d:%02d:%02d%s",
240 tw->tw_mday, tw_moty[tw->tw_mon],
241 tw->tw_year < 100 ? 2 : 4, tw->tw_year,
242 tw->tw_hour, tw->tw_min, tw->tw_sec, result);
243
244 if ((tw->tw_flags & TW_SDAY) == TW_SEXP)
245 snprintf(result, sizeof(result), "%s, %s", tw_dotw[tw->tw_wday], buffer);
246 else
247 if ((tw->tw_flags & TW_SDAY) == TW_SNIL)
248 strncpy(result, buffer, sizeof(result));
249 else
250 snprintf(result, sizeof(result), "%s (%s)", buffer, tw_dotw[tw->tw_wday]);
251
252 return result;
253 }
254
255
256 /*
257 ** Get the timezone for given offset as numeric value.
258 */
259 char *
dtimezone(int offset,int flags)260 dtimezone(int offset, int flags)
261 {
262 int hours, mins;
263 static char buffer[10];
264
265 if (offset < 0) {
266 mins = -((-offset) % 60);
267 hours = -((-offset) / 60);
268 } else {
269 mins = offset % 60;
270 hours = offset / 60;
271 }
272
273 #ifdef ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST
274 if (flags & TW_DST)
275 hours += 1;
276 #endif /* ADJUST_NUMERIC_ONLY_TZ_OFFSETS_WRT_DST */
277 snprintf(buffer, sizeof(buffer), "%s%02d%02d",
278 offset < 0 ? "-" : "+", abs(hours), abs(mins));
279 return buffer;
280 }
281
282
283 /*
284 ** Convert nmh time structure for local "broken-down"
285 ** time to calendar time (clock value). This routine
286 ** is based on the gtime() routine written by Steven Shafer
287 ** at CMU. It was forwarded to MTR by Jay Lepreau at Utah-CS.
288 */
289
290 time_t
dmktime(struct tws * tw)291 dmktime(struct tws *tw)
292 {
293 int i, sec, min, hour, mday, mon, year;
294 time_t result;
295
296 if (tw->tw_clock != 0)
297 return tw->tw_clock;
298
299 if ((sec = tw->tw_sec) < 0 || sec > 61
300 || (min = tw->tw_min) < 0 || min > 59
301 || (hour = tw->tw_hour) < 0 || hour > 23
302 || (mday = tw->tw_mday) < 1 || mday > 31
303 || (mon = tw->tw_mon + 1) < 1 || mon > 12)
304 return (tw->tw_clock = (time_t) -1);
305
306 year = tw->tw_year;
307
308 result = 0;
309 if (year < 1970)
310 year += 1900;
311
312 if (year < 1970)
313 year += 100;
314
315 for (i = 1970; i < year; i++)
316 result += dysize(i);
317 if (dysize(year) == 366 && mon >= 3)
318 result++;
319 while (--mon)
320 result += dmsize[mon - 1];
321 result += mday - 1;
322 result = 24 * result + hour;
323 result = 60 * result + min;
324 result = 60 * result + sec;
325 result -= 60 * tw->tw_zone;
326 if (tw->tw_flags & TW_DST)
327 result -= 60 * 60;
328
329 return (tw->tw_clock = result);
330 }
331
332
333 /*
334 ** Simple calculation of day of the week. Algorithm
335 ** used is Zeller's congruence. We assume that
336 ** if tw->tw_year < 100, then the century = 19.
337 */
338
339 void
set_dotw(struct tws * tw)340 set_dotw(struct tws *tw)
341 {
342 int month, day, year, century;
343
344 month = tw->tw_mon - 1;
345 day = tw->tw_mday;
346 year = tw->tw_year % 100;
347 century = tw->tw_year < 100 ? 19 : tw->tw_year / 100;
348
349 if (month <= 0) {
350 month += 12;
351 if (--year < 0) {
352 year += 100;
353 century--;
354 }
355 }
356
357 tw->tw_wday = ((26 * month - 2) / 10 + day + year + year / 4
358 - 3 * century / 4 + 1) % 7;
359 if (tw->tw_wday < 0)
360 tw->tw_wday += 7;
361
362 tw->tw_flags &= ~TW_SDAY, tw->tw_flags |= TW_SIMP;
363 }
364
365
366 /*
367 ** Copy nmh time structure
368 */
369
370 void
twscopy(struct tws * tb,struct tws * tw)371 twscopy(struct tws *tb, struct tws *tw)
372 {
373 *tb = *tw; /* struct copy */
374 }
375
376
377 /*
378 ** Compare two nmh time structures
379 */
380
381 int
twsort(struct tws * tw1,struct tws * tw2)382 twsort(struct tws *tw1, struct tws *tw2)
383 {
384 time_t c1, c2;
385
386 if (tw1->tw_clock == 0)
387 dmktime(tw1);
388 if (tw2->tw_clock == 0)
389 dmktime(tw2);
390
391 return ((c1 = tw1->tw_clock) > (c2 = tw2->tw_clock) ? 1
392 : c1 == c2 ? 0 : -1);
393 }
394