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