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