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