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