]>
Commit | Line | Data |
---|---|---|
ecee9d9e ET |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
6 | ||
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
9 | #include <string.h> | |
10 | #include <ctype.h> | |
11 | #include <time.h> | |
12 | ||
13 | static time_t my_mktime(struct tm *tm) | |
14 | { | |
15 | static const int mdays[] = { | |
16 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 | |
17 | }; | |
18 | int year = tm->tm_year - 70; | |
19 | int month = tm->tm_mon; | |
20 | int day = tm->tm_mday; | |
21 | ||
22 | if (year < 0 || year > 129) /* algo only works for 1970-2099 */ | |
23 | return -1; | |
24 | if (month < 0 || month > 11) /* array bounds */ | |
25 | return -1; | |
26 | if (month < 2 || (year + 2) % 4) | |
27 | day--; | |
28 | return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + | |
29 | tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; | |
30 | } | |
31 | ||
32 | static const char *month_names[] = { | |
89967023 LT |
33 | "January", "February", "March", "April", "May", "June", |
34 | "July", "August", "September", "October", "November", "December" | |
ecee9d9e ET |
35 | }; |
36 | ||
37 | static const char *weekday_names[] = { | |
89967023 | 38 | "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" |
ecee9d9e ET |
39 | }; |
40 | ||
f80cd783 LT |
41 | /* |
42 | * The "tz" thing is passed in as this strange "decimal parse of tz" | |
43 | * thing, which means that tz -0100 is passed in as the integer -100, | |
44 | * even though it means "sixty minutes off" | |
45 | */ | |
46 | const char *show_date(unsigned long time, int tz) | |
47 | { | |
48 | struct tm *tm; | |
49 | time_t t; | |
50 | static char timebuf[200]; | |
51 | int minutes; | |
52 | ||
53 | minutes = tz < 0 ? -tz : tz; | |
54 | minutes = (tz / 100)*60 + (tz % 100); | |
55 | minutes = tz < 0 ? -minutes : minutes; | |
56 | t = time - minutes * 60; | |
57 | tm = gmtime(&t); | |
58 | if (!tm) | |
59 | return NULL; | |
60 | sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d %+05d", | |
61 | weekday_names[tm->tm_wday], | |
62 | month_names[tm->tm_mon], | |
63 | tm->tm_mday, | |
64 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
65 | tm->tm_year + 1900, tz); | |
66 | return timebuf; | |
67 | } | |
68 | ||
89967023 LT |
69 | /* |
70 | * Check these. And note how it doesn't do the summer-time conversion. | |
71 | * | |
72 | * In my world, it's always summer, and things are probably a bit off | |
73 | * in other ways too. | |
74 | */ | |
75 | static const struct { | |
76 | const char *name; | |
77 | int offset; | |
5e2a78a4 | 78 | int dst; |
89967023 | 79 | } timezone_names[] = { |
5e2a78a4 LT |
80 | { "IDLW", -12, 0, }, /* International Date Line West */ |
81 | { "NT", -11, 0, }, /* Nome */ | |
82 | { "CAT", -10, 0, }, /* Central Alaska */ | |
83 | { "HST", -10, 0, }, /* Hawaii Standard */ | |
84 | { "HDT", -10, 1, }, /* Hawaii Daylight */ | |
85 | { "YST", -9, 0, }, /* Yukon Standard */ | |
86 | { "YDT", -9, 1, }, /* Yukon Daylight */ | |
87 | { "PST", -8, 0, }, /* Pacific Standard */ | |
88 | { "PDT", -8, 1, }, /* Pacific Daylight */ | |
89 | { "MST", -7, 0, }, /* Mountain Standard */ | |
90 | { "MDT", -7, 1, }, /* Mountain Daylight */ | |
91 | { "CST", -6, 0, }, /* Central Standard */ | |
92 | { "CDT", -6, 1, }, /* Central Daylight */ | |
93 | { "EST", -5, 0, }, /* Eastern Standard */ | |
94 | { "EDT", -5, 1, }, /* Eastern Daylight */ | |
95 | { "AST", -3, 0, }, /* Atlantic Standard */ | |
96 | { "ADT", -3, 1, }, /* Atlantic Daylight */ | |
97 | { "WAT", -1, 0, }, /* West Africa */ | |
98 | ||
99 | { "GMT", 0, 0, }, /* Greenwich Mean */ | |
100 | { "UTC", 0, 0, }, /* Universal (Coordinated) */ | |
101 | ||
102 | { "WET", 0, 0, }, /* Western European */ | |
103 | { "BST", 0, 1, }, /* British Summer */ | |
104 | { "CET", +1, 0, }, /* Central European */ | |
105 | { "MET", +1, 0, }, /* Middle European */ | |
106 | { "MEWT", +1, 0, }, /* Middle European Winter */ | |
107 | { "MEST", +1, 1, }, /* Middle European Summer */ | |
108 | { "CEST", +1, 1, }, /* Central European Summer */ | |
109 | { "MESZ", +1, 1, }, /* Middle European Summer */ | |
110 | { "FWT", +1, 0, }, /* French Winter */ | |
111 | { "FST", +1, 1, }, /* French Summer */ | |
112 | { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */ | |
92e2311b | 113 | { "EEST", +2, 1, }, /* Eastern European Daylight */ |
5e2a78a4 LT |
114 | { "WAST", +7, 0, }, /* West Australian Standard */ |
115 | { "WADT", +7, 1, }, /* West Australian Daylight */ | |
116 | { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */ | |
117 | { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */ | |
118 | { "EAST", +10, 0, }, /* Eastern Australian Standard */ | |
119 | { "EADT", +10, 1, }, /* Eastern Australian Daylight */ | |
120 | { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */ | |
121 | { "NZT", +11, 0, }, /* New Zealand */ | |
122 | { "NZST", +11, 0, }, /* New Zealand Standard */ | |
123 | { "NZDT", +11, 1, }, /* New Zealand Daylight */ | |
124 | { "IDLE", +12, 0, }, /* International Date Line East */ | |
89967023 | 125 | }; |
ecee9d9e | 126 | |
89967023 LT |
127 | #define NR_TZ (sizeof(timezone_names) / sizeof(timezone_names[0])) |
128 | ||
129 | static int match_string(const char *date, const char *str) | |
ecee9d9e | 130 | { |
89967023 LT |
131 | int i = 0; |
132 | ||
133 | for (i = 0; *date; date++, str++, i++) { | |
134 | if (*date == *str) | |
135 | continue; | |
136 | if (toupper(*date) == toupper(*str)) | |
137 | continue; | |
138 | if (!isalnum(*date)) | |
139 | break; | |
140 | return 0; | |
141 | } | |
142 | return i; | |
ecee9d9e ET |
143 | } |
144 | ||
68849b54 LT |
145 | static int skip_alpha(const char *date) |
146 | { | |
147 | int i = 0; | |
148 | do { | |
149 | i++; | |
150 | } while (isalpha(date[i])); | |
151 | return i; | |
152 | } | |
153 | ||
89967023 LT |
154 | /* |
155 | * Parse month, weekday, or timezone name | |
156 | */ | |
157 | static int match_alpha(const char *date, struct tm *tm, int *offset) | |
ecee9d9e | 158 | { |
89967023 | 159 | int i; |
ecee9d9e | 160 | |
89967023 LT |
161 | for (i = 0; i < 12; i++) { |
162 | int match = match_string(date, month_names[i]); | |
163 | if (match >= 3) { | |
164 | tm->tm_mon = i; | |
165 | return match; | |
ecee9d9e | 166 | } |
89967023 | 167 | } |
ecee9d9e | 168 | |
89967023 LT |
169 | for (i = 0; i < 7; i++) { |
170 | int match = match_string(date, weekday_names[i]); | |
171 | if (match >= 3) { | |
172 | tm->tm_wday = i; | |
173 | return match; | |
174 | } | |
175 | } | |
ecee9d9e | 176 | |
89967023 LT |
177 | for (i = 0; i < NR_TZ; i++) { |
178 | int match = match_string(date, timezone_names[i].name); | |
179 | if (match >= 3) { | |
5e2a78a4 LT |
180 | int off = timezone_names[i].offset; |
181 | ||
182 | /* This is bogus, but we like summer */ | |
183 | off += timezone_names[i].dst; | |
184 | ||
92e2311b LT |
185 | /* Only use the tz name offset if we don't have anything better */ |
186 | if (*offset == -1) | |
187 | *offset = 60*off; | |
188 | ||
89967023 | 189 | return match; |
ecee9d9e ET |
190 | } |
191 | } | |
ecee9d9e | 192 | |
68849b54 LT |
193 | if (match_string(date, "PM") == 2) { |
194 | if (tm->tm_hour > 0 && tm->tm_hour < 12) | |
195 | tm->tm_hour += 12; | |
196 | return 2; | |
197 | } | |
198 | ||
89967023 | 199 | /* BAD CRAP */ |
68849b54 | 200 | return skip_alpha(date); |
89967023 | 201 | } |
ecee9d9e | 202 | |
198b0fb6 | 203 | static int is_date(int year, int month, int day, struct tm *tm) |
89967023 | 204 | { |
198b0fb6 LT |
205 | if (month > 0 && month < 13 && day > 0 && day < 32) { |
206 | if (year == -1) { | |
207 | tm->tm_mon = month-1; | |
208 | tm->tm_mday = day; | |
209 | return 1; | |
89967023 | 210 | } |
198b0fb6 LT |
211 | if (year >= 1970 && year < 2100) { |
212 | year -= 1900; | |
213 | } else if (year > 70 && year < 100) { | |
214 | /* ok */ | |
215 | } else if (year < 38) { | |
216 | year += 100; | |
217 | } else | |
218 | return 0; | |
219 | ||
220 | tm->tm_mon = month-1; | |
221 | tm->tm_mday = day; | |
222 | tm->tm_year = year; | |
223 | return 1; | |
89967023 | 224 | } |
198b0fb6 LT |
225 | return 0; |
226 | } | |
ecee9d9e | 227 | |
198b0fb6 LT |
228 | static int match_multi_number(unsigned long num, char c, char *date, char *end, struct tm *tm) |
229 | { | |
230 | long num2, num3; | |
231 | ||
232 | num2 = strtol(end+1, &end, 10); | |
233 | num3 = -1; | |
234 | if (*end == c && isdigit(end[1])) | |
235 | num3 = strtol(end+1, &end, 10); | |
236 | ||
237 | /* Time? Date? */ | |
89967023 | 238 | switch (c) { |
198b0fb6 LT |
239 | case ':': |
240 | if (num3 < 0) | |
241 | num3 = 0; | |
242 | if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { | |
243 | tm->tm_hour = num; | |
244 | tm->tm_min = num2; | |
245 | tm->tm_sec = num3; | |
89967023 LT |
246 | break; |
247 | } | |
198b0fb6 | 248 | return 0; |
89967023 LT |
249 | |
250 | case '-': | |
251 | case '/': | |
198b0fb6 LT |
252 | if (num > 70) { |
253 | /* yyyy-mm-dd? */ | |
254 | if (is_date(num, num2, num3, tm)) | |
255 | break; | |
256 | /* yyyy-dd-mm? */ | |
257 | if (is_date(num, num3, num2, tm)) | |
89967023 | 258 | break; |
198b0fb6 LT |
259 | } |
260 | /* mm/dd/yy ? */ | |
261 | if (is_date(num3, num2, num, tm)) | |
89967023 | 262 | break; |
198b0fb6 LT |
263 | /* dd/mm/yy ? */ |
264 | if (is_date(num3, num, num2, tm)) | |
265 | break; | |
266 | return 0; | |
267 | } | |
268 | return end - date; | |
269 | } | |
270 | ||
271 | /* | |
272 | * We've seen a digit. Time? Year? Date? | |
273 | */ | |
274 | static int match_digit(char *date, struct tm *tm, int *offset) | |
275 | { | |
276 | int n; | |
277 | char *end; | |
278 | unsigned long num; | |
279 | ||
280 | num = strtoul(date, &end, 10); | |
281 | ||
282 | /* | |
283 | * Seconds since 1970? We trigger on that for anything after Jan 1, 2000 | |
284 | */ | |
285 | if (num > 946684800) { | |
286 | time_t time = num; | |
287 | if (gmtime_r(&time, tm)) | |
288 | return end - date; | |
289 | } | |
290 | ||
291 | /* | |
292 | * Check for special formats: num[:-/]num[same]num | |
293 | */ | |
294 | switch (*end) { | |
295 | case ':': | |
296 | case '/': | |
297 | case '-': | |
298 | if (isdigit(end[1])) { | |
299 | int match = match_multi_number(num, *end, date, end, tm); | |
300 | if (match) | |
301 | return match; | |
89967023 LT |
302 | } |
303 | } | |
198b0fb6 LT |
304 | |
305 | /* | |
306 | * None of the special formats? Try to guess what | |
307 | * the number meant. We use the number of digits | |
308 | * to make a more educated guess.. | |
309 | */ | |
310 | n = 0; | |
311 | do { | |
312 | n++; | |
313 | } while (isdigit(date[n])); | |
314 | ||
315 | /* Four-digit year or a timezone? */ | |
316 | if (n == 4) { | |
317 | if (num <= 1200 && *offset == -1) { | |
318 | unsigned int minutes = num % 100; | |
319 | unsigned int hours = num / 100; | |
320 | *offset = hours*60 + minutes; | |
321 | } else if (num > 1900 && num < 2100) | |
322 | tm->tm_year = num - 1900; | |
323 | return n; | |
324 | } | |
325 | ||
326 | /* | |
327 | * NOTE! We will give precedence to day-of-month over month or | |
328 | * year numebers in the 1-12 range. So 05 is always "mday 5", | |
329 | * unless we already have a mday.. | |
330 | * | |
331 | * IOW, 01 Apr 05 parses as "April 1st, 2005". | |
332 | */ | |
333 | if (num > 0 && num < 32 && tm->tm_mday < 0) { | |
334 | tm->tm_mday = num; | |
335 | return n; | |
336 | } | |
337 | ||
338 | /* Two-digit year? */ | |
339 | if (n == 2 && tm->tm_year < 0) { | |
340 | if (num < 10 && tm->tm_mday >= 0) { | |
341 | tm->tm_year = num + 100; | |
342 | return n; | |
343 | } | |
344 | if (num >= 70) { | |
345 | tm->tm_year = num; | |
346 | return n; | |
347 | } | |
348 | } | |
349 | ||
350 | if (num > 0 && num < 32) { | |
351 | tm->tm_mday = num; | |
352 | } else if (num > 1900) { | |
353 | tm->tm_year = num - 1900; | |
354 | } else if (num > 70) { | |
355 | tm->tm_year = num; | |
356 | } else if (num > 0 && num < 13) { | |
357 | tm->tm_mon = num-1; | |
358 | } | |
ecee9d9e | 359 | |
198b0fb6 | 360 | return n; |
89967023 | 361 | } |
ecee9d9e | 362 | |
89967023 LT |
363 | static int match_tz(char *date, int *offp) |
364 | { | |
365 | char *end; | |
366 | int offset = strtoul(date+1, &end, 10); | |
367 | int min, hour; | |
198b0fb6 | 368 | int n = end - date - 1; |
ecee9d9e | 369 | |
89967023 LT |
370 | min = offset % 100; |
371 | hour = offset / 100; | |
ecee9d9e | 372 | |
198b0fb6 LT |
373 | /* |
374 | * Don't accept any random crap.. At least 3 digits, and | |
375 | * a valid minute. We might want to check that the minutes | |
376 | * are divisible by 30 or something too. | |
377 | */ | |
68849b54 LT |
378 | if (min < 60 && n > 2) { |
379 | offset = hour*60+min; | |
380 | if (*date == '-') | |
381 | offset = -offset; | |
ecee9d9e | 382 | |
68849b54 LT |
383 | *offp = offset; |
384 | } | |
89967023 LT |
385 | return end - date; |
386 | } | |
ecee9d9e | 387 | |
89967023 LT |
388 | /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 |
389 | (i.e. English) day/month names, and it doesn't work correctly with %z. */ | |
390 | void parse_date(char *date, char *result, int maxlen) | |
391 | { | |
392 | struct tm tm; | |
7f26664f | 393 | int offset, sign; |
89967023 | 394 | time_t then; |
ecee9d9e | 395 | |
89967023 LT |
396 | memset(&tm, 0, sizeof(tm)); |
397 | tm.tm_year = -1; | |
398 | tm.tm_mon = -1; | |
399 | tm.tm_mday = -1; | |
eaa85129 LT |
400 | tm.tm_isdst = -1; |
401 | offset = -1; | |
89967023 LT |
402 | |
403 | for (;;) { | |
404 | int match = 0; | |
405 | unsigned char c = *date; | |
406 | ||
407 | /* Stop at end of string or newline */ | |
408 | if (!c || c == '\n') | |
409 | break; | |
410 | ||
411 | if (isalpha(c)) | |
412 | match = match_alpha(date, &tm, &offset); | |
413 | else if (isdigit(c)) | |
414 | match = match_digit(date, &tm, &offset); | |
415 | else if ((c == '-' || c == '+') && isdigit(date[1])) | |
416 | match = match_tz(date, &offset); | |
417 | ||
418 | if (!match) { | |
419 | /* BAD CRAP */ | |
420 | match = 1; | |
421 | } | |
422 | ||
423 | date += match; | |
424 | } | |
ecee9d9e | 425 | |
eaa85129 LT |
426 | /* mktime uses local timezone */ |
427 | then = my_mktime(&tm); | |
428 | if (offset == -1) | |
429 | offset = (then - mktime(&tm)) / 60; | |
430 | ||
ecee9d9e ET |
431 | if (then == -1) |
432 | return; | |
433 | ||
89967023 | 434 | then -= offset * 60; |
ecee9d9e | 435 | |
7f26664f LT |
436 | sign = '+'; |
437 | if (offset < 0) { | |
438 | offset = -offset; | |
439 | sign = '-'; | |
440 | } | |
441 | ||
442 | snprintf(result, maxlen, "%lu %c%02d%02d", then, sign, offset/60, offset % 60); | |
ecee9d9e ET |
443 | } |
444 | ||
445 | void datestamp(char *buf, int bufsize) | |
446 | { | |
447 | time_t now; | |
448 | int offset; | |
449 | ||
450 | time(&now); | |
451 | ||
452 | offset = my_mktime(localtime(&now)) - now; | |
453 | offset /= 60; | |
454 | ||
455 | snprintf(buf, bufsize, "%lu %+05d", now, offset/60*100 + offset%60); | |
456 | } |