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