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