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