2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 * This is like mktime, but without normalization of tm_wday and tm_yday.
12 static time_t tm_to_time_t(const struct tm *tm)
14 static const int mdays[] = {
15 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
17 int year = tm->tm_year - 70;
18 int month = tm->tm_mon;
19 int day = tm->tm_mday;
21 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
23 if (month < 0 || month > 11) /* array bounds */
25 if (month < 2 || (year + 2) % 4)
27 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
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;
33 static const char *month_names[] = {
34 "January", "February", "March", "April", "May", "June",
35 "July", "August", "September", "October", "November", "December"
38 static const char *weekday_names[] = {
39 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
42 static time_t gm_time_t(timestamp_t time, int tz)
46 minutes = tz < 0 ? -tz : tz;
47 minutes = (minutes / 100)*60 + (minutes % 100);
48 minutes = tz < 0 ? -minutes : minutes;
51 if (unsigned_add_overflows(time, minutes * 60))
52 die("Timestamp+tz too large: %"PRItime" +%04d",
54 } else if (time < -minutes * 60)
55 die("Timestamp before Unix epoch: %"PRItime" %04d", time, tz);
57 if (date_overflows(time))
58 die("Timestamp too large for this system: %"PRItime, time);
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"
67 static struct tm *time_to_tm(timestamp_t time, int tz)
69 time_t t = gm_time_t(time, tz);
73 static struct tm *time_to_tm_local(timestamp_t time)
80 * Fill in the localtime 'struct tm' for the supplied time,
81 * and return the local tz.
83 static int local_time_tzoffset(time_t t, struct tm *tm)
89 t_local = tm_to_time_t(tm);
91 return 0; /* error; just use +0000 */
99 offset /= 60; /* in minutes */
100 offset = (offset % 60) + ((offset / 60) * 100);
101 return offset * eastwest;
105 * What value of "tz" was in effect back then at "time" in the
108 static int local_tzoffset(timestamp_t time)
112 if (date_overflows(time))
113 die("Timestamp too large for this system: %"PRItime, time);
115 return local_time_tzoffset((time_t)time, &tm);
118 static void get_time(struct timeval *now)
122 x = getenv("GIT_TEST_DATE_NOW");
124 now->tv_sec = atoi(x);
128 gettimeofday(now, NULL);
131 void show_date_relative(timestamp_t time,
132 const struct timeval *now,
133 struct strbuf *timebuf)
136 if (now->tv_sec < time) {
137 strbuf_addstr(timebuf, _("in the future"));
140 diff = now->tv_sec - time;
143 Q_("%"PRItime" second ago", "%"PRItime" seconds ago", diff), diff);
146 /* Turn it into minutes */
147 diff = (diff + 30) / 60;
150 Q_("%"PRItime" minute ago", "%"PRItime" minutes ago", diff), diff);
153 /* Turn it into hours */
154 diff = (diff + 30) / 60;
157 Q_("%"PRItime" hour ago", "%"PRItime" hours ago", diff), diff);
160 /* We deal with number of days from here on */
161 diff = (diff + 12) / 24;
164 Q_("%"PRItime" day ago", "%"PRItime" days ago", diff), diff);
167 /* Say weeks for the past 10 weeks or so */
170 Q_("%"PRItime" week ago", "%"PRItime" weeks ago", (diff + 3) / 7),
174 /* Say months for the past 12 months or so */
177 Q_("%"PRItime" month ago", "%"PRItime" months ago", (diff + 15) / 30),
181 /* Give years and months for 5 years or so */
183 timestamp_t totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
184 timestamp_t years = totalmonths / 12;
185 timestamp_t months = totalmonths % 12;
187 struct strbuf sb = STRBUF_INIT;
188 strbuf_addf(&sb, Q_("%"PRItime" year", "%"PRItime" years", years), years);
190 /* TRANSLATORS: "%s" is "<n> years" */
191 Q_("%s, %"PRItime" month ago", "%s, %"PRItime" months ago", months),
196 Q_("%"PRItime" year ago", "%"PRItime" years ago", years), years);
199 /* Otherwise, just years. Centuries is probably overkill. */
201 Q_("%"PRItime" year ago", "%"PRItime" years ago", (diff + 183) / 365),
205 struct date_mode *date_mode_from_type(enum date_mode_type type)
207 static struct date_mode mode;
208 if (type == DATE_STRFTIME)
209 BUG("cannot create anonymous strftime date_mode struct");
215 static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm, int tz, struct tm *human_tm, int human_tz, int local)
226 hide.tz = local || tz == human_tz;
227 hide.year = tm->tm_year == human_tm->tm_year;
229 if (tm->tm_mon == human_tm->tm_mon) {
230 if (tm->tm_mday > human_tm->tm_mday) {
231 /* Future date: think timezones */
232 } else if (tm->tm_mday == human_tm->tm_mday) {
233 hide.date = hide.wday = 1;
234 } else if (tm->tm_mday + 5 > human_tm->tm_mday) {
235 /* Leave just weekday if it was a few days ago */
241 /* Show "today" times as just relative times */
245 show_date_relative(time, &now, buf);
250 * Always hide seconds for human-readable.
251 * Hide timezone if showing date.
252 * Hide weekday and time if showing year.
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
258 if (human_tm->tm_year) {
260 hide.tz |= !hide.date;
261 hide.wday = hide.time = !hide.year;
265 strbuf_addf(buf, "%.3s ", weekday_names[tm->tm_wday]);
267 strbuf_addf(buf, "%.3s %d ", month_names[tm->tm_mon], tm->tm_mday);
269 /* Do we want AM/PM depending on locale? */
271 strbuf_addf(buf, "%02d:%02d", tm->tm_hour, tm->tm_min);
273 strbuf_addf(buf, ":%02d", tm->tm_sec);
278 strbuf_addf(buf, " %d", tm->tm_year + 1900);
281 strbuf_addf(buf, " %+05d", tz);
284 const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
287 struct tm human_tm = { 0 };
289 static struct strbuf timebuf = STRBUF_INIT;
291 if (mode->type == DATE_UNIX) {
292 strbuf_reset(&timebuf);
293 strbuf_addf(&timebuf, "%"PRItime, time);
297 if (mode->type == DATE_HUMAN) {
302 /* Fill in the data for "current time" in human_tz and human_tm */
303 human_tz = local_time_tzoffset(now.tv_sec, &human_tm);
307 tz = local_tzoffset(time);
309 if (mode->type == DATE_RAW) {
310 strbuf_reset(&timebuf);
311 strbuf_addf(&timebuf, "%"PRItime" %+05d", time, tz);
315 if (mode->type == DATE_RELATIVE) {
318 strbuf_reset(&timebuf);
320 show_date_relative(time, &now, &timebuf);
325 tm = time_to_tm_local(time);
327 tm = time_to_tm(time, tz);
329 tm = time_to_tm(0, 0);
333 strbuf_reset(&timebuf);
334 if (mode->type == DATE_SHORT)
335 strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
336 tm->tm_mon + 1, tm->tm_mday);
337 else if (mode->type == DATE_ISO8601)
338 strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
342 tm->tm_hour, tm->tm_min, tm->tm_sec,
344 else if (mode->type == DATE_ISO8601_STRICT) {
345 char sign = (tz >= 0) ? '+' : '-';
347 strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
351 tm->tm_hour, tm->tm_min, tm->tm_sec,
352 sign, tz / 100, tz % 100);
353 } else if (mode->type == DATE_RFC2822)
354 strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
355 weekday_names[tm->tm_wday], tm->tm_mday,
356 month_names[tm->tm_mon], tm->tm_year + 1900,
357 tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
358 else if (mode->type == DATE_STRFTIME)
359 strbuf_addftime(&timebuf, mode->strftime_fmt, tm, tz,
362 show_date_normal(&timebuf, time, tm, tz, &human_tm, human_tz, mode->local);
367 * Check these. And note how it doesn't do the summer-time conversion.
369 * In my world, it's always summer, and things are probably a bit off
372 static const struct {
376 } timezone_names[] = {
377 { "IDLW", -12, 0, }, /* International Date Line West */
378 { "NT", -11, 0, }, /* Nome */
379 { "CAT", -10, 0, }, /* Central Alaska */
380 { "HST", -10, 0, }, /* Hawaii Standard */
381 { "HDT", -10, 1, }, /* Hawaii Daylight */
382 { "YST", -9, 0, }, /* Yukon Standard */
383 { "YDT", -9, 1, }, /* Yukon Daylight */
384 { "PST", -8, 0, }, /* Pacific Standard */
385 { "PDT", -8, 1, }, /* Pacific Daylight */
386 { "MST", -7, 0, }, /* Mountain Standard */
387 { "MDT", -7, 1, }, /* Mountain Daylight */
388 { "CST", -6, 0, }, /* Central Standard */
389 { "CDT", -6, 1, }, /* Central Daylight */
390 { "EST", -5, 0, }, /* Eastern Standard */
391 { "EDT", -5, 1, }, /* Eastern Daylight */
392 { "AST", -3, 0, }, /* Atlantic Standard */
393 { "ADT", -3, 1, }, /* Atlantic Daylight */
394 { "WAT", -1, 0, }, /* West Africa */
396 { "GMT", 0, 0, }, /* Greenwich Mean */
397 { "UTC", 0, 0, }, /* Universal (Coordinated) */
398 { "Z", 0, 0, }, /* Zulu, alias for UTC */
400 { "WET", 0, 0, }, /* Western European */
401 { "BST", 0, 1, }, /* British Summer */
402 { "CET", +1, 0, }, /* Central European */
403 { "MET", +1, 0, }, /* Middle European */
404 { "MEWT", +1, 0, }, /* Middle European Winter */
405 { "MEST", +1, 1, }, /* Middle European Summer */
406 { "CEST", +1, 1, }, /* Central European Summer */
407 { "MESZ", +1, 1, }, /* Middle European Summer */
408 { "FWT", +1, 0, }, /* French Winter */
409 { "FST", +1, 1, }, /* French Summer */
410 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
411 { "EEST", +2, 1, }, /* Eastern European Daylight */
412 { "WAST", +7, 0, }, /* West Australian Standard */
413 { "WADT", +7, 1, }, /* West Australian Daylight */
414 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
415 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
416 { "EAST", +10, 0, }, /* Eastern Australian Standard */
417 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
418 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
419 { "NZT", +12, 0, }, /* New Zealand */
420 { "NZST", +12, 0, }, /* New Zealand Standard */
421 { "NZDT", +12, 1, }, /* New Zealand Daylight */
422 { "IDLE", +12, 0, }, /* International Date Line East */
425 static int match_string(const char *date, const char *str)
429 for (i = 0; *date; date++, str++, i++) {
432 if (toupper(*date) == toupper(*str))
441 static int skip_alpha(const char *date)
446 } while (isalpha(date[i]));
451 * Parse month, weekday, or timezone name
453 static int match_alpha(const char *date, struct tm *tm, int *offset)
457 for (i = 0; i < 12; i++) {
458 int match = match_string(date, month_names[i]);
465 for (i = 0; i < 7; i++) {
466 int match = match_string(date, weekday_names[i]);
473 for (i = 0; i < ARRAY_SIZE(timezone_names); i++) {
474 int match = match_string(date, timezone_names[i].name);
475 if (match >= 3 || match == strlen(timezone_names[i].name)) {
476 int off = timezone_names[i].offset;
478 /* This is bogus, but we like summer */
479 off += timezone_names[i].dst;
481 /* Only use the tz name offset if we don't have anything better */
489 if (match_string(date, "PM") == 2) {
490 tm->tm_hour = (tm->tm_hour % 12) + 12;
494 if (match_string(date, "AM") == 2) {
495 tm->tm_hour = (tm->tm_hour % 12) + 0;
500 return skip_alpha(date);
503 static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
505 if (month > 0 && month < 13 && day > 0 && day < 32) {
506 struct tm check = *tm;
507 struct tm *r = (now_tm ? &check : tm);
510 r->tm_mon = month - 1;
515 r->tm_year = now_tm->tm_year;
517 else if (year >= 1970 && year < 2100)
518 r->tm_year = year - 1900;
519 else if (year > 70 && year < 100)
522 r->tm_year = year + 100;
528 specified = tm_to_time_t(r);
530 /* Be it commit time or author time, it does not make
531 * sense to specify timestamp way into the future. Make
532 * sure it is not later than ten days from now...
534 if ((specified != -1) && (now + 10*24*3600 < specified))
536 tm->tm_mon = r->tm_mon;
537 tm->tm_mday = r->tm_mday;
539 tm->tm_year = r->tm_year;
545 static int match_multi_number(timestamp_t num, char c, const char *date,
546 char *end, struct tm *tm, time_t now)
549 struct tm *refuse_future;
552 num2 = strtol(end+1, &end, 10);
554 if (*end == c && isdigit(end[1]))
555 num3 = strtol(end+1, &end, 10);
562 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
575 refuse_future = NULL;
576 if (gmtime_r(&now, &now_tm))
577 refuse_future = &now_tm;
581 if (is_date(num, num2, num3, NULL, now, tm))
584 if (is_date(num, num3, num2, NULL, now, tm))
587 /* Our eastern European friends say dd.mm.yy[yy]
588 * is the norm there, so giving precedence to
589 * mm/dd/yy[yy] form only when separator is not '.'
592 is_date(num3, num, num2, refuse_future, now, tm))
594 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
595 if (is_date(num3, num2, num, refuse_future, now, tm))
597 /* Funny European mm.dd.yy */
599 is_date(num3, num, num2, refuse_future, now, tm))
607 * Have we filled in any part of the time/date yet?
608 * We just do a binary 'and' to see if the sign bit
609 * is set in all the values.
611 static inline int nodate(struct tm *tm)
613 return (tm->tm_year &
622 * We've seen a digit. Time? Year? Date?
624 static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt)
630 num = parse_timestamp(date, &end, 10);
633 * Seconds since 1970? We trigger on that for any numbers with
634 * more than 8 digits. This is because we don't want to rule out
635 * numbers like 20070606 as a YYYYMMDD date.
637 if (num >= 100000000 && nodate(tm)) {
639 if (gmtime_r(&time, tm)) {
646 * Check for special formats: num[-.:/]num[same]num
653 if (isdigit(end[1])) {
654 int match = match_multi_number(num, *end, date, end, tm, 0);
661 * None of the special formats? Try to guess what
662 * the number meant. We use the number of digits
663 * to make a more educated guess..
668 } while (isdigit(date[n]));
670 /* Four-digit year or a timezone? */
672 if (num <= 1400 && *offset == -1) {
673 unsigned int minutes = num % 100;
674 unsigned int hours = num / 100;
675 *offset = hours*60 + minutes;
676 } else if (num > 1900 && num < 2100)
677 tm->tm_year = num - 1900;
682 * Ignore lots of numerals. We took care of 4-digit years above.
683 * Days or months must be one or two digits.
689 * NOTE! We will give precedence to day-of-month over month or
690 * year numbers in the 1-12 range. So 05 is always "mday 5",
691 * unless we already have a mday..
693 * IOW, 01 Apr 05 parses as "April 1st, 2005".
695 if (num > 0 && num < 32 && tm->tm_mday < 0) {
700 /* Two-digit year? */
701 if (n == 2 && tm->tm_year < 0) {
702 if (num < 10 && tm->tm_mday >= 0) {
703 tm->tm_year = num + 100;
712 if (num > 0 && num < 13 && tm->tm_mon < 0)
718 static int match_tz(const char *date, int *offp)
721 int hour = strtoul(date + 1, &end, 10);
722 int n = end - (date + 1);
730 min = 99; /* random crap */
731 } else if (*end == ':') {
733 min = strtoul(end + 1, &end, 10);
734 if (end - (date + 1) != 5)
735 min = 99; /* random crap */
736 } /* otherwise we parsed "hh" */
739 * Don't accept any random crap. Even though some places have
740 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
741 * UTC+14), there is something wrong if hour part is much
742 * larger than that. We might also want to check that the
743 * minutes are divisible by 15 or something too. (Offset of
744 * Kathmandu, Nepal is UTC+5:45)
746 if (min < 60 && hour < 24) {
747 int offset = hour * 60 + min;
755 static void date_string(timestamp_t date, int offset, struct strbuf *buf)
763 strbuf_addf(buf, "%"PRItime" %c%02d%02d", date, sign, offset / 60, offset % 60);
767 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
768 * only when it appears not as part of any other string.
770 static int match_object_header_date(const char *date, timestamp_t *timestamp, int *offset)
776 if (*date < '0' || '9' < *date)
778 stamp = parse_timestamp(date, &end, 10);
779 if (*end != ' ' || stamp == TIME_MAX || (end[1] != '+' && end[1] != '-'))
782 ofs = strtol(date, &end, 10);
783 if ((*end != '\0' && (*end != '\n')) || end != date + 4)
785 ofs = (ofs / 100) * 60 + (ofs % 100);
793 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
794 (i.e. English) day/month names, and it doesn't work correctly with %z. */
795 int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset)
799 timestamp_t dummy_timestamp;
803 timestamp = &dummy_timestamp;
805 offset = &dummy_offset;
807 memset(&tm, 0, sizeof(tm));
819 !match_object_header_date(date + 1, timestamp, offset))
820 return 0; /* success */
823 unsigned char c = *date;
825 /* Stop at end of string or newline */
830 match = match_alpha(date, &tm, offset);
832 match = match_digit(date, &tm, offset, &tm_gmt);
833 else if ((c == '-' || c == '+') && isdigit(date[1]))
834 match = match_tz(date, offset);
844 /* do not use mktime(), which uses local timezone, here */
845 *timestamp = tm_to_time_t(&tm);
846 if (*timestamp == -1)
852 /* gmtime_r() in match_digit() may have clobbered it */
854 temp_time = mktime(&tm);
855 if ((time_t)*timestamp > temp_time) {
856 *offset = ((time_t)*timestamp - temp_time) / 60;
858 *offset = -(int)((temp_time - (time_t)*timestamp) / 60);
863 *timestamp -= *offset * 60;
864 return 0; /* success */
867 int parse_expiry_date(const char *date, timestamp_t *timestamp)
871 if (!strcmp(date, "never") || !strcmp(date, "false"))
873 else if (!strcmp(date, "all") || !strcmp(date, "now"))
875 * We take over "now" here, which usually translates
876 * to the current timestamp. This is because the user
877 * really means to expire everything she has done in
878 * the past, and by definition reflogs are the record
879 * of the past, and there is nothing from the future
882 *timestamp = TIME_MAX;
884 *timestamp = approxidate_careful(date, &errors);
889 int parse_date(const char *date, struct strbuf *result)
891 timestamp_t timestamp;
893 if (parse_date_basic(date, ×tamp, &offset))
895 date_string(timestamp, offset, result);
899 static enum date_mode_type parse_date_type(const char *format, const char **end)
901 if (skip_prefix(format, "relative", end))
902 return DATE_RELATIVE;
903 if (skip_prefix(format, "iso8601-strict", end) ||
904 skip_prefix(format, "iso-strict", end))
905 return DATE_ISO8601_STRICT;
906 if (skip_prefix(format, "iso8601", end) ||
907 skip_prefix(format, "iso", end))
909 if (skip_prefix(format, "rfc2822", end) ||
910 skip_prefix(format, "rfc", end))
912 if (skip_prefix(format, "short", end))
914 if (skip_prefix(format, "default", end))
916 if (skip_prefix(format, "human", end))
918 if (skip_prefix(format, "raw", end))
920 if (skip_prefix(format, "unix", end))
922 if (skip_prefix(format, "format", end))
923 return DATE_STRFTIME;
925 * Please update $__git_log_date_formats in
926 * git-completion.bash when you add new formats.
929 die("unknown date format %s", format);
932 void parse_date_format(const char *format, struct date_mode *mode)
936 /* "auto:foo" is "if tty/pager, then foo, otherwise normal" */
937 if (skip_prefix(format, "auto:", &p)) {
938 if (isatty(1) || pager_in_use())
944 /* historical alias */
945 if (!strcmp(format, "local"))
946 format = "default-local";
948 mode->type = parse_date_type(format, &p);
951 if (skip_prefix(p, "-local", &p))
954 if (mode->type == DATE_STRFTIME) {
955 if (!skip_prefix(p, ":", &p))
956 die("date format missing colon separator: %s", format);
957 mode->strftime_fmt = xstrdup(p);
959 die("unknown date format %s", format);
962 void datestamp(struct strbuf *out)
969 offset = tm_to_time_t(localtime(&now)) - now;
972 date_string(now, offset, out);
976 * Relative time update (eg "2 days ago"). If we haven't set the time
977 * yet, we need to set it from current time.
979 static time_t update_tm(struct tm *tm, struct tm *now, time_t sec)
984 tm->tm_mday = now->tm_mday;
986 tm->tm_mon = now->tm_mon;
987 if (tm->tm_year < 0) {
988 tm->tm_year = now->tm_year;
989 if (tm->tm_mon > now->tm_mon)
993 n = mktime(tm) - sec;
999 * Do we have a pending number at the end, or when
1000 * we see a new one? Let's assume it's a month day,
1001 * as in "Dec 6, 1992"
1003 static void pending_number(struct tm *tm, int *num)
1009 if (tm->tm_mday < 0 && number < 32)
1010 tm->tm_mday = number;
1011 else if (tm->tm_mon < 0 && number < 13)
1012 tm->tm_mon = number-1;
1013 else if (tm->tm_year < 0) {
1014 if (number > 1969 && number < 2100)
1015 tm->tm_year = number - 1900;
1016 else if (number > 69 && number < 100)
1017 tm->tm_year = number;
1018 else if (number < 38)
1019 tm->tm_year = 100 + number;
1020 /* We screw up for number = 00 ? */
1025 static void date_now(struct tm *tm, struct tm *now, int *num)
1028 update_tm(tm, now, 0);
1031 static void date_yesterday(struct tm *tm, struct tm *now, int *num)
1034 update_tm(tm, now, 24*60*60);
1037 static void date_time(struct tm *tm, struct tm *now, int hour)
1039 if (tm->tm_hour < hour)
1040 update_tm(tm, now, 24*60*60);
1046 static void date_midnight(struct tm *tm, struct tm *now, int *num)
1048 pending_number(tm, num);
1049 date_time(tm, now, 0);
1052 static void date_noon(struct tm *tm, struct tm *now, int *num)
1054 pending_number(tm, num);
1055 date_time(tm, now, 12);
1058 static void date_tea(struct tm *tm, struct tm *now, int *num)
1060 pending_number(tm, num);
1061 date_time(tm, now, 17);
1064 static void date_pm(struct tm *tm, struct tm *now, int *num)
1075 tm->tm_hour = (hour % 12) + 12;
1078 static void date_am(struct tm *tm, struct tm *now, int *num)
1089 tm->tm_hour = (hour % 12);
1092 static void date_never(struct tm *tm, struct tm *now, int *num)
1095 localtime_r(&n, tm);
1099 static const struct special {
1101 void (*fn)(struct tm *, struct tm *, int *);
1103 { "yesterday", date_yesterday },
1104 { "noon", date_noon },
1105 { "midnight", date_midnight },
1106 { "tea", date_tea },
1109 { "never", date_never },
1110 { "now", date_now },
1114 static const char *number_name[] = {
1115 "zero", "one", "two", "three", "four",
1116 "five", "six", "seven", "eight", "nine", "ten",
1119 static const struct typelen {
1126 { "days", 24*60*60 },
1127 { "weeks", 7*24*60*60 },
1131 static const char *approxidate_alpha(const char *date, struct tm *tm, struct tm *now, int *num, int *touched)
1133 const struct typelen *tl;
1134 const struct special *s;
1135 const char *end = date;
1138 while (isalpha(*++end))
1141 for (i = 0; i < 12; i++) {
1142 int match = match_string(date, month_names[i]);
1150 for (s = special; s->name; s++) {
1151 int len = strlen(s->name);
1152 if (match_string(date, s->name) == len) {
1153 s->fn(tm, now, num);
1160 for (i = 1; i < 11; i++) {
1161 int len = strlen(number_name[i]);
1162 if (match_string(date, number_name[i]) == len) {
1168 if (match_string(date, "last") == 4) {
1177 int len = strlen(tl->type);
1178 if (match_string(date, tl->type) >= len-1) {
1179 update_tm(tm, now, tl->length * *num);
1187 for (i = 0; i < 7; i++) {
1188 int match = match_string(date, weekday_names[i]);
1190 int diff, n = *num -1;
1193 diff = tm->tm_wday - i;
1198 update_tm(tm, now, diff * 24 * 60 * 60);
1204 if (match_string(date, "months") >= 5) {
1206 update_tm(tm, now, 0); /* fill in date fields if needed */
1207 n = tm->tm_mon - *num;
1218 if (match_string(date, "years") >= 4) {
1219 update_tm(tm, now, 0); /* fill in date fields if needed */
1220 tm->tm_year -= *num;
1229 static const char *approxidate_digit(const char *date, struct tm *tm, int *num,
1233 timestamp_t number = parse_timestamp(date, &end, 10);
1240 if (isdigit(end[1])) {
1241 int match = match_multi_number(number, *end, date, end,
1244 return date + match;
1248 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1249 if (date[0] != '0' || end - date <= 2)
1254 static timestamp_t approxidate_str(const char *date,
1255 const struct timeval *tv,
1263 time_sec = tv->tv_sec;
1264 localtime_r(&time_sec, &tm);
1272 unsigned char c = *date;
1277 pending_number(&tm, &number);
1278 date = approxidate_digit(date-1, &tm, &number, time_sec);
1283 date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
1285 pending_number(&tm, &number);
1288 return (timestamp_t)update_tm(&tm, &now, 0);
1291 timestamp_t approxidate_relative(const char *date, const struct timeval *tv)
1293 timestamp_t timestamp;
1297 if (!parse_date_basic(date, ×tamp, &offset))
1299 return approxidate_str(date, tv, &errors);
1302 timestamp_t approxidate_careful(const char *date, int *error_ret)
1305 timestamp_t timestamp;
1311 if (!parse_date_basic(date, ×tamp, &offset)) {
1317 return approxidate_str(date, &tv, error_ret);
1320 int date_overflows(timestamp_t t)
1324 /* If we overflowed our timestamp data type, that's bad... */
1325 if ((uintmax_t)t >= TIME_MAX)
1329 * ...but we also are going to feed the result to system
1330 * functions that expect time_t, which is often "signed long".
1331 * Make sure that we fit into time_t, as well.
1334 return t != sys || (t < 1) != (sys < 1);