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