source src/date.c
Line | Flow | Count | Block(s) | Source |
---|---|---|---|---|
1 | - | /* | ||
2 | - | * GIT - The information manager from hell | ||
3 | - | * | ||
4 | - | * Copyright (C) Linus Torvalds, 2005 | ||
5 | - | */ | ||
6 | - | |||
7 | - | #include "common.h" | ||
8 | - | |||
9 | - | #ifndef GIT_WIN32 | ||
10 | - | #include <sys/time.h> | ||
11 | - | #endif | ||
12 | - | |||
13 | - | #include "util.h" | ||
14 | - | #include "cache.h" | ||
15 | - | #include "posix.h" | ||
16 | - | |||
17 | - | #include <ctype.h> | ||
18 | - | #include <time.h> | ||
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 | - | */ | ||
33 | 27 | 2 | static git_time_t tm_to_time_t(const struct tm *tm) | |
34 | - | { | ||
35 | - | static const int mdays[] = { | ||
36 | - | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 | ||
37 | - | }; | ||
38 | 27 | 2 | int year = tm->tm_year - 70; | |
39 | 27 | 2 | int month = tm->tm_mon; | |
40 | 27 | 2 | int day = tm->tm_mday; | |
41 | - | |||
42 | 27 | 2,3 | if (year < 0 || year > 129) /* algo only works for 1970-2099 */ | |
43 | 4 | 4 | return -1; | |
44 | 23 | 5,6 | if (month < 0 || month > 11) /* array bounds */ | |
45 | ##### | 7 | return -1; | |
46 | 23 | 8,9 | if (month < 2 || (year + 2) % 4) | |
47 | 10 | 10 | day--; | |
48 | 23 | 11-13 | if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0) | |
49 | 11 | 14 | return -1; | |
50 | 12 | 15 | return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + | |
51 | 12 | 15 | 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 */ | ||
109 | - | { "EET", +2, 0, }, /* Eastern Europe */ | ||
110 | - | { "EEST", +2, 1, }, /* Eastern European Daylight */ | ||
111 | - | { "WAST", +7, 0, }, /* West Australian Standard */ | ||
112 | - | { "WADT", +7, 1, }, /* West Australian Daylight */ | ||
113 | - | { "CCT", +8, 0, }, /* China Coast */ | ||
114 | - | { "JST", +9, 0, }, /* Japan Standard */ | ||
115 | - | { "EAST", +10, 0, }, /* Eastern Australian Standard */ | ||
116 | - | { "EADT", +10, 1, }, /* Eastern Australian Daylight */ | ||
117 | - | { "GST", +10, 0, }, /* Guam Standard */ | ||
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 | - | |||
124 | 648 | 2 | static size_t match_string(const char *date, const char *str) | |
125 | - | { | ||
126 | 648 | 2 | size_t i = 0; | |
127 | - | |||
128 | 711 | 2,11,12 | for (i = 0; *date; date++, str++, i++) { | |
129 | 710 | 3 | if (*date == *str) | |
130 | 27 | 4 | continue; | |
131 | 683 | 5 | if (toupper(*date) == toupper(*str)) | |
132 | 36 | 6 | continue; | |
133 | 647 | 7,8 | if (!isalnum(*date)) | |
134 | 3 | 9 | break; | |
135 | 644 | 10 | return 0; | |
136 | - | } | ||
137 | 4 | 13 | return i; | |
138 | - | } | ||
139 | - | |||
140 | 7 | 2 | static int skip_alpha(const char *date) | |
141 | - | { | ||
142 | 7 | 2 | int i = 0; | |
143 | - | do { | ||
144 | 30 | 3 | i++; | |
145 | 30 | 3,4 | } while (isalpha(date[i])); | |
146 | 7 | 5 | return i; | |
147 | - | } | ||
148 | - | |||
149 | - | /* | ||
150 | - | * Parse month, weekday, or timezone name | ||
151 | - | */ | ||
152 | 7 | 2 | static size_t match_alpha(const char *date, struct tm *tm, int *offset) | |
153 | - | { | ||
154 | - | unsigned int i; | ||
155 | - | |||
156 | 91 | 2,6,7 | for (i = 0; i < 12; i++) { | |
157 | 84 | 3 | size_t match = match_string(date, month_names[i]); | |
158 | 84 | 4 | if (match >= 3) { | |
159 | ##### | 5 | tm->tm_mon = i; | |
160 | ##### | 5 | return match; | |
161 | - | } | ||
162 | - | } | ||
163 | - | |||
164 | 56 | 8,12,13 | for (i = 0; i < 7; i++) { | |
165 | 49 | 9 | size_t match = match_string(date, weekday_names[i]); | |
166 | 49 | 10 | if (match >= 3) { | |
167 | ##### | 11 | tm->tm_wday = i; | |
168 | ##### | 11 | return match; | |
169 | - | } | ||
170 | - | } | ||
171 | - | |||
172 | 315 | 14,21,22 | for (i = 0; i < ARRAY_SIZE(timezone_names); i++) { | |
173 | 308 | 15 | size_t match = match_string(date, timezone_names[i].name); | |
174 | 308 | 16,17 | if (match >= 3 || match == strlen(timezone_names[i].name)) { | |
175 | ##### | 18 | int off = timezone_names[i].offset; | |
176 | - | |||
177 | - | /* This is bogus, but we like summer */ | ||
178 | ##### | 18 | off += timezone_names[i].dst; | |
179 | - | |||
180 | - | /* Only use the tz name offset if we don't have anything better */ | ||
181 | ##### | 18 | if (*offset == -1) | |
182 | ##### | 19 | *offset = 60*off; | |
183 | - | |||
184 | ##### | 20 | return match; | |
185 | - | } | ||
186 | - | } | ||
187 | - | |||
188 | 7 | 23,24 | if (match_string(date, "PM") == 2) { | |
189 | ##### | 25 | tm->tm_hour = (tm->tm_hour % 12) + 12; | |
190 | ##### | 25 | return 2; | |
191 | - | } | ||
192 | - | |||
193 | 7 | 26,27 | if (match_string(date, "AM") == 2) { | |
194 | ##### | 28 | tm->tm_hour = (tm->tm_hour % 12) + 0; | |
195 | ##### | 28 | return 2; | |
196 | - | } | ||
197 | - | |||
198 | - | /* BAD */ | ||
199 | 7 | 29 | return skip_alpha(date); | |
200 | - | } | ||
201 | - | |||
202 | 19 | 2 | static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) | |
203 | - | { | ||
204 | 19 | 2-5 | if (month > 0 && month < 13 && day > 0 && day < 32) { | |
205 | 15 | 6 | struct tm check = *tm; | |
206 | 15 | 6-8 | struct tm *r = (now_tm ? &check : tm); | |
207 | - | time_t specified; | ||
208 | - | |||
209 | 15 | 9 | r->tm_mon = month - 1; | |
210 | 15 | 9 | r->tm_mday = day; | |
211 | 15 | 9 | if (year == -1) { | |
212 | 2 | 10 | if (!now_tm) | |
213 | ##### | 11 | return 1; | |
214 | 2 | 12 | r->tm_year = now_tm->tm_year; | |
215 | - | } | ||
216 | 13 | 13,14 | else if (year >= 1970 && year < 2100) | |
217 | 13 | 15 | r->tm_year = year - 1900; | |
218 | ##### | 16,17 | else if (year > 70 && year < 100) | |
219 | ##### | 18 | r->tm_year = year; | |
220 | ##### | 19 | else if (year < 38) | |
221 | ##### | 20 | r->tm_year = year + 100; | |
222 | - | else | ||
223 | ##### | 21 | return 0; | |
224 | 15 | 22 | if (!now_tm) | |
225 | ##### | 23 | return 1; | |
226 | - | |||
227 | 15 | 24 | 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 | 15 | 25 | if (now + 10*24*3600 < specified) | |
234 | 4 | 26 | return 0; | |
235 | 11 | 27 | tm->tm_mon = r->tm_mon; | |
236 | 11 | 27 | tm->tm_mday = r->tm_mday; | |
237 | 11 | 27 | if (year != -1) | |
238 | 9 | 28 | tm->tm_year = r->tm_year; | |
239 | 15 | 29,30 | return 1; | |
240 | - | } | ||
241 | 4 | 31 | return 0; | |
242 | - | } | ||
243 | - | |||
244 | 18 | 2 | static size_t match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm) | |
245 | - | { | ||
246 | - | time_t now; | ||
247 | - | struct tm now_tm; | ||
248 | - | struct tm *refuse_future; | ||
249 | - | long num2, num3; | ||
250 | - | |||
251 | 18 | 2 | num2 = strtol(end+1, &end, 10); | |
252 | 18 | 3 | num3 = -1; | |
253 | 18 | 3-5 | if (*end == c && isdigit(end[1])) | |
254 | 16 | 6 | num3 = strtol(end+1, &end, 10); | |
255 | - | |||
256 | - | /* Time? Date? */ | ||
257 | 18 | 7 | switch (c) { | |
258 | - | case ':': | ||
259 | 5 | 8 | if (num3 < 0) | |
260 | ##### | 9 | num3 = 0; | |
261 | 5 | 10-14 | if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { | |
262 | 5 | 15 | tm->tm_hour = num; | |
263 | 5 | 15 | tm->tm_min = num2; | |
264 | 5 | 15 | tm->tm_sec = num3; | |
265 | 5 | 15 | break; | |
266 | - | } | ||
267 | ##### | 16 | return 0; | |
268 | - | |||
269 | - | case '-': | ||
270 | - | case '/': | ||
271 | - | case '.': | ||
272 | 13 | 17 | now = time(NULL); | |
273 | 13 | 18 | refuse_future = NULL; | |
274 | 13 | 18,19 | if (p_gmtime_r(&now, &now_tm)) | |
275 | 13 | 20 | refuse_future = &now_tm; | |
276 | - | |||
277 | 13 | 21 | if (num > 70) { | |
278 | - | /* yyyy-mm-dd? */ | ||
279 | 11 | 22,23 | if (is_date(num, num2, num3, refuse_future, now, tm)) | |
280 | 9 | 24 | break; | |
281 | - | /* yyyy-dd-mm? */ | ||
282 | 2 | 25,26 | if (is_date(num, num3, num2, refuse_future, now, tm)) | |
283 | ##### | 27 | 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 | 4 | 28,30 | if (c != '.' && | |
290 | 4 | 29 | is_date(num3, num, num2, refuse_future, now, tm)) | |
291 | 2 | 31 | break; | |
292 | - | /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ | ||
293 | 2 | 32,33 | if (is_date(num3, num2, num, refuse_future, now, tm)) | |
294 | ##### | 34 | break; | |
295 | - | /* Funny European mm.dd.yy */ | ||
296 | 2 | 35,37 | if (c == '.' && | |
297 | ##### | 36 | is_date(num3, num, num2, refuse_future, now, tm)) | |
298 | ##### | 38 | break; | |
299 | 2 | 39 | return 0; | |
300 | - | } | ||
301 | 16 | 40 | 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 | - | */ | ||
309 | ##### | 2 | static int nodate(struct tm *tm) | |
310 | - | { | ||
311 | ##### | 2 | return (tm->tm_year & | |
312 | ##### | 2 | tm->tm_mon & | |
313 | ##### | 2 | tm->tm_mday & | |
314 | ##### | 2 | tm->tm_hour & | |
315 | ##### | 2 | tm->tm_min & | |
316 | ##### | 2 | tm->tm_sec) < 0; | |
317 | - | } | ||
318 | - | |||
319 | - | /* | ||
320 | - | * We've seen a digit. Time? Year? Date? | ||
321 | - | */ | ||
322 | 17 | 2 | static size_t match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt) | |
323 | - | { | ||
324 | - | size_t n; | ||
325 | - | char *end; | ||
326 | - | unsigned long num; | ||
327 | - | |||
328 | 17 | 2 | 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 | 17 | 3-5 | if (num >= 100000000 && nodate(tm)) { | |
336 | ##### | 6 | time_t time = num; | |
337 | ##### | 6,7 | if (p_gmtime_r(&time, tm)) { | |
338 | ##### | 8 | *tm_gmt = 1; | |
339 | ##### | 8,9 | return end - date; | |
340 | - | } | ||
341 | - | } | ||
342 | - | |||
343 | - | /* | ||
344 | - | * Check for special formats: num[-.:/]num[same]num | ||
345 | - | */ | ||
346 | 17 | 10 | switch (*end) { | |
347 | - | case ':': | ||
348 | - | case '.': | ||
349 | - | case '/': | ||
350 | - | case '-': | ||
351 | 13 | 11,12 | if (isdigit(end[1])) { | |
352 | 13 | 13 | size_t match = match_multi_number(num, *end, date, end, tm); | |
353 | 13 | 14 | if (match) | |
354 | 13 | 15 | 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 | 4 | 16 | n = 0; | |
364 | - | do { | ||
365 | 5 | 17 | n++; | |
366 | 5 | 17,18 | } while (isdigit(date[n])); | |
367 | - | |||
368 | - | /* Four-digit year or a timezone? */ | ||
369 | 4 | 19 | if (n == 4) { | |
370 | ##### | 20-22 | if (num <= 1400 && *offset == -1) { | |
371 | ##### | 22 | unsigned int minutes = num % 100; | |
372 | ##### | 22 | unsigned int hours = num / 100; | |
373 | ##### | 22 | *offset = hours*60 + minutes; | |
374 | ##### | 23,24 | } else if (num > 1900 && num < 2100) | |
375 | ##### | 25 | tm->tm_year = num - 1900; | |
376 | ##### | 26 | 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 | 4 | 27 | if (n > 2) | |
384 | ##### | 28 | 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 | 4 | 29-31 | if (num > 0 && num < 32 && tm->tm_mday < 0) { | |
394 | 4 | 32 | tm->tm_mday = num; | |
395 | 4 | 32 | return n; | |
396 | - | } | ||
397 | - | |||
398 | - | /* Two-digit year? */ | ||
399 | ##### | 33,34 | if (n == 2 && tm->tm_year < 0) { | |
400 | ##### | 35,36 | if (num < 10 && tm->tm_mday >= 0) { | |
401 | ##### | 37 | tm->tm_year = num + 100; | |
402 | ##### | 37 | return n; | |
403 | - | } | ||
404 | ##### | 38 | if (num >= 70) { | |
405 | ##### | 39 | tm->tm_year = num; | |
406 | ##### | 39 | return n; | |
407 | - | } | ||
408 | - | } | ||
409 | - | |||
410 | ##### | 40-42 | if (num > 0 && num < 13 && tm->tm_mon < 0) | |
411 | ##### | 43 | tm->tm_mon = num-1; | |
412 | - | |||
413 | ##### | 44 | return n; | |
414 | - | } | ||
415 | - | |||
416 | 5 | 2 | static size_t match_tz(const char *date, int *offp) | |
417 | - | { | ||
418 | - | char *end; | ||
419 | 5 | 2 | int hour = strtoul(date + 1, &end, 10); | |
420 | 5 | 3 | size_t n = end - (date + 1); | |
421 | 5 | 3 | int min = 0; | |
422 | - | |||
423 | 5 | 3 | if (n == 4) { | |
424 | - | /* hhmm */ | ||
425 | 5 | 4 | min = hour % 100; | |
426 | 5 | 4 | hour = hour / 100; | |
427 | ##### | 5 | } else if (n != 2) { | |
428 | ##### | 6 | min = 99; /* random stuff */ | |
429 | ##### | 7 | } else if (*end == ':') { | |
430 | - | /* hh:mm? */ | ||
431 | ##### | 8 | min = strtoul(end + 1, &end, 10); | |
432 | ##### | 9 | if (end - (date + 1) != 5) | |
433 | ##### | 10 | min = 99; /* random stuff */ | |
434 | - | } /* otherwise we parsed "hh" */ | ||
435 | - | |||
436 | - | /* | ||
437 | - | * Don't accept any random stuff. Even though some places have | ||
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 | 5 | 11,12 | if (min < 60 && hour < 24) { | |
445 | 5 | 13 | int offset = hour * 60 + min; | |
446 | 5 | 13 | if (*date == '-') | |
447 | 3 | 14 | offset = -offset; | |
448 | 5 | 15 | *offp = offset; | |
449 | - | } | ||
450 | 5 | 16 | 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 | - | */ | ||
457 | ##### | 2 | static int match_object_header_date(const char *date, git_time_t *timestamp, int *offset) | |
458 | - | { | ||
459 | - | char *end; | ||
460 | - | unsigned long stamp; | ||
461 | - | int ofs; | ||
462 | - | |||
463 | ##### | 2,3 | if (*date < '0' || '9' <= *date) | |
464 | ##### | 4 | return -1; | |
465 | ##### | 5 | stamp = strtoul(date, &end, 10); | |
466 | ##### | 6-9 | if (*end != ' ' || stamp == ULONG_MAX || (end[1] != '+' && end[1] != '-')) | |
467 | ##### | 10 | return -1; | |
468 | ##### | 11 | date = end + 2; | |
469 | ##### | 11 | ofs = strtol(date, &end, 10); | |
470 | ##### | 12-14 | if ((*end != '\0' && (*end != '\n')) || end != date + 4) | |
471 | ##### | 15 | return -1; | |
472 | ##### | 16 | ofs = (ofs / 100) * 60 + (ofs % 100); | |
473 | ##### | 16 | if (date[-1] == '-') | |
474 | ##### | 17 | ofs = -ofs; | |
475 | ##### | 18 | *timestamp = stamp; | |
476 | ##### | 18 | *offset = ofs; | |
477 | ##### | 18 | 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. */ | ||
482 | 12 | 2 | static int parse_date_basic(const char *date, git_time_t *timestamp, int *offset) | |
483 | - | { | ||
484 | - | struct tm tm; | ||
485 | - | int tm_gmt; | ||
486 | - | git_time_t dummy_timestamp; | ||
487 | - | int dummy_offset; | ||
488 | - | |||
489 | 12 | 2 | if (!timestamp) | |
490 | ##### | 3 | timestamp = &dummy_timestamp; | |
491 | 12 | 4 | if (!offset) | |
492 | ##### | 5 | offset = &dummy_offset; | |
493 | - | |||
494 | 12 | 6 | memset(&tm, 0, sizeof(tm)); | |
495 | 12 | 6 | tm.tm_year = -1; | |
496 | 12 | 6 | tm.tm_mon = -1; | |
497 | 12 | 6 | tm.tm_mday = -1; | |
498 | 12 | 6 | tm.tm_isdst = -1; | |
499 | 12 | 6 | tm.tm_hour = -1; | |
500 | 12 | 6 | tm.tm_min = -1; | |
501 | 12 | 6 | tm.tm_sec = -1; | |
502 | 12 | 6 | *offset = -1; | |
503 | 12 | 6 | tm_gmt = 0; | |
504 | - | |||
505 | 12 | 6,8 | if (*date == '@' && | |
506 | ##### | 7 | !match_object_header_date(date + 1, timestamp, offset)) | |
507 | ##### | 9 | return 0; /* success */ | |
508 | - | for (;;) { | ||
509 | 58 | 10 | size_t match = 0; | |
510 | 58 | 10 | unsigned char c = *date; | |
511 | - | |||
512 | - | /* Stop at end of string or newline */ | ||
513 | 58 | 10,11 | if (!c || c == '\n') | |
514 | - | break; | ||
515 | - | |||
516 | 46 | 12,13 | if (isalpha(c)) | |
517 | 7 | 14 | match = match_alpha(date, &tm, offset); | |
518 | 39 | 15,16 | else if (isdigit(c)) | |
519 | 17 | 17 | match = match_digit(date, &tm, offset, &tm_gmt); | |
520 | 22 | 18-21 | else if ((c == '-' || c == '+') && isdigit(date[1])) | |
521 | 5 | 22 | match = match_tz(date, offset); | |
522 | - | |||
523 | 46 | 23 | if (!match) { | |
524 | - | /* BAD */ | ||
525 | 17 | 24 | match = 1; | |
526 | - | } | ||
527 | - | |||
528 | 46 | 25 | date += match; | |
529 | 46 | 25 | } | |
530 | - | |||
531 | - | /* mktime uses local timezone */ | ||
532 | 12 | 26 | *timestamp = tm_to_time_t(&tm); | |
533 | 12 | 27 | if (*offset == -1) | |
534 | 7 | 28,29 | *offset = (int)((time_t)*timestamp - mktime(&tm)) / 60; | |
535 | - | |||
536 | 12 | 30 | if (*timestamp == (git_time_t)-1) | |
537 | 7 | 31 | return -1; | |
538 | - | |||
539 | 5 | 32 | if (!tm_gmt) | |
540 | 5 | 33 | *timestamp -= *offset * 60; | |
541 | 5 | 34 | 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 | - | */ | ||
549 | 11 | 2 | static git_time_t update_tm(struct tm *tm, struct tm *now, unsigned long sec) | |
550 | - | { | ||
551 | - | time_t n; | ||
552 | - | |||
553 | 11 | 2 | if (tm->tm_mday < 0) | |
554 | 4 | 3 | tm->tm_mday = now->tm_mday; | |
555 | 11 | 4 | if (tm->tm_mon < 0) | |
556 | 4 | 5 | tm->tm_mon = now->tm_mon; | |
557 | 11 | 6 | if (tm->tm_year < 0) { | |
558 | 4 | 7 | tm->tm_year = now->tm_year; | |
559 | 4 | 7 | if (tm->tm_mon > now->tm_mon) | |
560 | ##### | 8 | tm->tm_year--; | |
561 | - | } | ||
562 | - | |||
563 | 11 | 9 | n = mktime(tm) - sec; | |
564 | 11 | 10 | p_localtime_r(&n, tm); | |
565 | 11 | 11 | return n; | |
566 | - | } | ||
567 | - | |||
568 | ##### | 2 | static void date_now(struct tm *tm, struct tm *now, int *num) | |
569 | - | { | ||
570 | - | GIT_UNUSED(num); | ||
571 | ##### | 2 | update_tm(tm, now, 0); | |
572 | ##### | 3 | } | |
573 | - | |||
574 | ##### | 2 | static void date_yesterday(struct tm *tm, struct tm *now, int *num) | |
575 | - | { | ||
576 | - | GIT_UNUSED(num); | ||
577 | ##### | 2 | update_tm(tm, now, 24*60*60); | |
578 | ##### | 3 | } | |
579 | - | |||
580 | ##### | 2 | static void date_time(struct tm *tm, struct tm *now, int hour) | |
581 | - | { | ||
582 | ##### | 2 | if (tm->tm_hour < hour) | |
583 | ##### | 3 | date_yesterday(tm, now, NULL); | |
584 | ##### | 4 | tm->tm_hour = hour; | |
585 | ##### | 4 | tm->tm_min = 0; | |
586 | ##### | 4 | tm->tm_sec = 0; | |
587 | ##### | 4 | } | |
588 | - | |||
589 | ##### | 2 | static void date_midnight(struct tm *tm, struct tm *now, int *num) | |
590 | - | { | ||
591 | - | GIT_UNUSED(num); | ||
592 | ##### | 2 | date_time(tm, now, 0); | |
593 | ##### | 3 | } | |
594 | - | |||
595 | ##### | 2 | static void date_noon(struct tm *tm, struct tm *now, int *num) | |
596 | - | { | ||
597 | - | GIT_UNUSED(num); | ||
598 | ##### | 2 | date_time(tm, now, 12); | |
599 | ##### | 3 | } | |
600 | - | |||
601 | ##### | 2 | static void date_tea(struct tm *tm, struct tm *now, int *num) | |
602 | - | { | ||
603 | - | GIT_UNUSED(num); | ||
604 | ##### | 2 | date_time(tm, now, 17); | |
605 | ##### | 3 | } | |
606 | - | |||
607 | ##### | 2 | static void date_pm(struct tm *tm, struct tm *now, int *num) | |
608 | - | { | ||
609 | ##### | 2 | int hour, n = *num; | |
610 | ##### | 2 | *num = 0; | |
611 | - | GIT_UNUSED(now); | ||
612 | - | |||
613 | ##### | 2 | hour = tm->tm_hour; | |
614 | ##### | 2 | if (n) { | |
615 | ##### | 3 | hour = n; | |
616 | ##### | 3 | tm->tm_min = 0; | |
617 | ##### | 3 | tm->tm_sec = 0; | |
618 | - | } | ||
619 | ##### | 4 | tm->tm_hour = (hour % 12) + 12; | |
620 | ##### | 4 | } | |
621 | - | |||
622 | ##### | 2 | static void date_am(struct tm *tm, struct tm *now, int *num) | |
623 | - | { | ||
624 | ##### | 2 | int hour, n = *num; | |
625 | ##### | 2 | *num = 0; | |
626 | - | GIT_UNUSED(now); | ||
627 | - | |||
628 | ##### | 2 | hour = tm->tm_hour; | |
629 | ##### | 2 | if (n) { | |
630 | ##### | 3 | hour = n; | |
631 | ##### | 3 | tm->tm_min = 0; | |
632 | ##### | 3 | tm->tm_sec = 0; | |
633 | - | } | ||
634 | ##### | 4 | tm->tm_hour = (hour % 12); | |
635 | ##### | 4 | } | |
636 | - | |||
637 | ##### | 2 | static void date_never(struct tm *tm, struct tm *now, int *num) | |
638 | - | { | ||
639 | ##### | 2 | time_t n = 0; | |
640 | - | GIT_UNUSED(now); | ||
641 | - | GIT_UNUSED(num); | ||
642 | ##### | 2 | p_localtime_r(&n, tm); | |
643 | ##### | 3 | } | |
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 | 7 | 2 | 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 | 7 | 2 | const char *end = date; | |
682 | - | int i; | ||
683 | - | |||
684 | 30 | 2-4 | while (isalpha(*++end)) | |
685 | - | /* scan to non-alpha */; | ||
686 | - | |||
687 | 91 | 5,9,10 | for (i = 0; i < 12; i++) { | |
688 | 84 | 6 | size_t match = match_string(date, month_names[i]); | |
689 | 84 | 7 | if (match >= 3) { | |
690 | ##### | 8 | tm->tm_mon = i; | |
691 | ##### | 8 | *touched = 1; | |
692 | ##### | 8 | return end; | |
693 | - | } | ||
694 | - | } | ||
695 | - | |||
696 | 63 | 11,16,17 | for (s = special; s->name; s++) { | |
697 | 56 | 12 | size_t len = strlen(s->name); | |
698 | 56 | 12,13 | if (match_string(date, s->name) == len) { | |
699 | ##### | 14 | s->fn(tm, now, num); | |
700 | ##### | 15 | *touched = 1; | |
701 | ##### | 15 | return end; | |
702 | - | } | ||
703 | - | } | ||
704 | - | |||
705 | 7 | 18 | if (!*num) { | |
706 | 33 | 19,23,24 | for (i = 1; i < 11; i++) { | |
707 | 30 | 20 | size_t len = strlen(number_name[i]); | |
708 | 30 | 20,21 | if (match_string(date, number_name[i]) == len) { | |
709 | ##### | 22 | *num = i; | |
710 | ##### | 22 | *touched = 1; | |
711 | ##### | 22 | return end; | |
712 | - | } | ||
713 | - | } | ||
714 | 3 | 25,26 | if (match_string(date, "last") == 4) { | |
715 | ##### | 27 | *num = 1; | |
716 | ##### | 27 | *touched = 1; | |
717 | - | } | ||
718 | 3 | 28 | return end; | |
719 | - | } | ||
720 | - | |||
721 | 4 | 29 | tl = typelen; | |
722 | 12 | 29,35 | while (tl->type) { | |
723 | 11 | 30 | size_t len = strlen(tl->type); | |
724 | 11 | 30,31 | if (match_string(date, tl->type) >= len-1) { | |
725 | 3 | 32 | update_tm(tm, now, tl->length * *num); | |
726 | 3 | 33 | *num = 0; | |
727 | 3 | 33 | *touched = 1; | |
728 | 3 | 33 | return end; | |
729 | - | } | ||
730 | 8 | 34 | tl++; | |
731 | - | } | ||
732 | - | |||
733 | 8 | 36,43,44 | for (i = 0; i < 7; i++) { | |
734 | 7 | 37 | size_t match = match_string(date, weekday_names[i]); | |
735 | 7 | 38 | if (match >= 3) { | |
736 | ##### | 39 | int diff, n = *num -1; | |
737 | ##### | 39 | *num = 0; | |
738 | - | |||
739 | ##### | 39 | diff = tm->tm_wday - i; | |
740 | ##### | 39 | if (diff <= 0) | |
741 | ##### | 40 | n++; | |
742 | ##### | 41 | diff += 7*n; | |
743 | - | |||
744 | ##### | 41 | update_tm(tm, now, diff * 24 * 60 * 60); | |
745 | ##### | 42 | *touched = 1; | |
746 | ##### | 42 | return end; | |
747 | - | } | ||
748 | - | } | ||
749 | - | |||
750 | 1 | 45,46 | if (match_string(date, "months") >= 5) { | |
751 | - | int n; | ||
752 | ##### | 47 | update_tm(tm, now, 0); /* fill in date fields if needed */ | |
753 | ##### | 48 | n = tm->tm_mon - *num; | |
754 | ##### | 48 | *num = 0; | |
755 | ##### | 48,50 | while (n < 0) { | |
756 | ##### | 49 | n += 12; | |
757 | ##### | 49 | tm->tm_year--; | |
758 | - | } | ||
759 | ##### | 51 | tm->tm_mon = n; | |
760 | ##### | 51 | *touched = 1; | |
761 | ##### | 51 | return end; | |
762 | - | } | ||
763 | - | |||
764 | 1 | 52,53 | if (match_string(date, "years") >= 4) { | |
765 | 1 | 54 | update_tm(tm, now, 0); /* fill in date fields if needed */ | |
766 | 1 | 55 | tm->tm_year -= *num; | |
767 | 1 | 55 | *num = 0; | |
768 | 1 | 55 | *touched = 1; | |
769 | 1 | 55 | return end; | |
770 | - | } | ||
771 | - | |||
772 | ##### | 56 | return end; | |
773 | - | } | ||
774 | - | |||
775 | 9 | 2 | static const char *approxidate_digit(const char *date, struct tm *tm, int *num) | |
776 | - | { | ||
777 | - | char *end; | ||
778 | 9 | 2 | unsigned long number = strtoul(date, &end, 10); | |
779 | - | |||
780 | 9 | 3 | switch (*end) { | |
781 | - | case ':': | ||
782 | - | case '.': | ||
783 | - | case '/': | ||
784 | - | case '-': | ||
785 | 5 | 4,5 | if (isdigit(end[1])) { | |
786 | 5 | 6 | size_t match = match_multi_number(number, *end, date, end, tm); | |
787 | 5 | 7 | if (match) | |
788 | 3 | 8 | return date + match; | |
789 | - | } | ||
790 | - | } | ||
791 | - | |||
792 | - | /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */ | ||
793 | 6 | 9,10 | if (date[0] != '0' || end - date <= 2) | |
794 | 6 | 11 | *num = number; | |
795 | 6 | 12 | 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 | 16 | 2 | static void pending_number(struct tm *tm, int *num) | |
804 | - | { | ||
805 | 16 | 2 | int number = *num; | |
806 | - | |||
807 | 16 | 2 | if (number) { | |
808 | 2 | 3 | *num = 0; | |
809 | 2 | 3,4 | if (tm->tm_mday < 0 && number < 32) | |
810 | ##### | 5 | tm->tm_mday = number; | |
811 | 2 | 6,7 | else if (tm->tm_mon < 0 && number < 13) | |
812 | ##### | 8 | tm->tm_mon = number-1; | |
813 | 2 | 9 | else if (tm->tm_year < 0) { | |
814 | 2 | 10,11 | if (number > 1969 && number < 2100) | |
815 | 2 | 12 | tm->tm_year = number - 1900; | |
816 | ##### | 13,14 | else if (number > 69 && number < 100) | |
817 | ##### | 15 | tm->tm_year = number; | |
818 | ##### | 16 | else if (number < 38) | |
819 | ##### | 17 | tm->tm_year = 100 + number; | |
820 | - | /* We mess up for number = 00 ? */ | ||
821 | - | } | ||
822 | - | } | ||
823 | 16 | 18 | } | |
824 | - | |||
825 | 7 | 2 | static git_time_t approxidate_str(const char *date, | |
826 | - | time_t time_sec, | ||
827 | - | int *error_ret) | ||
828 | - | { | ||
829 | 7 | 2 | int number = 0; | |
830 | 7 | 2 | int touched = 0; | |
831 | 7 | 2 | struct tm tm = {0}, now; | |
832 | - | |||
833 | 7 | 2 | p_localtime_r(&time_sec, &tm); | |
834 | 7 | 3 | now = tm; | |
835 | - | |||
836 | 7 | 3 | tm.tm_year = -1; | |
837 | 7 | 3 | tm.tm_mon = -1; | |
838 | 7 | 3 | tm.tm_mday = -1; | |
839 | - | |||
840 | - | for (;;) { | ||
841 | 32 | 4 | unsigned char c = *date; | |
842 | 32 | 4 | if (!c) | |
843 | 7 | 5 | break; | |
844 | 25 | 6 | date++; | |
845 | 25 | 6,7 | if (isdigit(c)) { | |
846 | 9 | 8 | pending_number(&tm, &number); | |
847 | 9 | 9 | date = approxidate_digit(date-1, &tm, &number); | |
848 | 9 | 10 | touched = 1; | |
849 | 9 | 10 | continue; | |
850 | - | } | ||
851 | 16 | 11,12 | if (isalpha(c)) | |
852 | 7 | 13 | date = approxidate_alpha(date-1, &tm, &now, &number, &touched); | |
853 | 25 | 14 | } | |
854 | 7 | 15 | pending_number(&tm, &number); | |
855 | 7 | 16 | if (!touched) | |
856 | ##### | 17 | *error_ret = 1; | |
857 | 7 | 18 | return update_tm(&tm, &now, 0); | |
858 | - | } | ||
859 | - | |||
860 | 12 | 2 | int git__date_parse(git_time_t *out, const char *date) | |
861 | - | { | ||
862 | - | time_t time_sec; | ||
863 | - | git_time_t timestamp; | ||
864 | 12 | 2 | int offset, error_ret=0; | |
865 | - | |||
866 | 12 | 2,3 | if (!parse_date_basic(date, ×tamp, &offset)) { | |
867 | 5 | 4 | *out = timestamp; | |
868 | 5 | 4 | return 0; | |
869 | - | } | ||
870 | - | |||
871 | 7 | 5,6 | if (time(&time_sec) == -1) | |
872 | ##### | 7 | return -1; | |
873 | - | |||
874 | 7 | 8 | *out = approxidate_str(date, time_sec, &error_ret); | |
875 | 7 | 9 | return error_ret; | |
876 | - | } | ||
877 | - | |||
878 | 20 | 2 | 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 | 20 | 2-4 | assert(out && date); | |
885 | - | |||
886 | 20 | 5 | t = (time_t) (date->time + date->offset * 60); | |
887 | - | |||
888 | 20 | 5,6 | if (p_gmtime_r (&t, &gmt) == NULL) | |
889 | ##### | 7 | return -1; | |
890 | - | |||
891 | 20 | 8,8,8,8,8 | written = p_snprintf(out, len, "%.3s, %u %.3s %.4u %02u:%02u:%02u %+03d%02d", | |
892 | 20 | 8 | weekday_names[gmt.tm_wday], | |
893 | - | gmt.tm_mday, | ||
894 | 20 | 8 | month_names[gmt.tm_mon], | |
895 | 20 | 8 | gmt.tm_year + 1900, | |
896 | - | gmt.tm_hour, gmt.tm_min, gmt.tm_sec, | ||
897 | 20 | 8,8 | date->offset / 60, date->offset % 60); | |
898 | - | |||
899 | 20 | 8,9 | if (written < 0 || (written > (int) len - 1)) | |
900 | 1 | 10 | return -1; | |
901 | - | |||
902 | 19 | 11 | return 0; | |
903 | - | } | ||
904 | - |