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