]>
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 | */ | |
23418ea9 | 12 | static 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--; | |
36e4986f LT |
27 | if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0) |
28 | return -1; | |
ecee9d9e ET |
29 | return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL + |
30 | tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec; | |
31 | } | |
32 | ||
33 | static const char *month_names[] = { | |
89967023 LT |
34 | "January", "February", "March", "April", "May", "June", |
35 | "July", "August", "September", "October", "November", "December" | |
ecee9d9e ET |
36 | }; |
37 | ||
38 | static const char *weekday_names[] = { | |
6b7b0427 | 39 | "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays" |
ecee9d9e ET |
40 | }; |
41 | ||
9a8e35e9 LT |
42 | static time_t gm_time_t(unsigned long time, int tz) |
43 | { | |
44 | int minutes; | |
45 | ||
46 | minutes = tz < 0 ? -tz : tz; | |
47 | minutes = (minutes / 100)*60 + (minutes % 100); | |
48 | minutes = tz < 0 ? -minutes : minutes; | |
49 | return time + minutes * 60; | |
50 | } | |
51 | ||
f80cd783 LT |
52 | /* |
53 | * The "tz" thing is passed in as this strange "decimal parse of tz" | |
54 | * thing, which means that tz -0100 is passed in as the integer -100, | |
55 | * even though it means "sixty minutes off" | |
56 | */ | |
2a387043 | 57 | static struct tm *time_to_tm(unsigned long time, int tz) |
f80cd783 | 58 | { |
9a8e35e9 | 59 | time_t t = gm_time_t(time, tz); |
2a387043 JH |
60 | return gmtime(&t); |
61 | } | |
62 | ||
a7b02ccf JH |
63 | /* |
64 | * What value of "tz" was in effect back then at "time" in the | |
65 | * local timezone? | |
66 | */ | |
67 | static int local_tzoffset(unsigned long time) | |
68 | { | |
69 | time_t t, t_local; | |
70 | struct tm tm; | |
71 | int offset, eastwest; | |
72 | ||
73 | t = time; | |
74 | localtime_r(&t, &tm); | |
bb5799d6 | 75 | t_local = tm_to_time_t(&tm); |
a7b02ccf | 76 | |
bab74837 JK |
77 | if (t_local == -1) |
78 | return 0; /* error; just use +0000 */ | |
a7b02ccf JH |
79 | if (t_local < t) { |
80 | eastwest = -1; | |
81 | offset = t - t_local; | |
82 | } else { | |
83 | eastwest = 1; | |
84 | offset = t_local - t; | |
85 | } | |
86 | offset /= 60; /* in minutes */ | |
87 | offset = (offset % 60) + ((offset / 60) * 100); | |
88 | return offset * eastwest; | |
89 | } | |
90 | ||
7d29afd4 | 91 | void show_date_relative(unsigned long time, int tz, |
33012fc4 | 92 | const struct timeval *now, |
7d29afd4 | 93 | struct strbuf *timebuf) |
33012fc4 AR |
94 | { |
95 | unsigned long diff; | |
7d29afd4 JN |
96 | if (now->tv_sec < time) { |
97 | strbuf_addstr(timebuf, _("in the future")); | |
98 | return; | |
99 | } | |
33012fc4 AR |
100 | diff = now->tv_sec - time; |
101 | if (diff < 90) { | |
7d29afd4 JN |
102 | strbuf_addf(timebuf, |
103 | Q_("%lu second ago", "%lu seconds ago", diff), diff); | |
104 | return; | |
33012fc4 AR |
105 | } |
106 | /* Turn it into minutes */ | |
107 | diff = (diff + 30) / 60; | |
108 | if (diff < 90) { | |
7d29afd4 JN |
109 | strbuf_addf(timebuf, |
110 | Q_("%lu minute ago", "%lu minutes ago", diff), diff); | |
111 | return; | |
33012fc4 AR |
112 | } |
113 | /* Turn it into hours */ | |
114 | diff = (diff + 30) / 60; | |
115 | if (diff < 36) { | |
7d29afd4 JN |
116 | strbuf_addf(timebuf, |
117 | Q_("%lu hour ago", "%lu hours ago", diff), diff); | |
118 | return; | |
33012fc4 AR |
119 | } |
120 | /* We deal with number of days from here on */ | |
121 | diff = (diff + 12) / 24; | |
122 | if (diff < 14) { | |
7d29afd4 JN |
123 | strbuf_addf(timebuf, |
124 | Q_("%lu day ago", "%lu days ago", diff), diff); | |
125 | return; | |
33012fc4 AR |
126 | } |
127 | /* Say weeks for the past 10 weeks or so */ | |
128 | if (diff < 70) { | |
7d29afd4 JN |
129 | strbuf_addf(timebuf, |
130 | Q_("%lu week ago", "%lu weeks ago", (diff + 3) / 7), | |
131 | (diff + 3) / 7); | |
132 | return; | |
33012fc4 AR |
133 | } |
134 | /* Say months for the past 12 months or so */ | |
dbc1b1f7 | 135 | if (diff < 365) { |
7d29afd4 JN |
136 | strbuf_addf(timebuf, |
137 | Q_("%lu month ago", "%lu months ago", (diff + 15) / 30), | |
138 | (diff + 15) / 30); | |
139 | return; | |
33012fc4 AR |
140 | } |
141 | /* Give years and months for 5 years or so */ | |
142 | if (diff < 1825) { | |
f1e9c548 MG |
143 | unsigned long totalmonths = (diff * 12 * 2 + 365) / (365 * 2); |
144 | unsigned long years = totalmonths / 12; | |
145 | unsigned long months = totalmonths % 12; | |
7d29afd4 JN |
146 | if (months) { |
147 | struct strbuf sb = STRBUF_INIT; | |
148 | strbuf_addf(&sb, Q_("%lu year", "%lu years", years), years); | |
7d29afd4 | 149 | strbuf_addf(timebuf, |
fcaed04d | 150 | /* TRANSLATORS: "%s" is "<n> years" */ |
7d29afd4 JN |
151 | Q_("%s, %lu month ago", "%s, %lu months ago", months), |
152 | sb.buf, months); | |
153 | strbuf_release(&sb); | |
154 | } else | |
155 | strbuf_addf(timebuf, | |
156 | Q_("%lu year ago", "%lu years ago", years), years); | |
157 | return; | |
33012fc4 AR |
158 | } |
159 | /* Otherwise, just years. Centuries is probably overkill. */ | |
7d29afd4 JN |
160 | strbuf_addf(timebuf, |
161 | Q_("%lu year ago", "%lu years ago", (diff + 183) / 365), | |
162 | (diff + 183) / 365); | |
33012fc4 AR |
163 | } |
164 | ||
a5481a6c JK |
165 | struct date_mode *date_mode_from_type(enum date_mode_type type) |
166 | { | |
167 | static struct date_mode mode; | |
aa1462cc JK |
168 | if (type == DATE_STRFTIME) |
169 | die("BUG: cannot create anonymous strftime date_mode struct"); | |
a5481a6c | 170 | mode.type = type; |
add00ba2 | 171 | mode.local = 0; |
a5481a6c JK |
172 | return &mode; |
173 | } | |
174 | ||
175 | const char *show_date(unsigned long time, int tz, const struct date_mode *mode) | |
2a387043 JH |
176 | { |
177 | struct tm *tm; | |
7d29afd4 | 178 | static struct strbuf timebuf = STRBUF_INIT; |
2a387043 | 179 | |
add00ba2 | 180 | if (mode->local) |
dc6d782c JK |
181 | tz = local_tzoffset(time); |
182 | ||
a5481a6c | 183 | if (mode->type == DATE_RAW) { |
7d29afd4 JN |
184 | strbuf_reset(&timebuf); |
185 | strbuf_addf(&timebuf, "%lu %+05d", time, tz); | |
186 | return timebuf.buf; | |
7dff9b30 LT |
187 | } |
188 | ||
a5481a6c | 189 | if (mode->type == DATE_RELATIVE) { |
9a8e35e9 | 190 | struct timeval now; |
7d29afd4 JN |
191 | |
192 | strbuf_reset(&timebuf); | |
9a8e35e9 | 193 | gettimeofday(&now, NULL); |
7d29afd4 JN |
194 | show_date_relative(time, tz, &now, &timebuf); |
195 | return timebuf.buf; | |
9a8e35e9 LT |
196 | } |
197 | ||
2a387043 | 198 | tm = time_to_tm(time, tz); |
2b15846d JK |
199 | if (!tm) { |
200 | tm = time_to_tm(0, 0); | |
201 | tz = 0; | |
202 | } | |
7d29afd4 JN |
203 | |
204 | strbuf_reset(&timebuf); | |
a5481a6c | 205 | if (mode->type == DATE_SHORT) |
7d29afd4 | 206 | strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900, |
f8493ec0 | 207 | tm->tm_mon + 1, tm->tm_mday); |
a5481a6c | 208 | else if (mode->type == DATE_ISO8601) |
7d29afd4 | 209 | strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d", |
ee8f838e RR |
210 | tm->tm_year + 1900, |
211 | tm->tm_mon + 1, | |
212 | tm->tm_mday, | |
213 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
214 | tz); | |
a5481a6c | 215 | else if (mode->type == DATE_ISO8601_STRICT) { |
466fb674 BB |
216 | char sign = (tz >= 0) ? '+' : '-'; |
217 | tz = abs(tz); | |
218 | strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d", | |
219 | tm->tm_year + 1900, | |
220 | tm->tm_mon + 1, | |
221 | tm->tm_mday, | |
222 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
223 | sign, tz / 100, tz % 100); | |
a5481a6c | 224 | } else if (mode->type == DATE_RFC2822) |
7d29afd4 | 225 | strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d", |
73013afd JH |
226 | weekday_names[tm->tm_wday], tm->tm_mday, |
227 | month_names[tm->tm_mon], tm->tm_year + 1900, | |
228 | tm->tm_hour, tm->tm_min, tm->tm_sec, tz); | |
aa1462cc JK |
229 | else if (mode->type == DATE_STRFTIME) |
230 | strbuf_addftime(&timebuf, mode->strftime_fmt, tm); | |
f8493ec0 | 231 | else |
7d29afd4 | 232 | strbuf_addf(&timebuf, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d", |
f8493ec0 JS |
233 | weekday_names[tm->tm_wday], |
234 | month_names[tm->tm_mon], | |
235 | tm->tm_mday, | |
236 | tm->tm_hour, tm->tm_min, tm->tm_sec, | |
a7b02ccf | 237 | tm->tm_year + 1900, |
add00ba2 | 238 | mode->local ? 0 : ' ', |
a7b02ccf | 239 | tz); |
7d29afd4 | 240 | return timebuf.buf; |
f80cd783 LT |
241 | } |
242 | ||
89967023 LT |
243 | /* |
244 | * Check these. And note how it doesn't do the summer-time conversion. | |
245 | * | |
246 | * In my world, it's always summer, and things are probably a bit off | |
247 | * in other ways too. | |
248 | */ | |
249 | static const struct { | |
250 | const char *name; | |
251 | int offset; | |
5e2a78a4 | 252 | int dst; |
89967023 | 253 | } timezone_names[] = { |
5e2a78a4 LT |
254 | { "IDLW", -12, 0, }, /* International Date Line West */ |
255 | { "NT", -11, 0, }, /* Nome */ | |
256 | { "CAT", -10, 0, }, /* Central Alaska */ | |
257 | { "HST", -10, 0, }, /* Hawaii Standard */ | |
258 | { "HDT", -10, 1, }, /* Hawaii Daylight */ | |
259 | { "YST", -9, 0, }, /* Yukon Standard */ | |
260 | { "YDT", -9, 1, }, /* Yukon Daylight */ | |
261 | { "PST", -8, 0, }, /* Pacific Standard */ | |
262 | { "PDT", -8, 1, }, /* Pacific Daylight */ | |
263 | { "MST", -7, 0, }, /* Mountain Standard */ | |
264 | { "MDT", -7, 1, }, /* Mountain Daylight */ | |
265 | { "CST", -6, 0, }, /* Central Standard */ | |
266 | { "CDT", -6, 1, }, /* Central Daylight */ | |
267 | { "EST", -5, 0, }, /* Eastern Standard */ | |
268 | { "EDT", -5, 1, }, /* Eastern Daylight */ | |
269 | { "AST", -3, 0, }, /* Atlantic Standard */ | |
270 | { "ADT", -3, 1, }, /* Atlantic Daylight */ | |
271 | { "WAT", -1, 0, }, /* West Africa */ | |
272 | ||
273 | { "GMT", 0, 0, }, /* Greenwich Mean */ | |
274 | { "UTC", 0, 0, }, /* Universal (Coordinated) */ | |
75b37e70 | 275 | { "Z", 0, 0, }, /* Zulu, alias for UTC */ |
5e2a78a4 LT |
276 | |
277 | { "WET", 0, 0, }, /* Western European */ | |
278 | { "BST", 0, 1, }, /* British Summer */ | |
279 | { "CET", +1, 0, }, /* Central European */ | |
280 | { "MET", +1, 0, }, /* Middle European */ | |
281 | { "MEWT", +1, 0, }, /* Middle European Winter */ | |
282 | { "MEST", +1, 1, }, /* Middle European Summer */ | |
283 | { "CEST", +1, 1, }, /* Central European Summer */ | |
284 | { "MESZ", +1, 1, }, /* Middle European Summer */ | |
285 | { "FWT", +1, 0, }, /* French Winter */ | |
286 | { "FST", +1, 1, }, /* French Summer */ | |
287 | { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */ | |
92e2311b | 288 | { "EEST", +2, 1, }, /* Eastern European Daylight */ |
5e2a78a4 LT |
289 | { "WAST", +7, 0, }, /* West Australian Standard */ |
290 | { "WADT", +7, 1, }, /* West Australian Daylight */ | |
291 | { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */ | |
292 | { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */ | |
293 | { "EAST", +10, 0, }, /* Eastern Australian Standard */ | |
294 | { "EADT", +10, 1, }, /* Eastern Australian Daylight */ | |
295 | { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */ | |
695ed472 SD |
296 | { "NZT", +12, 0, }, /* New Zealand */ |
297 | { "NZST", +12, 0, }, /* New Zealand Standard */ | |
298 | { "NZDT", +12, 1, }, /* New Zealand Daylight */ | |
5e2a78a4 | 299 | { "IDLE", +12, 0, }, /* International Date Line East */ |
89967023 | 300 | }; |
ecee9d9e | 301 | |
89967023 | 302 | static int match_string(const char *date, const char *str) |
ecee9d9e | 303 | { |
89967023 LT |
304 | int i = 0; |
305 | ||
306 | for (i = 0; *date; date++, str++, i++) { | |
307 | if (*date == *str) | |
308 | continue; | |
309 | if (toupper(*date) == toupper(*str)) | |
310 | continue; | |
311 | if (!isalnum(*date)) | |
312 | break; | |
313 | return 0; | |
314 | } | |
315 | return i; | |
ecee9d9e ET |
316 | } |
317 | ||
68849b54 LT |
318 | static int skip_alpha(const char *date) |
319 | { | |
320 | int i = 0; | |
321 | do { | |
322 | i++; | |
323 | } while (isalpha(date[i])); | |
324 | return i; | |
325 | } | |
326 | ||
89967023 LT |
327 | /* |
328 | * Parse month, weekday, or timezone name | |
329 | */ | |
330 | static int match_alpha(const char *date, struct tm *tm, int *offset) | |
ecee9d9e | 331 | { |
89967023 | 332 | int i; |
ecee9d9e | 333 | |
89967023 LT |
334 | for (i = 0; i < 12; i++) { |
335 | int match = match_string(date, month_names[i]); | |
336 | if (match >= 3) { | |
337 | tm->tm_mon = i; | |
338 | return match; | |
ecee9d9e | 339 | } |
89967023 | 340 | } |
ecee9d9e | 341 | |
89967023 LT |
342 | for (i = 0; i < 7; i++) { |
343 | int match = match_string(date, weekday_names[i]); | |
344 | if (match >= 3) { | |
345 | tm->tm_wday = i; | |
346 | return match; | |
347 | } | |
348 | } | |
ecee9d9e | 349 | |
b4f2a6ac | 350 | for (i = 0; i < ARRAY_SIZE(timezone_names); i++) { |
89967023 | 351 | int match = match_string(date, timezone_names[i].name); |
75b37e70 | 352 | if (match >= 3 || match == strlen(timezone_names[i].name)) { |
5e2a78a4 LT |
353 | int off = timezone_names[i].offset; |
354 | ||
355 | /* This is bogus, but we like summer */ | |
356 | off += timezone_names[i].dst; | |
357 | ||
92e2311b LT |
358 | /* Only use the tz name offset if we don't have anything better */ |
359 | if (*offset == -1) | |
360 | *offset = 60*off; | |
361 | ||
89967023 | 362 | return match; |
ecee9d9e ET |
363 | } |
364 | } | |
ecee9d9e | 365 | |
68849b54 | 366 | if (match_string(date, "PM") == 2) { |
18b633ca LT |
367 | tm->tm_hour = (tm->tm_hour % 12) + 12; |
368 | return 2; | |
369 | } | |
370 | ||
371 | if (match_string(date, "AM") == 2) { | |
372 | tm->tm_hour = (tm->tm_hour % 12) + 0; | |
68849b54 LT |
373 | return 2; |
374 | } | |
375 | ||
89967023 | 376 | /* BAD CRAP */ |
68849b54 | 377 | return skip_alpha(date); |
89967023 | 378 | } |
ecee9d9e | 379 | |
38035cf4 | 380 | static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm) |
89967023 | 381 | { |
198b0fb6 | 382 | if (month > 0 && month < 13 && day > 0 && day < 32) { |
38035cf4 JH |
383 | struct tm check = *tm; |
384 | struct tm *r = (now_tm ? &check : tm); | |
385 | time_t specified; | |
386 | ||
387 | r->tm_mon = month - 1; | |
388 | r->tm_mday = day; | |
198b0fb6 | 389 | if (year == -1) { |
38035cf4 JH |
390 | if (!now_tm) |
391 | return 1; | |
392 | r->tm_year = now_tm->tm_year; | |
89967023 | 393 | } |
38035cf4 JH |
394 | else if (year >= 1970 && year < 2100) |
395 | r->tm_year = year - 1900; | |
396 | else if (year > 70 && year < 100) | |
397 | r->tm_year = year; | |
398 | else if (year < 38) | |
399 | r->tm_year = year + 100; | |
400 | else | |
198b0fb6 | 401 | return 0; |
38035cf4 JH |
402 | if (!now_tm) |
403 | return 1; | |
404 | ||
bb5799d6 | 405 | specified = tm_to_time_t(r); |
198b0fb6 | 406 | |
38035cf4 JH |
407 | /* Be it commit time or author time, it does not make |
408 | * sense to specify timestamp way into the future. Make | |
409 | * sure it is not later than ten days from now... | |
410 | */ | |
e6e87516 | 411 | if ((specified != -1) && (now + 10*24*3600 < specified)) |
38035cf4 JH |
412 | return 0; |
413 | tm->tm_mon = r->tm_mon; | |
414 | tm->tm_mday = r->tm_mday; | |
415 | if (year != -1) | |
416 | tm->tm_year = r->tm_year; | |
198b0fb6 | 417 | return 1; |
89967023 | 418 | } |
198b0fb6 LT |
419 | return 0; |
420 | } | |
ecee9d9e | 421 | |
073281e2 JK |
422 | static int match_multi_number(unsigned long num, char c, const char *date, |
423 | char *end, struct tm *tm, time_t now) | |
198b0fb6 | 424 | { |
38035cf4 JH |
425 | struct tm now_tm; |
426 | struct tm *refuse_future; | |
198b0fb6 LT |
427 | long num2, num3; |
428 | ||
429 | num2 = strtol(end+1, &end, 10); | |
430 | num3 = -1; | |
431 | if (*end == c && isdigit(end[1])) | |
432 | num3 = strtol(end+1, &end, 10); | |
433 | ||
434 | /* Time? Date? */ | |
89967023 | 435 | switch (c) { |
198b0fb6 LT |
436 | case ':': |
437 | if (num3 < 0) | |
438 | num3 = 0; | |
439 | if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) { | |
440 | tm->tm_hour = num; | |
441 | tm->tm_min = num2; | |
442 | tm->tm_sec = num3; | |
89967023 LT |
443 | break; |
444 | } | |
198b0fb6 | 445 | return 0; |
89967023 LT |
446 | |
447 | case '-': | |
448 | case '/': | |
38035cf4 | 449 | case '.': |
073281e2 JK |
450 | if (!now) |
451 | now = time(NULL); | |
38035cf4 JH |
452 | refuse_future = NULL; |
453 | if (gmtime_r(&now, &now_tm)) | |
454 | refuse_future = &now_tm; | |
455 | ||
198b0fb6 LT |
456 | if (num > 70) { |
457 | /* yyyy-mm-dd? */ | |
d3723953 | 458 | if (is_date(num, num2, num3, NULL, now, tm)) |
198b0fb6 LT |
459 | break; |
460 | /* yyyy-dd-mm? */ | |
d3723953 | 461 | if (is_date(num, num3, num2, NULL, now, tm)) |
89967023 | 462 | break; |
198b0fb6 | 463 | } |
38035cf4 JH |
464 | /* Our eastern European friends say dd.mm.yy[yy] |
465 | * is the norm there, so giving precedence to | |
466 | * mm/dd/yy[yy] form only when separator is not '.' | |
467 | */ | |
468 | if (c != '.' && | |
469 | is_date(num3, num, num2, refuse_future, now, tm)) | |
470 | break; | |
471 | /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */ | |
472 | if (is_date(num3, num2, num, refuse_future, now, tm)) | |
89967023 | 473 | break; |
38035cf4 JH |
474 | /* Funny European mm.dd.yy */ |
475 | if (c == '.' && | |
476 | is_date(num3, num, num2, refuse_future, now, tm)) | |
198b0fb6 LT |
477 | break; |
478 | return 0; | |
479 | } | |
480 | return end - date; | |
481 | } | |
482 | ||
36e4986f LT |
483 | /* |
484 | * Have we filled in any part of the time/date yet? | |
485 | * We just do a binary 'and' to see if the sign bit | |
486 | * is set in all the values. | |
487 | */ | |
9f2b6d29 LT |
488 | static inline int nodate(struct tm *tm) |
489 | { | |
36e4986f LT |
490 | return (tm->tm_year & |
491 | tm->tm_mon & | |
492 | tm->tm_mday & | |
493 | tm->tm_hour & | |
494 | tm->tm_min & | |
495 | tm->tm_sec) < 0; | |
9f2b6d29 LT |
496 | } |
497 | ||
198b0fb6 | 498 | /* |
a6080a0a | 499 | * We've seen a digit. Time? Year? Date? |
198b0fb6 | 500 | */ |
26a2d8ae | 501 | static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt) |
198b0fb6 LT |
502 | { |
503 | int n; | |
504 | char *end; | |
505 | unsigned long num; | |
506 | ||
507 | num = strtoul(date, &end, 10); | |
508 | ||
509 | /* | |
a1a5a634 JS |
510 | * Seconds since 1970? We trigger on that for any numbers with |
511 | * more than 8 digits. This is because we don't want to rule out | |
512 | * numbers like 20070606 as a YYYYMMDD date. | |
198b0fb6 | 513 | */ |
9f2b6d29 | 514 | if (num >= 100000000 && nodate(tm)) { |
198b0fb6 | 515 | time_t time = num; |
9cb480f2 JH |
516 | if (gmtime_r(&time, tm)) { |
517 | *tm_gmt = 1; | |
198b0fb6 | 518 | return end - date; |
9cb480f2 | 519 | } |
198b0fb6 LT |
520 | } |
521 | ||
522 | /* | |
38035cf4 | 523 | * Check for special formats: num[-.:/]num[same]num |
198b0fb6 LT |
524 | */ |
525 | switch (*end) { | |
526 | case ':': | |
38035cf4 | 527 | case '.': |
198b0fb6 LT |
528 | case '/': |
529 | case '-': | |
530 | if (isdigit(end[1])) { | |
073281e2 | 531 | int match = match_multi_number(num, *end, date, end, tm, 0); |
198b0fb6 LT |
532 | if (match) |
533 | return match; | |
89967023 LT |
534 | } |
535 | } | |
198b0fb6 LT |
536 | |
537 | /* | |
538 | * None of the special formats? Try to guess what | |
539 | * the number meant. We use the number of digits | |
540 | * to make a more educated guess.. | |
541 | */ | |
542 | n = 0; | |
543 | do { | |
544 | n++; | |
545 | } while (isdigit(date[n])); | |
546 | ||
547 | /* Four-digit year or a timezone? */ | |
548 | if (n == 4) { | |
7122f82f | 549 | if (num <= 1400 && *offset == -1) { |
198b0fb6 LT |
550 | unsigned int minutes = num % 100; |
551 | unsigned int hours = num / 100; | |
552 | *offset = hours*60 + minutes; | |
553 | } else if (num > 1900 && num < 2100) | |
554 | tm->tm_year = num - 1900; | |
555 | return n; | |
556 | } | |
557 | ||
9f2b6d29 LT |
558 | /* |
559 | * Ignore lots of numerals. We took care of 4-digit years above. | |
560 | * Days or months must be one or two digits. | |
561 | */ | |
562 | if (n > 2) | |
563 | return n; | |
564 | ||
198b0fb6 LT |
565 | /* |
566 | * NOTE! We will give precedence to day-of-month over month or | |
82f9d58a | 567 | * year numbers in the 1-12 range. So 05 is always "mday 5", |
198b0fb6 LT |
568 | * unless we already have a mday.. |
569 | * | |
570 | * IOW, 01 Apr 05 parses as "April 1st, 2005". | |
571 | */ | |
572 | if (num > 0 && num < 32 && tm->tm_mday < 0) { | |
573 | tm->tm_mday = num; | |
574 | return n; | |
575 | } | |
576 | ||
577 | /* Two-digit year? */ | |
578 | if (n == 2 && tm->tm_year < 0) { | |
579 | if (num < 10 && tm->tm_mday >= 0) { | |
580 | tm->tm_year = num + 100; | |
581 | return n; | |
582 | } | |
583 | if (num >= 70) { | |
584 | tm->tm_year = num; | |
585 | return n; | |
586 | } | |
587 | } | |
588 | ||
90290552 | 589 | if (num > 0 && num < 13 && tm->tm_mon < 0) |
198b0fb6 | 590 | tm->tm_mon = num-1; |
a6080a0a | 591 | |
198b0fb6 | 592 | return n; |
89967023 | 593 | } |
ecee9d9e | 594 | |
26a2d8ae | 595 | static int match_tz(const char *date, int *offp) |
89967023 LT |
596 | { |
597 | char *end; | |
ee646eb4 HL |
598 | int hour = strtoul(date + 1, &end, 10); |
599 | int n = end - (date + 1); | |
600 | int min = 0; | |
ecee9d9e | 601 | |
ee646eb4 HL |
602 | if (n == 4) { |
603 | /* hhmm */ | |
604 | min = hour % 100; | |
605 | hour = hour / 100; | |
606 | } else if (n != 2) { | |
607 | min = 99; /* random crap */ | |
608 | } else if (*end == ':') { | |
609 | /* hh:mm? */ | |
610 | min = strtoul(end + 1, &end, 10); | |
611 | if (end - (date + 1) != 5) | |
612 | min = 99; /* random crap */ | |
613 | } /* otherwise we parsed "hh" */ | |
ecee9d9e | 614 | |
198b0fb6 | 615 | /* |
ee646eb4 HL |
616 | * Don't accept any random crap. Even though some places have |
617 | * offset larger than 12 hours (e.g. Pacific/Kiritimati is at | |
618 | * UTC+14), there is something wrong if hour part is much | |
619 | * larger than that. We might also want to check that the | |
620 | * minutes are divisible by 15 or something too. (Offset of | |
621 | * Kathmandu, Nepal is UTC+5:45) | |
198b0fb6 | 622 | */ |
ee646eb4 HL |
623 | if (min < 60 && hour < 24) { |
624 | int offset = hour * 60 + min; | |
68849b54 LT |
625 | if (*date == '-') |
626 | offset = -offset; | |
68849b54 LT |
627 | *offp = offset; |
628 | } | |
89967023 LT |
629 | return end - date; |
630 | } | |
ecee9d9e | 631 | |
c33ddc2e | 632 | static void date_string(unsigned long date, int offset, struct strbuf *buf) |
01c6ad29 LT |
633 | { |
634 | int sign = '+'; | |
635 | ||
636 | if (offset < 0) { | |
637 | offset = -offset; | |
638 | sign = '-'; | |
639 | } | |
c33ddc2e | 640 | strbuf_addf(buf, "%lu %c%02d%02d", date, sign, offset / 60, offset % 60); |
01c6ad29 LT |
641 | } |
642 | ||
116eb3ab JH |
643 | /* |
644 | * Parse a string like "0 +0000" as ancient timestamp near epoch, but | |
645 | * only when it appears not as part of any other string. | |
646 | */ | |
647 | static int match_object_header_date(const char *date, unsigned long *timestamp, int *offset) | |
648 | { | |
649 | char *end; | |
650 | unsigned long stamp; | |
651 | int ofs; | |
652 | ||
be21d167 | 653 | if (*date < '0' || '9' < *date) |
116eb3ab JH |
654 | return -1; |
655 | stamp = strtoul(date, &end, 10); | |
656 | if (*end != ' ' || stamp == ULONG_MAX || (end[1] != '+' && end[1] != '-')) | |
657 | return -1; | |
658 | date = end + 2; | |
659 | ofs = strtol(date, &end, 10); | |
660 | if ((*end != '\0' && (*end != '\n')) || end != date + 4) | |
661 | return -1; | |
662 | ofs = (ofs / 100) * 60 + (ofs % 100); | |
663 | if (date[-1] == '-') | |
664 | ofs = -ofs; | |
665 | *timestamp = stamp; | |
666 | *offset = ofs; | |
667 | return 0; | |
668 | } | |
669 | ||
89967023 LT |
670 | /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822 |
671 | (i.e. English) day/month names, and it doesn't work correctly with %z. */ | |
9644c061 | 672 | int parse_date_basic(const char *date, unsigned long *timestamp, int *offset) |
89967023 LT |
673 | { |
674 | struct tm tm; | |
c5043cc1 RR |
675 | int tm_gmt; |
676 | unsigned long dummy_timestamp; | |
677 | int dummy_offset; | |
678 | ||
679 | if (!timestamp) | |
680 | timestamp = &dummy_timestamp; | |
681 | if (!offset) | |
682 | offset = &dummy_offset; | |
ecee9d9e | 683 | |
89967023 LT |
684 | memset(&tm, 0, sizeof(tm)); |
685 | tm.tm_year = -1; | |
686 | tm.tm_mon = -1; | |
687 | tm.tm_mday = -1; | |
eaa85129 | 688 | tm.tm_isdst = -1; |
36e4986f LT |
689 | tm.tm_hour = -1; |
690 | tm.tm_min = -1; | |
691 | tm.tm_sec = -1; | |
c5043cc1 | 692 | *offset = -1; |
9cb480f2 | 693 | tm_gmt = 0; |
89967023 | 694 | |
2c733fb2 JH |
695 | if (*date == '@' && |
696 | !match_object_header_date(date + 1, timestamp, offset)) | |
116eb3ab | 697 | return 0; /* success */ |
89967023 LT |
698 | for (;;) { |
699 | int match = 0; | |
700 | unsigned char c = *date; | |
701 | ||
702 | /* Stop at end of string or newline */ | |
703 | if (!c || c == '\n') | |
704 | break; | |
705 | ||
706 | if (isalpha(c)) | |
c5043cc1 | 707 | match = match_alpha(date, &tm, offset); |
89967023 | 708 | else if (isdigit(c)) |
c5043cc1 | 709 | match = match_digit(date, &tm, offset, &tm_gmt); |
89967023 | 710 | else if ((c == '-' || c == '+') && isdigit(date[1])) |
c5043cc1 | 711 | match = match_tz(date, offset); |
89967023 LT |
712 | |
713 | if (!match) { | |
714 | /* BAD CRAP */ | |
715 | match = 1; | |
a6080a0a | 716 | } |
89967023 LT |
717 | |
718 | date += match; | |
719 | } | |
ecee9d9e | 720 | |
f6e63621 | 721 | /* do not use mktime(), which uses local timezone, here */ |
c5043cc1 | 722 | *timestamp = tm_to_time_t(&tm); |
7fcec48d JH |
723 | if (*timestamp == -1) |
724 | return -1; | |
725 | ||
e1033da6 | 726 | if (*offset == -1) { |
f6e63621 JH |
727 | time_t temp_time; |
728 | ||
729 | /* gmtime_r() in match_digit() may have clobbered it */ | |
730 | tm.tm_isdst = -1; | |
731 | temp_time = mktime(&tm); | |
e1033da6 MG |
732 | if ((time_t)*timestamp > temp_time) { |
733 | *offset = ((time_t)*timestamp - temp_time) / 60; | |
734 | } else { | |
735 | *offset = -(int)((temp_time - (time_t)*timestamp) / 60); | |
736 | } | |
737 | } | |
eaa85129 | 738 | |
9cb480f2 | 739 | if (!tm_gmt) |
c5043cc1 | 740 | *timestamp -= *offset * 60; |
9644c061 | 741 | return 0; /* success */ |
c5043cc1 RR |
742 | } |
743 | ||
3d27b9b0 JH |
744 | int parse_expiry_date(const char *date, unsigned long *timestamp) |
745 | { | |
746 | int errors = 0; | |
747 | ||
748 | if (!strcmp(date, "never") || !strcmp(date, "false")) | |
749 | *timestamp = 0; | |
750 | else if (!strcmp(date, "all") || !strcmp(date, "now")) | |
751 | /* | |
752 | * We take over "now" here, which usually translates | |
753 | * to the current timestamp. This is because the user | |
754 | * really means to expire everything she has done in | |
755 | * the past, and by definition reflogs are the record | |
756 | * of the past, and there is nothing from the future | |
757 | * to be kept. | |
758 | */ | |
759 | *timestamp = ULONG_MAX; | |
760 | else | |
761 | *timestamp = approxidate_careful(date, &errors); | |
762 | ||
763 | return errors; | |
764 | } | |
765 | ||
c33ddc2e | 766 | int parse_date(const char *date, struct strbuf *result) |
c5043cc1 RR |
767 | { |
768 | unsigned long timestamp; | |
769 | int offset; | |
9644c061 | 770 | if (parse_date_basic(date, ×tamp, &offset)) |
c5043cc1 | 771 | return -1; |
c33ddc2e JK |
772 | date_string(timestamp, offset, result); |
773 | return 0; | |
ecee9d9e ET |
774 | } |
775 | ||
add00ba2 JK |
776 | static enum date_mode_type parse_date_type(const char *format, const char **end) |
777 | { | |
778 | if (skip_prefix(format, "relative", end)) | |
779 | return DATE_RELATIVE; | |
780 | if (skip_prefix(format, "iso8601-strict", end) || | |
781 | skip_prefix(format, "iso-strict", end)) | |
782 | return DATE_ISO8601_STRICT; | |
783 | if (skip_prefix(format, "iso8601", end) || | |
784 | skip_prefix(format, "iso", end)) | |
785 | return DATE_ISO8601; | |
786 | if (skip_prefix(format, "rfc2822", end) || | |
787 | skip_prefix(format, "rfc", end)) | |
788 | return DATE_RFC2822; | |
789 | if (skip_prefix(format, "short", end)) | |
790 | return DATE_SHORT; | |
791 | if (skip_prefix(format, "default", end)) | |
792 | return DATE_NORMAL; | |
793 | if (skip_prefix(format, "raw", end)) | |
794 | return DATE_RAW; | |
795 | if (skip_prefix(format, "format", end)) | |
796 | return DATE_STRFTIME; | |
797 | ||
798 | die("unknown date format %s", format); | |
799 | } | |
800 | ||
a5481a6c | 801 | void parse_date_format(const char *format, struct date_mode *mode) |
856665f8 | 802 | { |
add00ba2 JK |
803 | const char *p; |
804 | ||
805 | /* historical alias */ | |
806 | if (!strcmp(format, "local")) | |
807 | format = "default-local"; | |
808 | ||
809 | mode->type = parse_date_type(format, &p); | |
810 | mode->local = 0; | |
811 | ||
812 | if (skip_prefix(p, "-local", &p)) | |
813 | mode->local = 1; | |
814 | ||
815 | if (mode->type == DATE_STRFTIME) { | |
816 | if (!skip_prefix(p, ":", &p)) | |
817 | die("date format missing colon separator: %s", format); | |
818 | mode->strftime_fmt = xstrdup(p); | |
819 | } else if (*p) | |
856665f8 AP |
820 | die("unknown date format %s", format); |
821 | } | |
822 | ||
c33ddc2e | 823 | void datestamp(struct strbuf *out) |
ecee9d9e ET |
824 | { |
825 | time_t now; | |
826 | int offset; | |
827 | ||
828 | time(&now); | |
829 | ||
bb5799d6 | 830 | offset = tm_to_time_t(localtime(&now)) - now; |
ecee9d9e ET |
831 | offset /= 60; |
832 | ||
c33ddc2e | 833 | date_string(now, offset, out); |
ecee9d9e | 834 | } |
3c07b1d1 | 835 | |
90290552 LT |
836 | /* |
837 | * Relative time update (eg "2 days ago"). If we haven't set the time | |
838 | * yet, we need to set it from current time. | |
839 | */ | |
840 | static unsigned long update_tm(struct tm *tm, struct tm *now, unsigned long sec) | |
3c07b1d1 | 841 | { |
90290552 LT |
842 | time_t n; |
843 | ||
844 | if (tm->tm_mday < 0) | |
845 | tm->tm_mday = now->tm_mday; | |
846 | if (tm->tm_mon < 0) | |
847 | tm->tm_mon = now->tm_mon; | |
848 | if (tm->tm_year < 0) { | |
849 | tm->tm_year = now->tm_year; | |
850 | if (tm->tm_mon > now->tm_mon) | |
851 | tm->tm_year--; | |
852 | } | |
853 | ||
854 | n = mktime(tm) - sec; | |
3c07b1d1 | 855 | localtime_r(&n, tm); |
90290552 | 856 | return n; |
3c07b1d1 LT |
857 | } |
858 | ||
93cfa7c7 JH |
859 | static void date_now(struct tm *tm, struct tm *now, int *num) |
860 | { | |
861 | update_tm(tm, now, 0); | |
862 | } | |
863 | ||
90290552 | 864 | static void date_yesterday(struct tm *tm, struct tm *now, int *num) |
a8aca418 | 865 | { |
90290552 | 866 | update_tm(tm, now, 24*60*60); |
a8aca418 LT |
867 | } |
868 | ||
90290552 | 869 | static void date_time(struct tm *tm, struct tm *now, int hour) |
a8aca418 LT |
870 | { |
871 | if (tm->tm_hour < hour) | |
90290552 | 872 | date_yesterday(tm, now, NULL); |
a8aca418 LT |
873 | tm->tm_hour = hour; |
874 | tm->tm_min = 0; | |
875 | tm->tm_sec = 0; | |
876 | } | |
877 | ||
90290552 | 878 | static void date_midnight(struct tm *tm, struct tm *now, int *num) |
a8aca418 | 879 | { |
90290552 | 880 | date_time(tm, now, 0); |
a8aca418 LT |
881 | } |
882 | ||
90290552 | 883 | static void date_noon(struct tm *tm, struct tm *now, int *num) |
a8aca418 | 884 | { |
90290552 | 885 | date_time(tm, now, 12); |
a8aca418 LT |
886 | } |
887 | ||
90290552 | 888 | static void date_tea(struct tm *tm, struct tm *now, int *num) |
a8aca418 | 889 | { |
90290552 | 890 | date_time(tm, now, 17); |
a8aca418 LT |
891 | } |
892 | ||
90290552 | 893 | static void date_pm(struct tm *tm, struct tm *now, int *num) |
393d340e | 894 | { |
18b633ca | 895 | int hour, n = *num; |
393d340e LT |
896 | *num = 0; |
897 | ||
18b633ca LT |
898 | hour = tm->tm_hour; |
899 | if (n) { | |
900 | hour = n; | |
393d340e LT |
901 | tm->tm_min = 0; |
902 | tm->tm_sec = 0; | |
903 | } | |
18b633ca | 904 | tm->tm_hour = (hour % 12) + 12; |
393d340e LT |
905 | } |
906 | ||
90290552 | 907 | static void date_am(struct tm *tm, struct tm *now, int *num) |
393d340e | 908 | { |
18b633ca | 909 | int hour, n = *num; |
393d340e LT |
910 | *num = 0; |
911 | ||
18b633ca LT |
912 | hour = tm->tm_hour; |
913 | if (n) { | |
914 | hour = n; | |
393d340e LT |
915 | tm->tm_min = 0; |
916 | tm->tm_sec = 0; | |
917 | } | |
18b633ca | 918 | tm->tm_hour = (hour % 12); |
393d340e LT |
919 | } |
920 | ||
90290552 | 921 | static void date_never(struct tm *tm, struct tm *now, int *num) |
af66366a | 922 | { |
8c6b5786 OM |
923 | time_t n = 0; |
924 | localtime_r(&n, tm); | |
af66366a JS |
925 | } |
926 | ||
a8aca418 LT |
927 | static const struct special { |
928 | const char *name; | |
90290552 | 929 | void (*fn)(struct tm *, struct tm *, int *); |
a8aca418 LT |
930 | } special[] = { |
931 | { "yesterday", date_yesterday }, | |
932 | { "noon", date_noon }, | |
933 | { "midnight", date_midnight }, | |
934 | { "tea", date_tea }, | |
393d340e LT |
935 | { "PM", date_pm }, |
936 | { "AM", date_am }, | |
af66366a | 937 | { "never", date_never }, |
93cfa7c7 | 938 | { "now", date_now }, |
a8aca418 LT |
939 | { NULL } |
940 | }; | |
941 | ||
3c07b1d1 LT |
942 | static const char *number_name[] = { |
943 | "zero", "one", "two", "three", "four", | |
944 | "five", "six", "seven", "eight", "nine", "ten", | |
945 | }; | |
946 | ||
a8aca418 | 947 | static const struct typelen { |
3c07b1d1 LT |
948 | const char *type; |
949 | int length; | |
950 | } typelen[] = { | |
951 | { "seconds", 1 }, | |
952 | { "minutes", 60 }, | |
953 | { "hours", 60*60 }, | |
954 | { "days", 24*60*60 }, | |
955 | { "weeks", 7*24*60*60 }, | |
956 | { NULL } | |
a6080a0a | 957 | }; |
3c07b1d1 | 958 | |
93cfa7c7 | 959 | static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched) |
3c07b1d1 | 960 | { |
a8aca418 LT |
961 | const struct typelen *tl; |
962 | const struct special *s; | |
3c07b1d1 | 963 | const char *end = date; |
5df7dbba | 964 | int i; |
3c07b1d1 | 965 | |
38db01b7 | 966 | while (isalpha(*++end)) |
5df7dbba | 967 | ; |
3c07b1d1 LT |
968 | |
969 | for (i = 0; i < 12; i++) { | |
970 | int match = match_string(date, month_names[i]); | |
971 | if (match >= 3) { | |
972 | tm->tm_mon = i; | |
93cfa7c7 | 973 | *touched = 1; |
3c07b1d1 LT |
974 | return end; |
975 | } | |
976 | } | |
977 | ||
a8aca418 LT |
978 | for (s = special; s->name; s++) { |
979 | int len = strlen(s->name); | |
980 | if (match_string(date, s->name) == len) { | |
90290552 | 981 | s->fn(tm, now, num); |
93cfa7c7 | 982 | *touched = 1; |
a8aca418 LT |
983 | return end; |
984 | } | |
3c07b1d1 LT |
985 | } |
986 | ||
987 | if (!*num) { | |
988 | for (i = 1; i < 11; i++) { | |
989 | int len = strlen(number_name[i]); | |
990 | if (match_string(date, number_name[i]) == len) { | |
991 | *num = i; | |
93cfa7c7 | 992 | *touched = 1; |
3c07b1d1 LT |
993 | return end; |
994 | } | |
995 | } | |
93cfa7c7 | 996 | if (match_string(date, "last") == 4) { |
3c07b1d1 | 997 | *num = 1; |
93cfa7c7 JH |
998 | *touched = 1; |
999 | } | |
3c07b1d1 LT |
1000 | return end; |
1001 | } | |
1002 | ||
1003 | tl = typelen; | |
1004 | while (tl->type) { | |
1005 | int len = strlen(tl->type); | |
1006 | if (match_string(date, tl->type) >= len-1) { | |
90290552 | 1007 | update_tm(tm, now, tl->length * *num); |
3c07b1d1 | 1008 | *num = 0; |
93cfa7c7 | 1009 | *touched = 1; |
3c07b1d1 LT |
1010 | return end; |
1011 | } | |
1012 | tl++; | |
1013 | } | |
1014 | ||
6b7b0427 LT |
1015 | for (i = 0; i < 7; i++) { |
1016 | int match = match_string(date, weekday_names[i]); | |
1017 | if (match >= 3) { | |
1018 | int diff, n = *num -1; | |
1019 | *num = 0; | |
1020 | ||
1021 | diff = tm->tm_wday - i; | |
1022 | if (diff <= 0) | |
1023 | n++; | |
1024 | diff += 7*n; | |
1025 | ||
90290552 | 1026 | update_tm(tm, now, diff * 24 * 60 * 60); |
93cfa7c7 | 1027 | *touched = 1; |
6b7b0427 LT |
1028 | return end; |
1029 | } | |
1030 | } | |
1031 | ||
3c07b1d1 | 1032 | if (match_string(date, "months") >= 5) { |
931e8e27 JK |
1033 | int n; |
1034 | update_tm(tm, now, 0); /* fill in date fields if needed */ | |
1035 | n = tm->tm_mon - *num; | |
3c07b1d1 LT |
1036 | *num = 0; |
1037 | while (n < 0) { | |
1038 | n += 12; | |
1039 | tm->tm_year--; | |
1040 | } | |
1041 | tm->tm_mon = n; | |
93cfa7c7 | 1042 | *touched = 1; |
3c07b1d1 LT |
1043 | return end; |
1044 | } | |
1045 | ||
1046 | if (match_string(date, "years") >= 4) { | |
931e8e27 | 1047 | update_tm(tm, now, 0); /* fill in date fields if needed */ |
3c07b1d1 LT |
1048 | tm->tm_year -= *num; |
1049 | *num = 0; | |
93cfa7c7 | 1050 | *touched = 1; |
3c07b1d1 LT |
1051 | return end; |
1052 | } | |
1053 | ||
1054 | return end; | |
1055 | } | |
1056 | ||
073281e2 JK |
1057 | static const char *approxidate_digit(const char *date, struct tm *tm, int *num, |
1058 | time_t now) | |
e92a54d9 LT |
1059 | { |
1060 | char *end; | |
1061 | unsigned long number = strtoul(date, &end, 10); | |
1062 | ||
393d340e LT |
1063 | switch (*end) { |
1064 | case ':': | |
1065 | case '.': | |
1066 | case '/': | |
1067 | case '-': | |
1068 | if (isdigit(end[1])) { | |
073281e2 JK |
1069 | int match = match_multi_number(number, *end, date, end, |
1070 | tm, now); | |
393d340e LT |
1071 | if (match) |
1072 | return date + match; | |
1073 | } | |
1074 | } | |
1075 | ||
9f2b6d29 LT |
1076 | /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */ |
1077 | if (date[0] != '0' || end - date <= 2) | |
1078 | *num = number; | |
e92a54d9 LT |
1079 | return end; |
1080 | } | |
1081 | ||
90290552 LT |
1082 | /* |
1083 | * Do we have a pending number at the end, or when | |
1084 | * we see a new one? Let's assume it's a month day, | |
1085 | * as in "Dec 6, 1992" | |
1086 | */ | |
1087 | static void pending_number(struct tm *tm, int *num) | |
1088 | { | |
1089 | int number = *num; | |
1090 | ||
1091 | if (number) { | |
1092 | *num = 0; | |
1093 | if (tm->tm_mday < 0 && number < 32) | |
1094 | tm->tm_mday = number; | |
36e4986f LT |
1095 | else if (tm->tm_mon < 0 && number < 13) |
1096 | tm->tm_mon = number-1; | |
1097 | else if (tm->tm_year < 0) { | |
1098 | if (number > 1969 && number < 2100) | |
1099 | tm->tm_year = number - 1900; | |
1100 | else if (number > 69 && number < 100) | |
1101 | tm->tm_year = number; | |
1102 | else if (number < 38) | |
1103 | tm->tm_year = 100 + number; | |
1104 | /* We screw up for number = 00 ? */ | |
1105 | } | |
90290552 LT |
1106 | } |
1107 | } | |
1108 | ||
93cfa7c7 JH |
1109 | static unsigned long approxidate_str(const char *date, |
1110 | const struct timeval *tv, | |
1111 | int *error_ret) | |
3c07b1d1 LT |
1112 | { |
1113 | int number = 0; | |
93cfa7c7 | 1114 | int touched = 0; |
3c07b1d1 | 1115 | struct tm tm, now; |
f697b33b | 1116 | time_t time_sec; |
3c07b1d1 | 1117 | |
33012fc4 | 1118 | time_sec = tv->tv_sec; |
f697b33b | 1119 | localtime_r(&time_sec, &tm); |
3c07b1d1 | 1120 | now = tm; |
90290552 LT |
1121 | |
1122 | tm.tm_year = -1; | |
1123 | tm.tm_mon = -1; | |
1124 | tm.tm_mday = -1; | |
1125 | ||
3c07b1d1 LT |
1126 | for (;;) { |
1127 | unsigned char c = *date; | |
1128 | if (!c) | |
1129 | break; | |
1130 | date++; | |
1131 | if (isdigit(c)) { | |
90290552 | 1132 | pending_number(&tm, &number); |
073281e2 | 1133 | date = approxidate_digit(date-1, &tm, &number, time_sec); |
93cfa7c7 | 1134 | touched = 1; |
3c07b1d1 LT |
1135 | continue; |
1136 | } | |
1137 | if (isalpha(c)) | |
93cfa7c7 | 1138 | date = approxidate_alpha(date-1, &tm, &now, &number, &touched); |
3c07b1d1 | 1139 | } |
90290552 | 1140 | pending_number(&tm, &number); |
93cfa7c7 JH |
1141 | if (!touched) |
1142 | *error_ret = 1; | |
90290552 | 1143 | return update_tm(&tm, &now, 0); |
3c07b1d1 | 1144 | } |
33012fc4 AR |
1145 | |
1146 | unsigned long approxidate_relative(const char *date, const struct timeval *tv) | |
1147 | { | |
c5043cc1 RR |
1148 | unsigned long timestamp; |
1149 | int offset; | |
93cfa7c7 | 1150 | int errors = 0; |
33012fc4 | 1151 | |
9644c061 | 1152 | if (!parse_date_basic(date, ×tamp, &offset)) |
c5043cc1 | 1153 | return timestamp; |
93cfa7c7 | 1154 | return approxidate_str(date, tv, &errors); |
33012fc4 AR |
1155 | } |
1156 | ||
93cfa7c7 | 1157 | unsigned long approxidate_careful(const char *date, int *error_ret) |
33012fc4 AR |
1158 | { |
1159 | struct timeval tv; | |
c5043cc1 RR |
1160 | unsigned long timestamp; |
1161 | int offset; | |
93cfa7c7 JH |
1162 | int dummy = 0; |
1163 | if (!error_ret) | |
1164 | error_ret = &dummy; | |
33012fc4 | 1165 | |
9644c061 | 1166 | if (!parse_date_basic(date, ×tamp, &offset)) { |
93cfa7c7 | 1167 | *error_ret = 0; |
c5043cc1 | 1168 | return timestamp; |
93cfa7c7 | 1169 | } |
33012fc4 AR |
1170 | |
1171 | gettimeofday(&tv, NULL); | |
93cfa7c7 | 1172 | return approxidate_str(date, &tv, error_ret); |
33012fc4 | 1173 | } |
7ca36d93 JK |
1174 | |
1175 | int date_overflows(unsigned long t) | |
1176 | { | |
1177 | time_t sys; | |
1178 | ||
1179 | /* If we overflowed our unsigned long, that's bad... */ | |
1180 | if (t == ULONG_MAX) | |
1181 | return 1; | |
1182 | ||
1183 | /* | |
1184 | * ...but we also are going to feed the result to system | |
1185 | * functions that expect time_t, which is often "signed long". | |
1186 | * Make sure that we fit into time_t, as well. | |
1187 | */ | |
1188 | sys = t; | |
1189 | return t != sys || (t < 1) != (sys < 1); | |
1190 | } |