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