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