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