]>
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 | ||
bb5799d6 JS |
9 | /* |
10 | * This is like mktime, but without normalization of tm_wday and tm_yday. | |
11 | */ | |
23418ea9 | 12 | static time_t tm_to_time_t(const struct tm *tm) |
ecee9d9e ET |
13 | { |
14 | static const int mdays[] = { | |
15 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 | |
16 | }; | |
17 | int year = tm->tm_year - 70; | |
18 | int month = tm->tm_mon; | |
19 | int day = tm->tm_mday; | |
20 | ||
21 | if (year < 0 || year > 129) /* algo only works for 1970-2099 */ | |
22 | return -1; | |
23 | if (month < 0 || month > 11) /* array bounds */ | |
24 | return -1; | |
25 | if (month < 2 || (year + 2) % 4) | |
26 | day--; | |
36e4986f LT |
27 | if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0) |
28 | return -1; | |
ecee9d9e ET |
29 | return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + |
30 | tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; | |
31 | } | |
32 | ||
33 | static const char *month_names[] = { | |
89967023 LT |
34 | "January", "February", "March", "April", "May", "June", |
35 | "July", "August", "September", "October", "November", "December" | |
ecee9d9e ET |
36 | }; |
37 | ||
38 | static const char *weekday_names[] = { | |
6b7b0427 | 39 | "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays" |
ecee9d9e ET |
40 | }; |
41 | ||
9a8e35e9 LT |
42 | static time_t gm_time_t(unsigned long time, int tz) |
43 | { | |
44 | int minutes; | |
45 | ||
46 | minutes = tz < 0 ? -tz : tz; | |
47 | minutes = (minutes / 100)*60 + (minutes % 100); | |
48 | minutes = tz < 0 ? -minutes : minutes; | |
49 | return time + minutes * 60; | |
50 | } | |
51 | ||
f80cd783 LT |
52 | /* |
53 | * The "tz" thing is passed in as this strange "decimal parse of tz" | |
54 | * thing, which means that tz -0100 is passed in as the integer -100, | |
55 | * even though it means "sixty minutes off" | |
56 | */ | |
2a387043 | 57 | static struct tm *time_to_tm(unsigned long time, int tz) |
f80cd783 | 58 | { |
9a8e35e9 | 59 | time_t t = gm_time_t(time, tz); |
2a387043 JH |
60 | return gmtime(&t); |
61 | } | |
62 | ||
a7b02ccf JH |
63 | /* |
64 | * What value of "tz" was in effect back then at "time" in the | |
65 | * local timezone? | |
66 | */ | |
67 | static int local_tzoffset(unsigned long time) | |
68 | { | |
69 | time_t t, t_local; | |
70 | struct tm tm; | |
71 | int offset, eastwest; | |
72 | ||
73 | t = time; | |
74 | localtime_r(&t, &tm); | |
bb5799d6 | 75 | t_local = tm_to_time_t(&tm); |
a7b02ccf JH |
76 | |
77 | if (t_local < t) { | |
78 | eastwest = -1; | |
79 | offset = t - t_local; | |
80 | } else { | |
81 | eastwest = 1; | |
82 | offset = t_local - t; | |
83 | } | |
84 | offset /= 60; /* in minutes */ | |
85 | offset = (offset % 60) + ((offset / 60) * 100); | |
86 | return offset * eastwest; | |
87 | } | |
88 | ||
33012fc4 AR |
89 | const char *show_date_relative(unsigned long time, int tz, |
90 | const struct timeval *now, | |
91 | char *timebuf, | |
92 | size_t timebuf_size) | |
93 | { | |
94 | unsigned long diff; | |
95 | if (now->tv_sec < time) | |
96 | return "in the future"; | |
97 | diff = now->tv_sec - time; | |
98 | if (diff < 90) { | |
99 | snprintf(timebuf, timebuf_size, "%lu seconds ago", diff); | |
100 | return timebuf; | |
101 | } | |
102 | /* Turn it into minutes */ | |
103 | diff = (diff + 30) / 60; | |
104 | if (diff < 90) { | |
105 | snprintf(timebuf, timebuf_size, "%lu minutes ago", diff); | |
106 | return timebuf; | |
107 | } | |
108 | /* Turn it into hours */ | |
109 | diff = (diff + 30) / 60; | |
110 | if (diff < 36) { | |
111 | snprintf(timebuf, timebuf_size, "%lu hours ago", diff); | |
112 | return timebuf; | |
113 | } | |
114 | /* We deal with number of days from here on */ | |
115 | diff = (diff + 12) / 24; | |
116 | if (diff < 14) { | |
117 | snprintf(timebuf, timebuf_size, "%lu days ago", diff); | |
118 | return timebuf; | |
119 | } | |
120 | /* Say weeks for the past 10 weeks or so */ | |
121 | if (diff < 70) { | |
122 | snprintf(timebuf, timebuf_size, "%lu weeks ago", (diff + 3) / 7); | |
123 | return timebuf; | |
124 | } | |
125 | /* Say months for the past 12 months or so */ | |
dbc1b1f7 | 126 | if (diff < 365) { |
33012fc4 AR |
127 | snprintf(timebuf, timebuf_size, "%lu months ago", (diff + 15) / 30); |
128 | return timebuf; | |
129 | } | |
130 | /* Give years and months for 5 years or so */ | |
131 | if (diff < 1825) { | |
f1e9c548 MG |
132 | unsigned long totalmonths = (diff * 12 * 2 + 365) / (365 * 2); |
133 | unsigned long years = totalmonths / 12; | |
134 | unsigned long months = totalmonths % 12; | |
33012fc4 AR |
135 | int n; |
136 | n = snprintf(timebuf, timebuf_size, "%lu year%s", | |
137 | years, (years > 1 ? "s" : "")); | |
138 | if (months) | |
139 | snprintf(timebuf + n, timebuf_size - n, | |
140 | ", %lu month%s ago", | |
141 | months, (months > 1 ? "s" : "")); | |
142 | else | |
143 | snprintf(timebuf + n, timebuf_size - n, " ago"); | |
144 | return timebuf; | |
145 | } | |
146 | /* Otherwise, just years. Centuries is probably overkill. */ | |
147 | snprintf(timebuf, timebuf_size, "%lu years ago", (diff + 183) / 365); | |
148 | return timebuf; | |
149 | } | |
150 | ||
f8493ec0 | 151 | const char *show_date(unsigned long time, int tz, enum date_mode mode) |
2a387043 JH |
152 | { |
153 | struct tm *tm; | |
154 | static char timebuf[200]; | |
155 | ||
7dff9b30 LT |
156 | if (mode == DATE_RAW) { |
157 | snprintf(timebuf, sizeof(timebuf), "%lu %+05d", time, tz); | |
158 | return timebuf; | |
159 | } | |
160 | ||
f8493ec0 | 161 | if (mode == DATE_RELATIVE) { |
9a8e35e9 LT |
162 | struct timeval now; |
163 | gettimeofday(&now, NULL); | |
33012fc4 AR |
164 | return show_date_relative(time, tz, &now, |
165 | timebuf, sizeof(timebuf)); | |
9a8e35e9 LT |
166 | } |
167 | ||
a7b02ccf JH |
168 | if (mode == DATE_LOCAL) |
169 | tz = local_tzoffset(time); | |
170 | ||
2a387043 | 171 | tm = time_to_tm(time, tz); |
f80cd783 LT |
172 | if (!tm) |
173 | return NULL; | |
f8493ec0 JS |
174 | if (mode == DATE_SHORT) |
175 | sprintf(timebuf, "%04d-%02d-%02d", tm->tm_year + 1900, | |
176 | tm->tm_mon + 1, tm->tm_mday); | |
ee8f838e RR |
177 | else if (mode == DATE_ISO8601) |
178 | sprintf(timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d", | |
179 | tm->tm_year + 1900, | |
180 | tm->tm_mon + 1, | |
181 | tm->tm_mday, | |
182 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
183 | tz); | |
73013afd JH |
184 | else if (mode == DATE_RFC2822) |
185 | sprintf(timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d", | |
186 | weekday_names[tm->tm_wday], tm->tm_mday, | |
187 | month_names[tm->tm_mon], tm->tm_year + 1900, | |
188 | tm->tm_hour, tm->tm_min, tm->tm_sec, tz); | |
f8493ec0 | 189 | else |
a7b02ccf | 190 | sprintf(timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d", |
f8493ec0 JS |
191 | weekday_names[tm->tm_wday], |
192 | month_names[tm->tm_mon], | |
193 | tm->tm_mday, | |
194 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
a7b02ccf JH |
195 | tm->tm_year + 1900, |
196 | (mode == DATE_LOCAL) ? 0 : ' ', | |
197 | tz); | |
f80cd783 LT |
198 | return timebuf; |
199 | } | |
200 | ||
89967023 LT |
201 | /* |
202 | * Check these. And note how it doesn't do the summer-time conversion. | |
203 | * | |
204 | * In my world, it's always summer, and things are probably a bit off | |
205 | * in other ways too. | |
206 | */ | |
207 | static const struct { | |
208 | const char *name; | |
209 | int offset; | |
5e2a78a4 | 210 | int dst; |
89967023 | 211 | } timezone_names[] = { |
5e2a78a4 LT |
212 | { "IDLW", -12, 0, }, /* International Date Line West */ |
213 | { "NT", -11, 0, }, /* Nome */ | |
214 | { "CAT", -10, 0, }, /* Central Alaska */ | |
215 | { "HST", -10, 0, }, /* Hawaii Standard */ | |
216 | { "HDT", -10, 1, }, /* Hawaii Daylight */ | |
217 | { "YST", -9, 0, }, /* Yukon Standard */ | |
218 | { "YDT", -9, 1, }, /* Yukon Daylight */ | |
219 | { "PST", -8, 0, }, /* Pacific Standard */ | |
220 | { "PDT", -8, 1, }, /* Pacific Daylight */ | |
221 | { "MST", -7, 0, }, /* Mountain Standard */ | |
222 | { "MDT", -7, 1, }, /* Mountain Daylight */ | |
223 | { "CST", -6, 0, }, /* Central Standard */ | |
224 | { "CDT", -6, 1, }, /* Central Daylight */ | |
225 | { "EST", -5, 0, }, /* Eastern Standard */ | |
226 | { "EDT", -5, 1, }, /* Eastern Daylight */ | |
227 | { "AST", -3, 0, }, /* Atlantic Standard */ | |
228 | { "ADT", -3, 1, }, /* Atlantic Daylight */ | |
229 | { "WAT", -1, 0, }, /* West Africa */ | |
230 | ||
231 | { "GMT", 0, 0, }, /* Greenwich Mean */ | |
232 | { "UTC", 0, 0, }, /* Universal (Coordinated) */ | |
75b37e70 | 233 | { "Z", 0, 0, }, /* Zulu, alias for UTC */ |
5e2a78a4 LT |
234 | |
235 | { "WET", 0, 0, }, /* Western European */ | |
236 | { "BST", 0, 1, }, /* British Summer */ | |
237 | { "CET", +1, 0, }, /* Central European */ | |
238 | { "MET", +1, 0, }, /* Middle European */ | |
239 | { "MEWT", +1, 0, }, /* Middle European Winter */ | |
240 | { "MEST", +1, 1, }, /* Middle European Summer */ | |
241 | { "CEST", +1, 1, }, /* Central European Summer */ | |
242 | { "MESZ", +1, 1, }, /* Middle European Summer */ | |
243 | { "FWT", +1, 0, }, /* French Winter */ | |
244 | { "FST", +1, 1, }, /* French Summer */ | |
245 | { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */ | |
92e2311b | 246 | { "EEST", +2, 1, }, /* Eastern European Daylight */ |
5e2a78a4 LT |
247 | { "WAST", +7, 0, }, /* West Australian Standard */ |
248 | { "WADT", +7, 1, }, /* West Australian Daylight */ | |
249 | { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */ | |
250 | { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */ | |
251 | { "EAST", +10, 0, }, /* Eastern Australian Standard */ | |
252 | { "EADT", +10, 1, }, /* Eastern Australian Daylight */ | |
253 | { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */ | |
695ed472 SD |
254 | { "NZT", +12, 0, }, /* New Zealand */ |
255 | { "NZST", +12, 0, }, /* New Zealand Standard */ | |
256 | { "NZDT", +12, 1, }, /* New Zealand Daylight */ | |
5e2a78a4 | 257 | { "IDLE", +12, 0, }, /* International Date Line East */ |
89967023 | 258 | }; |
ecee9d9e | 259 | |
89967023 | 260 | static int match_string(const char *date, const char *str) |
ecee9d9e | 261 | { |
89967023 LT |
262 | int i = 0; |
263 | ||
264 | for (i = 0; *date; date++, str++, i++) { | |
265 | if (*date == *str) | |
266 | continue; | |
267 | if (toupper(*date) == toupper(*str)) | |
268 | continue; | |
269 | if (!isalnum(*date)) | |
270 | break; | |
271 | return 0; | |
272 | } | |
273 | return i; | |
ecee9d9e ET |
274 | } |
275 | ||
68849b54 LT |
276 | static int skip_alpha(const char *date) |
277 | { | |
278 | int i = 0; | |
279 | do { | |
280 | i++; | |
281 | } while (isalpha(date[i])); | |
282 | return i; | |
283 | } | |
284 | ||
89967023 LT |
285 | /* |
286 | * Parse month, weekday, or timezone name | |
287 | */ | |
288 | static int match_alpha(const char *date, struct tm *tm, int *offset) | |
ecee9d9e | 289 | { |
89967023 | 290 | int i; |
ecee9d9e | 291 | |
89967023 LT |
292 | for (i = 0; i < 12; i++) { |
293 | int match = match_string(date, month_names[i]); | |
294 | if (match >= 3) { | |
295 | tm->tm_mon = i; | |
296 | return match; | |
ecee9d9e | 297 | } |
89967023 | 298 | } |
ecee9d9e | 299 | |
89967023 LT |
300 | for (i = 0; i < 7; i++) { |
301 | int match = match_string(date, weekday_names[i]); | |
302 | if (match >= 3) { | |
303 | tm->tm_wday = i; | |
304 | return match; | |
305 | } | |
306 | } | |
ecee9d9e | 307 | |
b4f2a6ac | 308 | for (i = 0; i < ARRAY_SIZE(timezone_names); i++) { |
89967023 | 309 | int match = match_string(date, timezone_names[i].name); |
75b37e70 | 310 | if (match >= 3 || match == strlen(timezone_names[i].name)) { |
5e2a78a4 LT |
311 | int off = timezone_names[i].offset; |
312 | ||
313 | /* This is bogus, but we like summer */ | |
314 | off += timezone_names[i].dst; | |
315 | ||
92e2311b LT |
316 | /* Only use the tz name offset if we don't have anything better */ |
317 | if (*offset == -1) | |
318 | *offset = 60*off; | |
319 | ||
89967023 | 320 | return match; |
ecee9d9e ET |
321 | } |
322 | } | |
ecee9d9e | 323 | |
68849b54 | 324 | if (match_string(date, "PM") == 2) { |
18b633ca LT |
325 | tm->tm_hour = (tm->tm_hour % 12) + 12; |
326 | return 2; | |
327 | } | |
328 | ||
329 | if (match_string(date, "AM") == 2) { | |
330 | tm->tm_hour = (tm->tm_hour % 12) + 0; | |
68849b54 LT |
331 | return 2; |
332 | } | |
333 | ||
89967023 | 334 | /* BAD CRAP */ |
68849b54 | 335 | return skip_alpha(date); |
89967023 | 336 | } |
ecee9d9e | 337 | |
38035cf4 | 338 | static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) |
89967023 | 339 | { |
198b0fb6 | 340 | if (month > 0 && month < 13 && day > 0 && day < 32) { |
38035cf4 JH |
341 | struct tm check = *tm; |
342 | struct tm *r = (now_tm ? &check : tm); | |
343 | time_t specified; | |
344 | ||
345 | r->tm_mon = month - 1; | |
346 | r->tm_mday = day; | |
198b0fb6 | 347 | if (year == -1) { |
38035cf4 JH |
348 | if (!now_tm) |
349 | return 1; | |
350 | r->tm_year = now_tm->tm_year; | |
89967023 | 351 | } |
38035cf4 JH |
352 | else if (year >= 1970 && year < 2100) |
353 | r->tm_year = year - 1900; | |
354 | else if (year > 70 && year < 100) | |
355 | r->tm_year = year; | |
356 | else if (year < 38) | |
357 | r->tm_year = year + 100; | |
358 | else | |
198b0fb6 | 359 | return 0; |
38035cf4 JH |
360 | if (!now_tm) |
361 | return 1; | |
362 | ||
bb5799d6 | 363 | specified = tm_to_time_t(r); |
198b0fb6 | 364 | |
38035cf4 JH |
365 | /* Be it commit time or author time, it does not make |
366 | * sense to specify timestamp way into the future. Make | |
367 | * sure it is not later than ten days from now... | |
368 | */ | |
369 | if (now + 10*24*3600 < specified) | |
370 | return 0; | |
371 | tm->tm_mon = r->tm_mon; | |
372 | tm->tm_mday = r->tm_mday; | |
373 | if (year != -1) | |
374 | tm->tm_year = r->tm_year; | |
198b0fb6 | 375 | return 1; |
89967023 | 376 | } |
198b0fb6 LT |
377 | return 0; |
378 | } | |
ecee9d9e | 379 | |
26a2d8ae | 380 | static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm) |
198b0fb6 | 381 | { |
38035cf4 JH |
382 | time_t now; |
383 | struct tm now_tm; | |
384 | struct tm *refuse_future; | |
198b0fb6 LT |
385 | long num2, num3; |
386 | ||
387 | num2 = strtol(end+1, &end, 10); | |
388 | num3 = -1; | |
389 | if (*end == c && isdigit(end[1])) | |
390 | num3 = strtol(end+1, &end, 10); | |
391 | ||
392 | /* Time? Date? */ | |
89967023 | 393 | switch (c) { |
198b0fb6 LT |
394 | case ':': |
395 | if (num3 < 0) | |
396 | num3 = 0; | |
397 | if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { | |
398 | tm->tm_hour = num; | |
399 | tm->tm_min = num2; | |
400 | tm->tm_sec = num3; | |
89967023 LT |
401 | break; |
402 | } | |
198b0fb6 | 403 | return 0; |
89967023 LT |
404 | |
405 | case '-': | |
406 | case '/': | |
38035cf4 JH |
407 | case '.': |
408 | now = time(NULL); | |
409 | refuse_future = NULL; | |
410 | if (gmtime_r(&now, &now_tm)) | |
411 | refuse_future = &now_tm; | |
412 | ||
198b0fb6 LT |
413 | if (num > 70) { |
414 | /* yyyy-mm-dd? */ | |
38035cf4 | 415 | if (is_date(num, num2, num3, refuse_future, now, tm)) |
198b0fb6 LT |
416 | break; |
417 | /* yyyy-dd-mm? */ | |
38035cf4 | 418 | if (is_date(num, num3, num2, refuse_future, now, tm)) |
89967023 | 419 | break; |
198b0fb6 | 420 | } |
38035cf4 JH |
421 | /* Our eastern European friends say dd.mm.yy[yy] |
422 | * is the norm there, so giving precedence to | |
423 | * mm/dd/yy[yy] form only when separator is not '.' | |
424 | */ | |
425 | if (c != '.' && | |
426 | is_date(num3, num, num2, refuse_future, now, tm)) | |
427 | break; | |
428 | /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ | |
429 | if (is_date(num3, num2, num, refuse_future, now, tm)) | |
89967023 | 430 | break; |
38035cf4 JH |
431 | /* Funny European mm.dd.yy */ |
432 | if (c == '.' && | |
433 | is_date(num3, num, num2, refuse_future, now, tm)) | |
198b0fb6 LT |
434 | break; |
435 | return 0; | |
436 | } | |
437 | return end - date; | |
438 | } | |
439 | ||
36e4986f LT |
440 | /* |
441 | * Have we filled in any part of the time/date yet? | |
442 | * We just do a binary 'and' to see if the sign bit | |
443 | * is set in all the values. | |
444 | */ | |
9f2b6d29 LT |
445 | static inline int nodate(struct tm *tm) |
446 | { | |
36e4986f LT |
447 | return (tm->tm_year & |
448 | tm->tm_mon & | |
449 | tm->tm_mday & | |
450 | tm->tm_hour & | |
451 | tm->tm_min & | |
452 | tm->tm_sec) < 0; | |
9f2b6d29 LT |
453 | } |
454 | ||
198b0fb6 | 455 | /* |
a6080a0a | 456 | * We've seen a digit. Time? Year? Date? |
198b0fb6 | 457 | */ |
26a2d8ae | 458 | static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt) |
198b0fb6 LT |
459 | { |
460 | int n; | |
461 | char *end; | |
462 | unsigned long num; | |
463 | ||
464 | num = strtoul(date, &end, 10); | |
465 | ||
466 | /* | |
a1a5a634 JS |
467 | * Seconds since 1970? We trigger on that for any numbers with |
468 | * more than 8 digits. This is because we don't want to rule out | |
469 | * numbers like 20070606 as a YYYYMMDD date. | |
198b0fb6 | 470 | */ |
9f2b6d29 | 471 | if (num >= 100000000 && nodate(tm)) { |
198b0fb6 | 472 | time_t time = num; |
9cb480f2 JH |
473 | if (gmtime_r(&time, tm)) { |
474 | *tm_gmt = 1; | |
198b0fb6 | 475 | return end - date; |
9cb480f2 | 476 | } |
198b0fb6 LT |
477 | } |
478 | ||
479 | /* | |
38035cf4 | 480 | * Check for special formats: num[-.:/]num[same]num |
198b0fb6 LT |
481 | */ |
482 | switch (*end) { | |
483 | case ':': | |
38035cf4 | 484 | case '.': |
198b0fb6 LT |
485 | case '/': |
486 | case '-': | |
487 | if (isdigit(end[1])) { | |
488 | int match = match_multi_number(num, *end, date, end, tm); | |
489 | if (match) | |
490 | return match; | |
89967023 LT |
491 | } |
492 | } | |
198b0fb6 LT |
493 | |
494 | /* | |
495 | * None of the special formats? Try to guess what | |
496 | * the number meant. We use the number of digits | |
497 | * to make a more educated guess.. | |
498 | */ | |
499 | n = 0; | |
500 | do { | |
501 | n++; | |
502 | } while (isdigit(date[n])); | |
503 | ||
504 | /* Four-digit year or a timezone? */ | |
505 | if (n == 4) { | |
7122f82f | 506 | if (num <= 1400 && *offset == -1) { |
198b0fb6 LT |
507 | unsigned int minutes = num % 100; |
508 | unsigned int hours = num / 100; | |
509 | *offset = hours*60 + minutes; | |
510 | } else if (num > 1900 && num < 2100) | |
511 | tm->tm_year = num - 1900; | |
512 | return n; | |
513 | } | |
514 | ||
9f2b6d29 LT |
515 | /* |
516 | * Ignore lots of numerals. We took care of 4-digit years above. | |
517 | * Days or months must be one or two digits. | |
518 | */ | |
519 | if (n > 2) | |
520 | return n; | |
521 | ||
198b0fb6 LT |
522 | /* |
523 | * NOTE! We will give precedence to day-of-month over month or | |
82f9d58a | 524 | * year numbers in the 1-12 range. So 05 is always "mday 5", |
198b0fb6 LT |
525 | * unless we already have a mday.. |
526 | * | |
527 | * IOW, 01 Apr 05 parses as "April 1st, 2005". | |
528 | */ | |
529 | if (num > 0 && num < 32 && tm->tm_mday < 0) { | |
530 | tm->tm_mday = num; | |
531 | return n; | |
532 | } | |
533 | ||
534 | /* Two-digit year? */ | |
535 | if (n == 2 && tm->tm_year < 0) { | |
536 | if (num < 10 && tm->tm_mday >= 0) { | |
537 | tm->tm_year = num + 100; | |
538 | return n; | |
539 | } | |
540 | if (num >= 70) { | |
541 | tm->tm_year = num; | |
542 | return n; | |
543 | } | |
544 | } | |
545 | ||
90290552 | 546 | if (num > 0 && num < 13 && tm->tm_mon < 0) |
198b0fb6 | 547 | tm->tm_mon = num-1; |
a6080a0a | 548 | |
198b0fb6 | 549 | return n; |
89967023 | 550 | } |
ecee9d9e | 551 | |
26a2d8ae | 552 | static int match_tz(const char *date, int *offp) |
89967023 LT |
553 | { |
554 | char *end; | |
ee646eb4 HL |
555 | int hour = strtoul(date + 1, &end, 10); |
556 | int n = end - (date + 1); | |
557 | int min = 0; | |
ecee9d9e | 558 | |
ee646eb4 HL |
559 | if (n == 4) { |
560 | /* hhmm */ | |
561 | min = hour % 100; | |
562 | hour = hour / 100; | |
563 | } else if (n != 2) { | |
564 | min = 99; /* random crap */ | |
565 | } else if (*end == ':') { | |
566 | /* hh:mm? */ | |
567 | min = strtoul(end + 1, &end, 10); | |
568 | if (end - (date + 1) != 5) | |
569 | min = 99; /* random crap */ | |
570 | } /* otherwise we parsed "hh" */ | |
ecee9d9e | 571 | |
198b0fb6 | 572 | /* |
ee646eb4 HL |
573 | * Don't accept any random crap. Even though some places have |
574 | * offset larger than 12 hours (e.g. Pacific/Kiritimati is at | |
575 | * UTC+14), there is something wrong if hour part is much | |
576 | * larger than that. We might also want to check that the | |
577 | * minutes are divisible by 15 or something too. (Offset of | |
578 | * Kathmandu, Nepal is UTC+5:45) | |
198b0fb6 | 579 | */ |
ee646eb4 HL |
580 | if (min < 60 && hour < 24) { |
581 | int offset = hour * 60 + min; | |
68849b54 LT |
582 | if (*date == '-') |
583 | offset = -offset; | |
68849b54 LT |
584 | *offp = offset; |
585 | } | |
89967023 LT |
586 | return end - date; |
587 | } | |
ecee9d9e | 588 | |
01c6ad29 LT |
589 | static int date_string(unsigned long date, int offset, char *buf, int len) |
590 | { | |
591 | int sign = '+'; | |
592 | ||
593 | if (offset < 0) { | |
594 | offset = -offset; | |
595 | sign = '-'; | |
596 | } | |
597 | return snprintf(buf, len, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60); | |
598 | } | |
599 | ||
116eb3ab JH |
600 | /* |
601 | * Parse a string like "0 +0000" as ancient timestamp near epoch, but | |
602 | * only when it appears not as part of any other string. | |
603 | */ | |
604 | static int match_object_header_date(const char *date, unsigned long *timestamp, int *offset) | |
605 | { | |
606 | char *end; | |
607 | unsigned long stamp; | |
608 | int ofs; | |
609 | ||
610 | if (*date < '0' || '9' <= *date) | |
611 | return -1; | |
612 | stamp = strtoul(date, &end, 10); | |
613 | if (*end != ' ' || stamp == ULONG_MAX || (end[1] != '+' && end[1] != '-')) | |
614 | return -1; | |
615 | date = end + 2; | |
616 | ofs = strtol(date, &end, 10); | |
617 | if ((*end != '\0' && (*end != '\n')) || end != date + 4) | |
618 | return -1; | |
619 | ofs = (ofs / 100) * 60 + (ofs % 100); | |
620 | if (date[-1] == '-') | |
621 | ofs = -ofs; | |
622 | *timestamp = stamp; | |
623 | *offset = ofs; | |
624 | return 0; | |
625 | } | |
626 | ||
89967023 LT |
627 | /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 |
628 | (i.e. English) day/month names, and it doesn't work correctly with %z. */ | |
9644c061 | 629 | int parse_date_basic(const char *date, unsigned long *timestamp, int *offset) |
89967023 LT |
630 | { |
631 | struct tm tm; | |
c5043cc1 RR |
632 | int tm_gmt; |
633 | unsigned long dummy_timestamp; | |
634 | int dummy_offset; | |
635 | ||
636 | if (!timestamp) | |
637 | timestamp = &dummy_timestamp; | |
638 | if (!offset) | |
639 | offset = &dummy_offset; | |
ecee9d9e | 640 | |
89967023 LT |
641 | memset(&tm, 0, sizeof(tm)); |
642 | tm.tm_year = -1; | |
643 | tm.tm_mon = -1; | |
644 | tm.tm_mday = -1; | |
eaa85129 | 645 | tm.tm_isdst = -1; |
36e4986f LT |
646 | tm.tm_hour = -1; |
647 | tm.tm_min = -1; | |
648 | tm.tm_sec = -1; | |
c5043cc1 | 649 | *offset = -1; |
9cb480f2 | 650 | tm_gmt = 0; |
89967023 | 651 | |
2c733fb2 JH |
652 | if (*date == '@' && |
653 | !match_object_header_date(date + 1, timestamp, offset)) | |
116eb3ab | 654 | return 0; /* success */ |
89967023 LT |
655 | for (;;) { |
656 | int match = 0; | |
657 | unsigned char c = *date; | |
658 | ||
659 | /* Stop at end of string or newline */ | |
660 | if (!c || c == '\n') | |
661 | break; | |
662 | ||
663 | if (isalpha(c)) | |
c5043cc1 | 664 | match = match_alpha(date, &tm, offset); |
89967023 | 665 | else if (isdigit(c)) |
c5043cc1 | 666 | match = match_digit(date, &tm, offset, &tm_gmt); |
89967023 | 667 | else if ((c == '-' || c == '+') && isdigit(date[1])) |
c5043cc1 | 668 | match = match_tz(date, offset); |
89967023 LT |
669 | |
670 | if (!match) { | |
671 | /* BAD CRAP */ | |
672 | match = 1; | |
a6080a0a | 673 | } |
89967023 LT |
674 | |
675 | date += match; | |
676 | } | |
ecee9d9e | 677 | |
eaa85129 | 678 | /* mktime uses local timezone */ |
c5043cc1 RR |
679 | *timestamp = tm_to_time_t(&tm); |
680 | if (*offset == -1) | |
9ba0f033 | 681 | *offset = ((time_t)*timestamp - mktime(&tm)) / 60; |
eaa85129 | 682 | |
c5043cc1 | 683 | if (*timestamp == -1) |
2a39064c | 684 | return -1; |
ecee9d9e | 685 | |
9cb480f2 | 686 | if (!tm_gmt) |
c5043cc1 | 687 | *timestamp -= *offset * 60; |
9644c061 | 688 | return 0; /* success */ |
c5043cc1 RR |
689 | } |
690 | ||
691 | int parse_date(const char *date, char *result, int maxlen) | |
692 | { | |
693 | unsigned long timestamp; | |
694 | int offset; | |
9644c061 | 695 | if (parse_date_basic(date, ×tamp, &offset)) |
c5043cc1 | 696 | return -1; |
9644c061 | 697 | return date_string(timestamp, offset, result, maxlen); |
ecee9d9e ET |
698 | } |
699 | ||
856665f8 AP |
700 | enum date_mode parse_date_format(const char *format) |
701 | { | |
702 | if (!strcmp(format, "relative")) | |
703 | return DATE_RELATIVE; | |
704 | else if (!strcmp(format, "iso8601") || | |
705 | !strcmp(format, "iso")) | |
706 | return DATE_ISO8601; | |
707 | else if (!strcmp(format, "rfc2822") || | |
708 | !strcmp(format, "rfc")) | |
709 | return DATE_RFC2822; | |
710 | else if (!strcmp(format, "short")) | |
711 | return DATE_SHORT; | |
712 | else if (!strcmp(format, "local")) | |
713 | return DATE_LOCAL; | |
714 | else if (!strcmp(format, "default")) | |
715 | return DATE_NORMAL; | |
7dff9b30 LT |
716 | else if (!strcmp(format, "raw")) |
717 | return DATE_RAW; | |
856665f8 AP |
718 | else |
719 | die("unknown date format %s", format); | |
720 | } | |
721 | ||
ecee9d9e ET |
722 | void datestamp(char *buf, int bufsize) |
723 | { | |
724 | time_t now; | |
725 | int offset; | |
726 | ||
727 | time(&now); | |
728 | ||
bb5799d6 | 729 | offset = tm_to_time_t(localtime(&now)) - now; |
ecee9d9e ET |
730 | offset /= 60; |
731 | ||
01c6ad29 | 732 | date_string(now, offset, buf, bufsize); |
ecee9d9e | 733 | } |
3c07b1d1 | 734 | |
90290552 LT |
735 | /* |
736 | * Relative time update (eg "2 days ago"). If we haven't set the time | |
737 | * yet, we need to set it from current time. | |
738 | */ | |
739 | static unsigned long update_tm(struct tm *tm, struct tm *now, unsigned long sec) | |
3c07b1d1 | 740 | { |
90290552 LT |
741 | time_t n; |
742 | ||
743 | if (tm->tm_mday < 0) | |
744 | tm->tm_mday = now->tm_mday; | |
745 | if (tm->tm_mon < 0) | |
746 | tm->tm_mon = now->tm_mon; | |
747 | if (tm->tm_year < 0) { | |
748 | tm->tm_year = now->tm_year; | |
749 | if (tm->tm_mon > now->tm_mon) | |
750 | tm->tm_year--; | |
751 | } | |
752 | ||
753 | n = mktime(tm) - sec; | |
3c07b1d1 | 754 | localtime_r(&n, tm); |
90290552 | 755 | return n; |
3c07b1d1 LT |
756 | } |
757 | ||
93cfa7c7 JH |
758 | static void date_now(struct tm *tm, struct tm *now, int *num) |
759 | { | |
760 | update_tm(tm, now, 0); | |
761 | } | |
762 | ||
90290552 | 763 | static void date_yesterday(struct tm *tm, struct tm *now, int *num) |
a8aca418 | 764 | { |
90290552 | 765 | update_tm(tm, now, 24*60*60); |
a8aca418 LT |
766 | } |
767 | ||
90290552 | 768 | static void date_time(struct tm *tm, struct tm *now, int hour) |
a8aca418 LT |
769 | { |
770 | if (tm->tm_hour < hour) | |
90290552 | 771 | date_yesterday(tm, now, NULL); |
a8aca418 LT |
772 | tm->tm_hour = hour; |
773 | tm->tm_min = 0; | |
774 | tm->tm_sec = 0; | |
775 | } | |
776 | ||
90290552 | 777 | static void date_midnight(struct tm *tm, struct tm *now, int *num) |
a8aca418 | 778 | { |
90290552 | 779 | date_time(tm, now, 0); |
a8aca418 LT |
780 | } |
781 | ||
90290552 | 782 | static void date_noon(struct tm *tm, struct tm *now, int *num) |
a8aca418 | 783 | { |
90290552 | 784 | date_time(tm, now, 12); |
a8aca418 LT |
785 | } |
786 | ||
90290552 | 787 | static void date_tea(struct tm *tm, struct tm *now, int *num) |
a8aca418 | 788 | { |
90290552 | 789 | date_time(tm, now, 17); |
a8aca418 LT |
790 | } |
791 | ||
90290552 | 792 | static void date_pm(struct tm *tm, struct tm *now, int *num) |
393d340e | 793 | { |
18b633ca | 794 | int hour, n = *num; |
393d340e LT |
795 | *num = 0; |
796 | ||
18b633ca LT |
797 | hour = tm->tm_hour; |
798 | if (n) { | |
799 | hour = n; | |
393d340e LT |
800 | tm->tm_min = 0; |
801 | tm->tm_sec = 0; | |
802 | } | |
18b633ca | 803 | tm->tm_hour = (hour % 12) + 12; |
393d340e LT |
804 | } |
805 | ||
90290552 | 806 | static void date_am(struct tm *tm, struct tm *now, int *num) |
393d340e | 807 | { |
18b633ca | 808 | int hour, n = *num; |
393d340e LT |
809 | *num = 0; |
810 | ||
18b633ca LT |
811 | hour = tm->tm_hour; |
812 | if (n) { | |
813 | hour = n; | |
393d340e LT |
814 | tm->tm_min = 0; |
815 | tm->tm_sec = 0; | |
816 | } | |
18b633ca | 817 | tm->tm_hour = (hour % 12); |
393d340e LT |
818 | } |
819 | ||
90290552 | 820 | static void date_never(struct tm *tm, struct tm *now, int *num) |
af66366a | 821 | { |
8c6b5786 OM |
822 | time_t n = 0; |
823 | localtime_r(&n, tm); | |
af66366a JS |
824 | } |
825 | ||
a8aca418 LT |
826 | static const struct special { |
827 | const char *name; | |
90290552 | 828 | void (*fn)(struct tm *, struct tm *, int *); |
a8aca418 LT |
829 | } special[] = { |
830 | { "yesterday", date_yesterday }, | |
831 | { "noon", date_noon }, | |
832 | { "midnight", date_midnight }, | |
833 | { "tea", date_tea }, | |
393d340e LT |
834 | { "PM", date_pm }, |
835 | { "AM", date_am }, | |
af66366a | 836 | { "never", date_never }, |
93cfa7c7 | 837 | { "now", date_now }, |
a8aca418 LT |
838 | { NULL } |
839 | }; | |
840 | ||
3c07b1d1 LT |
841 | static const char *number_name[] = { |
842 | "zero", "one", "two", "three", "four", | |
843 | "five", "six", "seven", "eight", "nine", "ten", | |
844 | }; | |
845 | ||
a8aca418 | 846 | static const struct typelen { |
3c07b1d1 LT |
847 | const char *type; |
848 | int length; | |
849 | } typelen[] = { | |
850 | { "seconds", 1 }, | |
851 | { "minutes", 60 }, | |
852 | { "hours", 60*60 }, | |
853 | { "days", 24*60*60 }, | |
854 | { "weeks", 7*24*60*60 }, | |
855 | { NULL } | |
a6080a0a | 856 | }; |
3c07b1d1 | 857 | |
93cfa7c7 | 858 | static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched) |
3c07b1d1 | 859 | { |
a8aca418 LT |
860 | const struct typelen *tl; |
861 | const struct special *s; | |
3c07b1d1 | 862 | const char *end = date; |
5df7dbba | 863 | int i; |
3c07b1d1 | 864 | |
5df7dbba PH |
865 | while (isalpha(*++end)); |
866 | ; | |
3c07b1d1 LT |
867 | |
868 | for (i = 0; i < 12; i++) { | |
869 | int match = match_string(date, month_names[i]); | |
870 | if (match >= 3) { | |
871 | tm->tm_mon = i; | |
93cfa7c7 | 872 | *touched = 1; |
3c07b1d1 LT |
873 | return end; |
874 | } | |
875 | } | |
876 | ||
a8aca418 LT |
877 | for (s = special; s->name; s++) { |
878 | int len = strlen(s->name); | |
879 | if (match_string(date, s->name) == len) { | |
90290552 | 880 | s->fn(tm, now, num); |
93cfa7c7 | 881 | *touched = 1; |
a8aca418 LT |
882 | return end; |
883 | } | |
3c07b1d1 LT |
884 | } |
885 | ||
886 | if (!*num) { | |
887 | for (i = 1; i < 11; i++) { | |
888 | int len = strlen(number_name[i]); | |
889 | if (match_string(date, number_name[i]) == len) { | |
890 | *num = i; | |
93cfa7c7 | 891 | *touched = 1; |
3c07b1d1 LT |
892 | return end; |
893 | } | |
894 | } | |
93cfa7c7 | 895 | if (match_string(date, "last") == 4) { |
3c07b1d1 | 896 | *num = 1; |
93cfa7c7 JH |
897 | *touched = 1; |
898 | } | |
3c07b1d1 LT |
899 | return end; |
900 | } | |
901 | ||
902 | tl = typelen; | |
903 | while (tl->type) { | |
904 | int len = strlen(tl->type); | |
905 | if (match_string(date, tl->type) >= len-1) { | |
90290552 | 906 | update_tm(tm, now, tl->length * *num); |
3c07b1d1 | 907 | *num = 0; |
93cfa7c7 | 908 | *touched = 1; |
3c07b1d1 LT |
909 | return end; |
910 | } | |
911 | tl++; | |
912 | } | |
913 | ||
6b7b0427 LT |
914 | for (i = 0; i < 7; i++) { |
915 | int match = match_string(date, weekday_names[i]); | |
916 | if (match >= 3) { | |
917 | int diff, n = *num -1; | |
918 | *num = 0; | |
919 | ||
920 | diff = tm->tm_wday - i; | |
921 | if (diff <= 0) | |
922 | n++; | |
923 | diff += 7*n; | |
924 | ||
90290552 | 925 | update_tm(tm, now, diff * 24 * 60 * 60); |
93cfa7c7 | 926 | *touched = 1; |
6b7b0427 LT |
927 | return end; |
928 | } | |
929 | } | |
930 | ||
3c07b1d1 | 931 | if (match_string(date, "months") >= 5) { |
931e8e27 JK |
932 | int n; |
933 | update_tm(tm, now, 0); /* fill in date fields if needed */ | |
934 | n = tm->tm_mon - *num; | |
3c07b1d1 LT |
935 | *num = 0; |
936 | while (n < 0) { | |
937 | n += 12; | |
938 | tm->tm_year--; | |
939 | } | |
940 | tm->tm_mon = n; | |
93cfa7c7 | 941 | *touched = 1; |
3c07b1d1 LT |
942 | return end; |
943 | } | |
944 | ||
945 | if (match_string(date, "years") >= 4) { | |
931e8e27 | 946 | update_tm(tm, now, 0); /* fill in date fields if needed */ |
3c07b1d1 LT |
947 | tm->tm_year -= *num; |
948 | *num = 0; | |
93cfa7c7 | 949 | *touched = 1; |
3c07b1d1 LT |
950 | return end; |
951 | } | |
952 | ||
953 | return end; | |
954 | } | |
955 | ||
e92a54d9 LT |
956 | static const char *approxidate_digit(const char *date, struct tm *tm, int *num) |
957 | { | |
958 | char *end; | |
959 | unsigned long number = strtoul(date, &end, 10); | |
960 | ||
393d340e LT |
961 | switch (*end) { |
962 | case ':': | |
963 | case '.': | |
964 | case '/': | |
965 | case '-': | |
966 | if (isdigit(end[1])) { | |
967 | int match = match_multi_number(number, *end, date, end, tm); | |
968 | if (match) | |
969 | return date + match; | |
970 | } | |
971 | } | |
972 | ||
9f2b6d29 LT |
973 | /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */ |
974 | if (date[0] != '0' || end - date <= 2) | |
975 | *num = number; | |
e92a54d9 LT |
976 | return end; |
977 | } | |
978 | ||
90290552 LT |
979 | /* |
980 | * Do we have a pending number at the end, or when | |
981 | * we see a new one? Let's assume it's a month day, | |
982 | * as in "Dec 6, 1992" | |
983 | */ | |
984 | static void pending_number(struct tm *tm, int *num) | |
985 | { | |
986 | int number = *num; | |
987 | ||
988 | if (number) { | |
989 | *num = 0; | |
990 | if (tm->tm_mday < 0 && number < 32) | |
991 | tm->tm_mday = number; | |
36e4986f LT |
992 | else if (tm->tm_mon < 0 && number < 13) |
993 | tm->tm_mon = number-1; | |
994 | else if (tm->tm_year < 0) { | |
995 | if (number > 1969 && number < 2100) | |
996 | tm->tm_year = number - 1900; | |
997 | else if (number > 69 && number < 100) | |
998 | tm->tm_year = number; | |
999 | else if (number < 38) | |
1000 | tm->tm_year = 100 + number; | |
1001 | /* We screw up for number = 00 ? */ | |
1002 | } | |
90290552 LT |
1003 | } |
1004 | } | |
1005 | ||
93cfa7c7 JH |
1006 | static unsigned long approxidate_str(const char *date, |
1007 | const struct timeval *tv, | |
1008 | int *error_ret) | |
3c07b1d1 LT |
1009 | { |
1010 | int number = 0; | |
93cfa7c7 | 1011 | int touched = 0; |
3c07b1d1 | 1012 | struct tm tm, now; |
f697b33b | 1013 | time_t time_sec; |
3c07b1d1 | 1014 | |
33012fc4 | 1015 | time_sec = tv->tv_sec; |
f697b33b | 1016 | localtime_r(&time_sec, &tm); |
3c07b1d1 | 1017 | now = tm; |
90290552 LT |
1018 | |
1019 | tm.tm_year = -1; | |
1020 | tm.tm_mon = -1; | |
1021 | tm.tm_mday = -1; | |
1022 | ||
3c07b1d1 LT |
1023 | for (;;) { |
1024 | unsigned char c = *date; | |
1025 | if (!c) | |
1026 | break; | |
1027 | date++; | |
1028 | if (isdigit(c)) { | |
90290552 | 1029 | pending_number(&tm, &number); |
e92a54d9 | 1030 | date = approxidate_digit(date-1, &tm, &number); |
93cfa7c7 | 1031 | touched = 1; |
3c07b1d1 LT |
1032 | continue; |
1033 | } | |
1034 | if (isalpha(c)) | |
93cfa7c7 | 1035 | date = approxidate_alpha(date-1, &tm, &now, &number, &touched); |
3c07b1d1 | 1036 | } |
90290552 | 1037 | pending_number(&tm, &number); |
93cfa7c7 JH |
1038 | if (!touched) |
1039 | *error_ret = 1; | |
90290552 | 1040 | return update_tm(&tm, &now, 0); |
3c07b1d1 | 1041 | } |
33012fc4 AR |
1042 | |
1043 | unsigned long approxidate_relative(const char *date, const struct timeval *tv) | |
1044 | { | |
c5043cc1 RR |
1045 | unsigned long timestamp; |
1046 | int offset; | |
93cfa7c7 | 1047 | int errors = 0; |
33012fc4 | 1048 | |
9644c061 | 1049 | if (!parse_date_basic(date, ×tamp, &offset)) |
c5043cc1 | 1050 | return timestamp; |
93cfa7c7 | 1051 | return approxidate_str(date, tv, &errors); |
33012fc4 AR |
1052 | } |
1053 | ||
93cfa7c7 | 1054 | unsigned long approxidate_careful(const char *date, int *error_ret) |
33012fc4 AR |
1055 | { |
1056 | struct timeval tv; | |
c5043cc1 RR |
1057 | unsigned long timestamp; |
1058 | int offset; | |
93cfa7c7 JH |
1059 | int dummy = 0; |
1060 | if (!error_ret) | |
1061 | error_ret = &dummy; | |
33012fc4 | 1062 | |
9644c061 | 1063 | if (!parse_date_basic(date, ×tamp, &offset)) { |
93cfa7c7 | 1064 | *error_ret = 0; |
c5043cc1 | 1065 | return timestamp; |
93cfa7c7 | 1066 | } |
33012fc4 AR |
1067 | |
1068 | gettimeofday(&tv, NULL); | |
93cfa7c7 | 1069 | return approxidate_str(date, &tv, error_ret); |
33012fc4 | 1070 | } |