]>
Commit | Line | Data |
---|---|---|
ecee9d9e ET |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
6 | ||
e99d59ff LT |
7 | #include "cache.h" |
8 | ||
ecee9d9e ET |
9 | static time_t my_mktime(struct tm *tm) |
10 | { | |
11 | static const int mdays[] = { | |
12 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 | |
13 | }; | |
14 | int year = tm->tm_year - 70; | |
15 | int month = tm->tm_mon; | |
16 | int day = tm->tm_mday; | |
17 | ||
18 | if (year < 0 || year > 129) /* algo only works for 1970-2099 */ | |
19 | return -1; | |
20 | if (month < 0 || month > 11) /* array bounds */ | |
21 | return -1; | |
22 | if (month < 2 || (year + 2) % 4) | |
23 | day--; | |
24 | return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + | |
25 | tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; | |
26 | } | |
27 | ||
28 | static const char *month_names[] = { | |
89967023 LT |
29 | "January", "February", "March", "April", "May", "June", |
30 | "July", "August", "September", "October", "November", "December" | |
ecee9d9e ET |
31 | }; |
32 | ||
33 | static const char *weekday_names[] = { | |
6b7b0427 | 34 | "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays" |
ecee9d9e ET |
35 | }; |
36 | ||
9a8e35e9 LT |
37 | static time_t gm_time_t(unsigned long time, int tz) |
38 | { | |
39 | int minutes; | |
40 | ||
41 | minutes = tz < 0 ? -tz : tz; | |
42 | minutes = (minutes / 100)*60 + (minutes % 100); | |
43 | minutes = tz < 0 ? -minutes : minutes; | |
44 | return time + minutes * 60; | |
45 | } | |
46 | ||
f80cd783 LT |
47 | /* |
48 | * The "tz" thing is passed in as this strange "decimal parse of tz" | |
49 | * thing, which means that tz -0100 is passed in as the integer -100, | |
50 | * even though it means "sixty minutes off" | |
51 | */ | |
2a387043 | 52 | static struct tm *time_to_tm(unsigned long time, int tz) |
f80cd783 | 53 | { |
9a8e35e9 | 54 | time_t t = gm_time_t(time, tz); |
2a387043 JH |
55 | return gmtime(&t); |
56 | } | |
57 | ||
a7b02ccf JH |
58 | /* |
59 | * What value of "tz" was in effect back then at "time" in the | |
60 | * local timezone? | |
61 | */ | |
62 | static int local_tzoffset(unsigned long time) | |
63 | { | |
64 | time_t t, t_local; | |
65 | struct tm tm; | |
66 | int offset, eastwest; | |
67 | ||
68 | t = time; | |
69 | localtime_r(&t, &tm); | |
70 | t_local = my_mktime(&tm); | |
71 | ||
72 | if (t_local < t) { | |
73 | eastwest = -1; | |
74 | offset = t - t_local; | |
75 | } else { | |
76 | eastwest = 1; | |
77 | offset = t_local - t; | |
78 | } | |
79 | offset /= 60; /* in minutes */ | |
80 | offset = (offset % 60) + ((offset / 60) * 100); | |
81 | return offset * eastwest; | |
82 | } | |
83 | ||
f8493ec0 | 84 | const char *show_date(unsigned long time, int tz, enum date_mode mode) |
2a387043 JH |
85 | { |
86 | struct tm *tm; | |
87 | static char timebuf[200]; | |
88 | ||
f8493ec0 | 89 | if (mode == DATE_RELATIVE) { |
9a8e35e9 | 90 | unsigned long diff; |
9a8e35e9 LT |
91 | struct timeval now; |
92 | gettimeofday(&now, NULL); | |
da8f070c | 93 | if (now.tv_sec < time) |
9a8e35e9 | 94 | return "in the future"; |
da8f070c | 95 | diff = now.tv_sec - time; |
9a8e35e9 LT |
96 | if (diff < 90) { |
97 | snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff); | |
98 | return timebuf; | |
99 | } | |
100 | /* Turn it into minutes */ | |
101 | diff = (diff + 30) / 60; | |
102 | if (diff < 90) { | |
103 | snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff); | |
104 | return timebuf; | |
105 | } | |
106 | /* Turn it into hours */ | |
107 | diff = (diff + 30) / 60; | |
108 | if (diff < 36) { | |
109 | snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff); | |
110 | return timebuf; | |
111 | } | |
112 | /* We deal with number of days from here on */ | |
113 | diff = (diff + 12) / 24; | |
114 | if (diff < 14) { | |
115 | snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff); | |
116 | return timebuf; | |
117 | } | |
118 | /* Say weeks for the past 10 weeks or so */ | |
119 | if (diff < 70) { | |
120 | snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7); | |
121 | return timebuf; | |
122 | } | |
123 | /* Say months for the past 12 months or so */ | |
124 | if (diff < 360) { | |
125 | snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30); | |
126 | return timebuf; | |
127 | } | |
128 | /* Else fall back on absolute format.. */ | |
129 | } | |
130 | ||
a7b02ccf JH |
131 | if (mode == DATE_LOCAL) |
132 | tz = local_tzoffset(time); | |
133 | ||
2a387043 | 134 | tm = time_to_tm(time, tz); |
f80cd783 LT |
135 | if (!tm) |
136 | return NULL; | |
f8493ec0 JS |
137 | if (mode == DATE_SHORT) |
138 | sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900, | |
139 | tm->tm_mon + 1, tm->tm_mday); | |
ee8f838e RR |
140 | else if (mode == DATE_ISO8601) |
141 | sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d", | |
142 | tm->tm_year + 1900, | |
143 | tm->tm_mon + 1, | |
144 | tm->tm_mday, | |
145 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
146 | tz); | |
f8493ec0 | 147 | else |
a7b02ccf | 148 | sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d", |
f8493ec0 JS |
149 | weekday_names[tm->tm_wday], |
150 | month_names[tm->tm_mon], | |
151 | tm->tm_mday, | |
152 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
a7b02ccf JH |
153 | tm->tm_year + 1900, |
154 | (mode == DATE_LOCAL) ? 0 : ' ', | |
155 | tz); | |
f80cd783 LT |
156 | return timebuf; |
157 | } | |
158 | ||
2a387043 JH |
159 | const char *show_rfc2822_date(unsigned long time, int tz) |
160 | { | |
161 | struct tm *tm; | |
162 | static char timebuf[200]; | |
163 | ||
164 | tm = time_to_tm(time, tz); | |
165 | if (!tm) | |
166 | return NULL; | |
167 | sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d", | |
168 | weekday_names[tm->tm_wday], tm->tm_mday, | |
169 | month_names[tm->tm_mon], tm->tm_year + 1900, | |
170 | tm->tm_hour, tm->tm_min, tm->tm_sec, tz); | |
171 | return timebuf; | |
172 | } | |
173 | ||
89967023 LT |
174 | /* |
175 | * Check these. And note how it doesn't do the summer-time conversion. | |
176 | * | |
177 | * In my world, it's always summer, and things are probably a bit off | |
178 | * in other ways too. | |
179 | */ | |
180 | static const struct { | |
181 | const char *name; | |
182 | int offset; | |
5e2a78a4 | 183 | int dst; |
89967023 | 184 | } timezone_names[] = { |
5e2a78a4 LT |
185 | { "IDLW", -12, 0, }, /* International Date Line West */ |
186 | { "NT", -11, 0, }, /* Nome */ | |
187 | { "CAT", -10, 0, }, /* Central Alaska */ | |
188 | { "HST", -10, 0, }, /* Hawaii Standard */ | |
189 | { "HDT", -10, 1, }, /* Hawaii Daylight */ | |
190 | { "YST", -9, 0, }, /* Yukon Standard */ | |
191 | { "YDT", -9, 1, }, /* Yukon Daylight */ | |
192 | { "PST", -8, 0, }, /* Pacific Standard */ | |
193 | { "PDT", -8, 1, }, /* Pacific Daylight */ | |
194 | { "MST", -7, 0, }, /* Mountain Standard */ | |
195 | { "MDT", -7, 1, }, /* Mountain Daylight */ | |
196 | { "CST", -6, 0, }, /* Central Standard */ | |
197 | { "CDT", -6, 1, }, /* Central Daylight */ | |
198 | { "EST", -5, 0, }, /* Eastern Standard */ | |
199 | { "EDT", -5, 1, }, /* Eastern Daylight */ | |
200 | { "AST", -3, 0, }, /* Atlantic Standard */ | |
201 | { "ADT", -3, 1, }, /* Atlantic Daylight */ | |
202 | { "WAT", -1, 0, }, /* West Africa */ | |
203 | ||
204 | { "GMT", 0, 0, }, /* Greenwich Mean */ | |
205 | { "UTC", 0, 0, }, /* Universal (Coordinated) */ | |
206 | ||
207 | { "WET", 0, 0, }, /* Western European */ | |
208 | { "BST", 0, 1, }, /* British Summer */ | |
209 | { "CET", +1, 0, }, /* Central European */ | |
210 | { "MET", +1, 0, }, /* Middle European */ | |
211 | { "MEWT", +1, 0, }, /* Middle European Winter */ | |
212 | { "MEST", +1, 1, }, /* Middle European Summer */ | |
213 | { "CEST", +1, 1, }, /* Central European Summer */ | |
214 | { "MESZ", +1, 1, }, /* Middle European Summer */ | |
215 | { "FWT", +1, 0, }, /* French Winter */ | |
216 | { "FST", +1, 1, }, /* French Summer */ | |
217 | { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */ | |
92e2311b | 218 | { "EEST", +2, 1, }, /* Eastern European Daylight */ |
5e2a78a4 LT |
219 | { "WAST", +7, 0, }, /* West Australian Standard */ |
220 | { "WADT", +7, 1, }, /* West Australian Daylight */ | |
221 | { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */ | |
222 | { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */ | |
223 | { "EAST", +10, 0, }, /* Eastern Australian Standard */ | |
224 | { "EADT", +10, 1, }, /* Eastern Australian Daylight */ | |
225 | { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */ | |
226 | { "NZT", +11, 0, }, /* New Zealand */ | |
227 | { "NZST", +11, 0, }, /* New Zealand Standard */ | |
228 | { "NZDT", +11, 1, }, /* New Zealand Daylight */ | |
229 | { "IDLE", +12, 0, }, /* International Date Line East */ | |
89967023 | 230 | }; |
ecee9d9e | 231 | |
89967023 | 232 | static int match_string(const char *date, const char *str) |
ecee9d9e | 233 | { |
89967023 LT |
234 | int i = 0; |
235 | ||
236 | for (i = 0; *date; date++, str++, i++) { | |
237 | if (*date == *str) | |
238 | continue; | |
239 | if (toupper(*date) == toupper(*str)) | |
240 | continue; | |
241 | if (!isalnum(*date)) | |
242 | break; | |
243 | return 0; | |
244 | } | |
245 | return i; | |
ecee9d9e ET |
246 | } |
247 | ||
68849b54 LT |
248 | static int skip_alpha(const char *date) |
249 | { | |
250 | int i = 0; | |
251 | do { | |
252 | i++; | |
253 | } while (isalpha(date[i])); | |
254 | return i; | |
255 | } | |
256 | ||
89967023 LT |
257 | /* |
258 | * Parse month, weekday, or timezone name | |
259 | */ | |
260 | static int match_alpha(const char *date, struct tm *tm, int *offset) | |
ecee9d9e | 261 | { |
89967023 | 262 | int i; |
ecee9d9e | 263 | |
89967023 LT |
264 | for (i = 0; i < 12; i++) { |
265 | int match = match_string(date, month_names[i]); | |
266 | if (match >= 3) { | |
267 | tm->tm_mon = i; | |
268 | return match; | |
ecee9d9e | 269 | } |
89967023 | 270 | } |
ecee9d9e | 271 | |
89967023 LT |
272 | for (i = 0; i < 7; i++) { |
273 | int match = match_string(date, weekday_names[i]); | |
274 | if (match >= 3) { | |
275 | tm->tm_wday = i; | |
276 | return match; | |
277 | } | |
278 | } | |
ecee9d9e | 279 | |
b4f2a6ac | 280 | for (i = 0; i < ARRAY_SIZE(timezone_names); i++) { |
89967023 LT |
281 | int match = match_string(date, timezone_names[i].name); |
282 | if (match >= 3) { | |
5e2a78a4 LT |
283 | int off = timezone_names[i].offset; |
284 | ||
285 | /* This is bogus, but we like summer */ | |
286 | off += timezone_names[i].dst; | |
287 | ||
92e2311b LT |
288 | /* Only use the tz name offset if we don't have anything better */ |
289 | if (*offset == -1) | |
290 | *offset = 60*off; | |
291 | ||
89967023 | 292 | return match; |
ecee9d9e ET |
293 | } |
294 | } | |
ecee9d9e | 295 | |
68849b54 | 296 | if (match_string(date, "PM") == 2) { |
18b633ca LT |
297 | tm->tm_hour = (tm->tm_hour % 12) + 12; |
298 | return 2; | |
299 | } | |
300 | ||
301 | if (match_string(date, "AM") == 2) { | |
302 | tm->tm_hour = (tm->tm_hour % 12) + 0; | |
68849b54 LT |
303 | return 2; |
304 | } | |
305 | ||
89967023 | 306 | /* BAD CRAP */ |
68849b54 | 307 | return skip_alpha(date); |
89967023 | 308 | } |
ecee9d9e | 309 | |
38035cf4 | 310 | static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) |
89967023 | 311 | { |
198b0fb6 | 312 | if (month > 0 && month < 13 && day > 0 && day < 32) { |
38035cf4 JH |
313 | struct tm check = *tm; |
314 | struct tm *r = (now_tm ? &check : tm); | |
315 | time_t specified; | |
316 | ||
317 | r->tm_mon = month - 1; | |
318 | r->tm_mday = day; | |
198b0fb6 | 319 | if (year == -1) { |
38035cf4 JH |
320 | if (!now_tm) |
321 | return 1; | |
322 | r->tm_year = now_tm->tm_year; | |
89967023 | 323 | } |
38035cf4 JH |
324 | else if (year >= 1970 && year < 2100) |
325 | r->tm_year = year - 1900; | |
326 | else if (year > 70 && year < 100) | |
327 | r->tm_year = year; | |
328 | else if (year < 38) | |
329 | r->tm_year = year + 100; | |
330 | else | |
198b0fb6 | 331 | return 0; |
38035cf4 JH |
332 | if (!now_tm) |
333 | return 1; | |
334 | ||
335 | specified = my_mktime(r); | |
198b0fb6 | 336 | |
38035cf4 JH |
337 | /* Be it commit time or author time, it does not make |
338 | * sense to specify timestamp way into the future. Make | |
339 | * sure it is not later than ten days from now... | |
340 | */ | |
341 | if (now + 10*24*3600 < specified) | |
342 | return 0; | |
343 | tm->tm_mon = r->tm_mon; | |
344 | tm->tm_mday = r->tm_mday; | |
345 | if (year != -1) | |
346 | tm->tm_year = r->tm_year; | |
198b0fb6 | 347 | return 1; |
89967023 | 348 | } |
198b0fb6 LT |
349 | return 0; |
350 | } | |
ecee9d9e | 351 | |
26a2d8ae | 352 | static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm) |
198b0fb6 | 353 | { |
38035cf4 JH |
354 | time_t now; |
355 | struct tm now_tm; | |
356 | struct tm *refuse_future; | |
198b0fb6 LT |
357 | long num2, num3; |
358 | ||
359 | num2 = strtol(end+1, &end, 10); | |
360 | num3 = -1; | |
361 | if (*end == c && isdigit(end[1])) | |
362 | num3 = strtol(end+1, &end, 10); | |
363 | ||
364 | /* Time? Date? */ | |
89967023 | 365 | switch (c) { |
198b0fb6 LT |
366 | case ':': |
367 | if (num3 < 0) | |
368 | num3 = 0; | |
369 | if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { | |
370 | tm->tm_hour = num; | |
371 | tm->tm_min = num2; | |
372 | tm->tm_sec = num3; | |
89967023 LT |
373 | break; |
374 | } | |
198b0fb6 | 375 | return 0; |
89967023 LT |
376 | |
377 | case '-': | |
378 | case '/': | |
38035cf4 JH |
379 | case '.': |
380 | now = time(NULL); | |
381 | refuse_future = NULL; | |
382 | if (gmtime_r(&now, &now_tm)) | |
383 | refuse_future = &now_tm; | |
384 | ||
198b0fb6 LT |
385 | if (num > 70) { |
386 | /* yyyy-mm-dd? */ | |
38035cf4 | 387 | if (is_date(num, num2, num3, refuse_future, now, tm)) |
198b0fb6 LT |
388 | break; |
389 | /* yyyy-dd-mm? */ | |
38035cf4 | 390 | if (is_date(num, num3, num2, refuse_future, now, tm)) |
89967023 | 391 | break; |
198b0fb6 | 392 | } |
38035cf4 JH |
393 | /* Our eastern European friends say dd.mm.yy[yy] |
394 | * is the norm there, so giving precedence to | |
395 | * mm/dd/yy[yy] form only when separator is not '.' | |
396 | */ | |
397 | if (c != '.' && | |
398 | is_date(num3, num, num2, refuse_future, now, tm)) | |
399 | break; | |
400 | /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ | |
401 | if (is_date(num3, num2, num, refuse_future, now, tm)) | |
89967023 | 402 | break; |
38035cf4 JH |
403 | /* Funny European mm.dd.yy */ |
404 | if (c == '.' && | |
405 | is_date(num3, num, num2, refuse_future, now, tm)) | |
198b0fb6 LT |
406 | break; |
407 | return 0; | |
408 | } | |
409 | return end - date; | |
410 | } | |
411 | ||
412 | /* | |
a6080a0a | 413 | * We've seen a digit. Time? Year? Date? |
198b0fb6 | 414 | */ |
26a2d8ae | 415 | static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt) |
198b0fb6 LT |
416 | { |
417 | int n; | |
418 | char *end; | |
419 | unsigned long num; | |
420 | ||
421 | num = strtoul(date, &end, 10); | |
422 | ||
423 | /* | |
a1a5a634 JS |
424 | * Seconds since 1970? We trigger on that for any numbers with |
425 | * more than 8 digits. This is because we don't want to rule out | |
426 | * numbers like 20070606 as a YYYYMMDD date. | |
198b0fb6 | 427 | */ |
a1a5a634 | 428 | if (num >= 100000000) { |
198b0fb6 | 429 | time_t time = num; |
9cb480f2 JH |
430 | if (gmtime_r(&time, tm)) { |
431 | *tm_gmt = 1; | |
198b0fb6 | 432 | return end - date; |
9cb480f2 | 433 | } |
198b0fb6 LT |
434 | } |
435 | ||
436 | /* | |
38035cf4 | 437 | * Check for special formats: num[-.:/]num[same]num |
198b0fb6 LT |
438 | */ |
439 | switch (*end) { | |
440 | case ':': | |
38035cf4 | 441 | case '.': |
198b0fb6 LT |
442 | case '/': |
443 | case '-': | |
444 | if (isdigit(end[1])) { | |
445 | int match = match_multi_number(num, *end, date, end, tm); | |
446 | if (match) | |
447 | return match; | |
89967023 LT |
448 | } |
449 | } | |
198b0fb6 LT |
450 | |
451 | /* | |
452 | * None of the special formats? Try to guess what | |
453 | * the number meant. We use the number of digits | |
454 | * to make a more educated guess.. | |
455 | */ | |
456 | n = 0; | |
457 | do { | |
458 | n++; | |
459 | } while (isdigit(date[n])); | |
460 | ||
461 | /* Four-digit year or a timezone? */ | |
462 | if (n == 4) { | |
7122f82f | 463 | if (num <= 1400 && *offset == -1) { |
198b0fb6 LT |
464 | unsigned int minutes = num % 100; |
465 | unsigned int hours = num / 100; | |
466 | *offset = hours*60 + minutes; | |
467 | } else if (num > 1900 && num < 2100) | |
468 | tm->tm_year = num - 1900; | |
469 | return n; | |
470 | } | |
471 | ||
472 | /* | |
473 | * NOTE! We will give precedence to day-of-month over month or | |
82f9d58a | 474 | * year numbers in the 1-12 range. So 05 is always "mday 5", |
198b0fb6 LT |
475 | * unless we already have a mday.. |
476 | * | |
477 | * IOW, 01 Apr 05 parses as "April 1st, 2005". | |
478 | */ | |
479 | if (num > 0 && num < 32 && tm->tm_mday < 0) { | |
480 | tm->tm_mday = num; | |
481 | return n; | |
482 | } | |
483 | ||
484 | /* Two-digit year? */ | |
485 | if (n == 2 && tm->tm_year < 0) { | |
486 | if (num < 10 && tm->tm_mday >= 0) { | |
487 | tm->tm_year = num + 100; | |
488 | return n; | |
489 | } | |
490 | if (num >= 70) { | |
491 | tm->tm_year = num; | |
492 | return n; | |
493 | } | |
494 | } | |
495 | ||
496 | if (num > 0 && num < 32) { | |
497 | tm->tm_mday = num; | |
498 | } else if (num > 1900) { | |
499 | tm->tm_year = num - 1900; | |
500 | } else if (num > 70) { | |
501 | tm->tm_year = num; | |
502 | } else if (num > 0 && num < 13) { | |
503 | tm->tm_mon = num-1; | |
504 | } | |
a6080a0a | 505 | |
198b0fb6 | 506 | return n; |
89967023 | 507 | } |
ecee9d9e | 508 | |
26a2d8ae | 509 | static int match_tz(const char *date, int *offp) |
89967023 LT |
510 | { |
511 | char *end; | |
512 | int offset = strtoul(date+1, &end, 10); | |
513 | int min, hour; | |
198b0fb6 | 514 | int n = end - date - 1; |
ecee9d9e | 515 | |
89967023 LT |
516 | min = offset % 100; |
517 | hour = offset / 100; | |
ecee9d9e | 518 | |
198b0fb6 LT |
519 | /* |
520 | * Don't accept any random crap.. At least 3 digits, and | |
521 | * a valid minute. We might want to check that the minutes | |
522 | * are divisible by 30 or something too. | |
523 | */ | |
68849b54 LT |
524 | if (min < 60 && n > 2) { |
525 | offset = hour*60+min; | |
526 | if (*date == '-') | |
527 | offset = -offset; | |
ecee9d9e | 528 | |
68849b54 LT |
529 | *offp = offset; |
530 | } | |
89967023 LT |
531 | return end - date; |
532 | } | |
ecee9d9e | 533 | |
01c6ad29 LT |
534 | static int date_string(unsigned long date, int offset, char *buf, int len) |
535 | { | |
536 | int sign = '+'; | |
537 | ||
538 | if (offset < 0) { | |
539 | offset = -offset; | |
540 | sign = '-'; | |
541 | } | |
542 | return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60); | |
543 | } | |
544 | ||
89967023 LT |
545 | /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 |
546 | (i.e. English) day/month names, and it doesn't work correctly with %z. */ | |
2a39064c | 547 | int parse_date(const char *date, char *result, int maxlen) |
89967023 LT |
548 | { |
549 | struct tm tm; | |
01c6ad29 | 550 | int offset, tm_gmt; |
89967023 | 551 | time_t then; |
ecee9d9e | 552 | |
89967023 LT |
553 | memset(&tm, 0, sizeof(tm)); |
554 | tm.tm_year = -1; | |
555 | tm.tm_mon = -1; | |
556 | tm.tm_mday = -1; | |
eaa85129 LT |
557 | tm.tm_isdst = -1; |
558 | offset = -1; | |
9cb480f2 | 559 | tm_gmt = 0; |
89967023 LT |
560 | |
561 | for (;;) { | |
562 | int match = 0; | |
563 | unsigned char c = *date; | |
564 | ||
565 | /* Stop at end of string or newline */ | |
566 | if (!c || c == '\n') | |
567 | break; | |
568 | ||
569 | if (isalpha(c)) | |
570 | match = match_alpha(date, &tm, &offset); | |
571 | else if (isdigit(c)) | |
9cb480f2 | 572 | match = match_digit(date, &tm, &offset, &tm_gmt); |
89967023 LT |
573 | else if ((c == '-' || c == '+') && isdigit(date[1])) |
574 | match = match_tz(date, &offset); | |
575 | ||
576 | if (!match) { | |
577 | /* BAD CRAP */ | |
578 | match = 1; | |
a6080a0a | 579 | } |
89967023 LT |
580 | |
581 | date += match; | |
582 | } | |
ecee9d9e | 583 | |
eaa85129 | 584 | /* mktime uses local timezone */ |
a6080a0a | 585 | then = my_mktime(&tm); |
eaa85129 LT |
586 | if (offset == -1) |
587 | offset = (then - mktime(&tm)) / 60; | |
588 | ||
ecee9d9e | 589 | if (then == -1) |
2a39064c | 590 | return -1; |
ecee9d9e | 591 | |
9cb480f2 JH |
592 | if (!tm_gmt) |
593 | then -= offset * 60; | |
01c6ad29 | 594 | return date_string(then, offset, result, maxlen); |
ecee9d9e ET |
595 | } |
596 | ||
597 | void datestamp(char *buf, int bufsize) | |
598 | { | |
599 | time_t now; | |
600 | int offset; | |
601 | ||
602 | time(&now); | |
603 | ||
604 | offset = my_mktime(localtime(&now)) - now; | |
605 | offset /= 60; | |
606 | ||
01c6ad29 | 607 | date_string(now, offset, buf, bufsize); |
ecee9d9e | 608 | } |
3c07b1d1 LT |
609 | |
610 | static void update_tm(struct tm *tm, unsigned long sec) | |
611 | { | |
612 | time_t n = mktime(tm) - sec; | |
613 | localtime_r(&n, tm); | |
614 | } | |
615 | ||
a8aca418 LT |
616 | static void date_yesterday(struct tm *tm, int *num) |
617 | { | |
618 | update_tm(tm, 24*60*60); | |
619 | } | |
620 | ||
621 | static void date_time(struct tm *tm, int hour) | |
622 | { | |
623 | if (tm->tm_hour < hour) | |
624 | date_yesterday(tm, NULL); | |
625 | tm->tm_hour = hour; | |
626 | tm->tm_min = 0; | |
627 | tm->tm_sec = 0; | |
628 | } | |
629 | ||
630 | static void date_midnight(struct tm *tm, int *num) | |
631 | { | |
632 | date_time(tm, 0); | |
633 | } | |
634 | ||
635 | static void date_noon(struct tm *tm, int *num) | |
636 | { | |
637 | date_time(tm, 12); | |
638 | } | |
639 | ||
640 | static void date_tea(struct tm *tm, int *num) | |
641 | { | |
642 | date_time(tm, 17); | |
643 | } | |
644 | ||
393d340e LT |
645 | static void date_pm(struct tm *tm, int *num) |
646 | { | |
18b633ca | 647 | int hour, n = *num; |
393d340e LT |
648 | *num = 0; |
649 | ||
18b633ca LT |
650 | hour = tm->tm_hour; |
651 | if (n) { | |
652 | hour = n; | |
393d340e LT |
653 | tm->tm_min = 0; |
654 | tm->tm_sec = 0; | |
655 | } | |
18b633ca | 656 | tm->tm_hour = (hour % 12) + 12; |
393d340e LT |
657 | } |
658 | ||
659 | static void date_am(struct tm *tm, int *num) | |
660 | { | |
18b633ca | 661 | int hour, n = *num; |
393d340e LT |
662 | *num = 0; |
663 | ||
18b633ca LT |
664 | hour = tm->tm_hour; |
665 | if (n) { | |
666 | hour = n; | |
393d340e LT |
667 | tm->tm_min = 0; |
668 | tm->tm_sec = 0; | |
669 | } | |
18b633ca | 670 | tm->tm_hour = (hour % 12); |
393d340e LT |
671 | } |
672 | ||
a8aca418 LT |
673 | static const struct special { |
674 | const char *name; | |
675 | void (*fn)(struct tm *, int *); | |
676 | } special[] = { | |
677 | { "yesterday", date_yesterday }, | |
678 | { "noon", date_noon }, | |
679 | { "midnight", date_midnight }, | |
680 | { "tea", date_tea }, | |
393d340e LT |
681 | { "PM", date_pm }, |
682 | { "AM", date_am }, | |
a8aca418 LT |
683 | { NULL } |
684 | }; | |
685 | ||
3c07b1d1 LT |
686 | static const char *number_name[] = { |
687 | "zero", "one", "two", "three", "four", | |
688 | "five", "six", "seven", "eight", "nine", "ten", | |
689 | }; | |
690 | ||
a8aca418 | 691 | static const struct typelen { |
3c07b1d1 LT |
692 | const char *type; |
693 | int length; | |
694 | } typelen[] = { | |
695 | { "seconds", 1 }, | |
696 | { "minutes", 60 }, | |
697 | { "hours", 60*60 }, | |
698 | { "days", 24*60*60 }, | |
699 | { "weeks", 7*24*60*60 }, | |
700 | { NULL } | |
a6080a0a | 701 | }; |
3c07b1d1 LT |
702 | |
703 | static const char *approxidate_alpha(const char *date, struct tm *tm, int *num) | |
704 | { | |
a8aca418 LT |
705 | const struct typelen *tl; |
706 | const struct special *s; | |
3c07b1d1 | 707 | const char *end = date; |
5df7dbba | 708 | int i; |
3c07b1d1 | 709 | |
5df7dbba PH |
710 | while (isalpha(*++end)); |
711 | ; | |
3c07b1d1 LT |
712 | |
713 | for (i = 0; i < 12; i++) { | |
714 | int match = match_string(date, month_names[i]); | |
715 | if (match >= 3) { | |
716 | tm->tm_mon = i; | |
717 | return end; | |
718 | } | |
719 | } | |
720 | ||
a8aca418 LT |
721 | for (s = special; s->name; s++) { |
722 | int len = strlen(s->name); | |
723 | if (match_string(date, s->name) == len) { | |
724 | s->fn(tm, num); | |
725 | return end; | |
726 | } | |
3c07b1d1 LT |
727 | } |
728 | ||
729 | if (!*num) { | |
730 | for (i = 1; i < 11; i++) { | |
731 | int len = strlen(number_name[i]); | |
732 | if (match_string(date, number_name[i]) == len) { | |
733 | *num = i; | |
734 | return end; | |
735 | } | |
736 | } | |
737 | if (match_string(date, "last") == 4) | |
738 | *num = 1; | |
739 | return end; | |
740 | } | |
741 | ||
742 | tl = typelen; | |
743 | while (tl->type) { | |
744 | int len = strlen(tl->type); | |
745 | if (match_string(date, tl->type) >= len-1) { | |
746 | update_tm(tm, tl->length * *num); | |
747 | *num = 0; | |
748 | return end; | |
749 | } | |
750 | tl++; | |
751 | } | |
752 | ||
6b7b0427 LT |
753 | for (i = 0; i < 7; i++) { |
754 | int match = match_string(date, weekday_names[i]); | |
755 | if (match >= 3) { | |
756 | int diff, n = *num -1; | |
757 | *num = 0; | |
758 | ||
759 | diff = tm->tm_wday - i; | |
760 | if (diff <= 0) | |
761 | n++; | |
762 | diff += 7*n; | |
763 | ||
764 | update_tm(tm, diff * 24 * 60 * 60); | |
765 | return end; | |
766 | } | |
767 | } | |
768 | ||
3c07b1d1 LT |
769 | if (match_string(date, "months") >= 5) { |
770 | int n = tm->tm_mon - *num; | |
771 | *num = 0; | |
772 | while (n < 0) { | |
773 | n += 12; | |
774 | tm->tm_year--; | |
775 | } | |
776 | tm->tm_mon = n; | |
777 | return end; | |
778 | } | |
779 | ||
780 | if (match_string(date, "years") >= 4) { | |
781 | tm->tm_year -= *num; | |
782 | *num = 0; | |
783 | return end; | |
784 | } | |
785 | ||
786 | return end; | |
787 | } | |
788 | ||
e92a54d9 LT |
789 | static const char *approxidate_digit(const char *date, struct tm *tm, int *num) |
790 | { | |
791 | char *end; | |
792 | unsigned long number = strtoul(date, &end, 10); | |
793 | ||
393d340e LT |
794 | switch (*end) { |
795 | case ':': | |
796 | case '.': | |
797 | case '/': | |
798 | case '-': | |
799 | if (isdigit(end[1])) { | |
800 | int match = match_multi_number(number, *end, date, end, tm); | |
801 | if (match) | |
802 | return date + match; | |
803 | } | |
804 | } | |
805 | ||
e92a54d9 LT |
806 | *num = number; |
807 | return end; | |
808 | } | |
809 | ||
3c07b1d1 LT |
810 | unsigned long approxidate(const char *date) |
811 | { | |
812 | int number = 0; | |
813 | struct tm tm, now; | |
814 | struct timeval tv; | |
815 | char buffer[50]; | |
816 | ||
817 | if (parse_date(date, buffer, sizeof(buffer)) > 0) | |
818 | return strtoul(buffer, NULL, 10); | |
819 | ||
820 | gettimeofday(&tv, NULL); | |
821 | localtime_r(&tv.tv_sec, &tm); | |
822 | now = tm; | |
823 | for (;;) { | |
824 | unsigned char c = *date; | |
825 | if (!c) | |
826 | break; | |
827 | date++; | |
828 | if (isdigit(c)) { | |
e92a54d9 | 829 | date = approxidate_digit(date-1, &tm, &number); |
3c07b1d1 LT |
830 | continue; |
831 | } | |
832 | if (isalpha(c)) | |
833 | date = approxidate_alpha(date-1, &tm, &number); | |
834 | } | |
835 | if (number > 0 && number < 32) | |
836 | tm.tm_mday = number; | |
b73cebf4 | 837 | if (tm.tm_mon > now.tm_mon && tm.tm_year == now.tm_year) |
3c07b1d1 LT |
838 | tm.tm_year--; |
839 | return mktime(&tm); | |
840 | } |