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