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
);
74 * What value of "tz" was in effect back then at "time" in the
77 static int local_tzoffset(timestamp_t time
)
83 if (date_overflows(time
))
84 die("Timestamp too large for this system: %"PRItime
, time
);
88 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
;
104 void show_date_relative(timestamp_t time
, int tz
,
105 const struct timeval
*now
,
106 struct strbuf
*timebuf
)
109 if (now
->tv_sec
< time
) {
110 strbuf_addstr(timebuf
, _("in the future"));
113 diff
= now
->tv_sec
- time
;
116 Q_("%"PRItime
" second ago", "%"PRItime
" seconds ago", diff
), diff
);
119 /* Turn it into minutes */
120 diff
= (diff
+ 30) / 60;
123 Q_("%"PRItime
" minute ago", "%"PRItime
" minutes ago", diff
), diff
);
126 /* Turn it into hours */
127 diff
= (diff
+ 30) / 60;
130 Q_("%"PRItime
" hour ago", "%"PRItime
" hours ago", diff
), diff
);
133 /* We deal with number of days from here on */
134 diff
= (diff
+ 12) / 24;
137 Q_("%"PRItime
" day ago", "%"PRItime
" days ago", diff
), diff
);
140 /* Say weeks for the past 10 weeks or so */
143 Q_("%"PRItime
" week ago", "%"PRItime
" weeks ago", (diff
+ 3) / 7),
147 /* Say months for the past 12 months or so */
150 Q_("%"PRItime
" month ago", "%"PRItime
" months ago", (diff
+ 15) / 30),
154 /* Give years and months for 5 years or so */
156 timestamp_t totalmonths
= (diff
* 12 * 2 + 365) / (365 * 2);
157 timestamp_t years
= totalmonths
/ 12;
158 timestamp_t months
= totalmonths
% 12;
160 struct strbuf sb
= STRBUF_INIT
;
161 strbuf_addf(&sb
, Q_("%"PRItime
" year", "%"PRItime
" years", years
), years
);
163 /* TRANSLATORS: "%s" is "<n> years" */
164 Q_("%s, %"PRItime
" month ago", "%s, %"PRItime
" months ago", months
),
169 Q_("%"PRItime
" year ago", "%"PRItime
" years ago", years
), years
);
172 /* Otherwise, just years. Centuries is probably overkill. */
174 Q_("%"PRItime
" year ago", "%"PRItime
" years ago", (diff
+ 183) / 365),
178 struct date_mode
*date_mode_from_type(enum date_mode_type type
)
180 static struct date_mode mode
;
181 if (type
== DATE_STRFTIME
)
182 die("BUG: cannot create anonymous strftime date_mode struct");
188 const char *show_date(timestamp_t time
, int tz
, const struct date_mode
*mode
)
191 static struct strbuf timebuf
= STRBUF_INIT
;
193 if (mode
->type
== DATE_UNIX
) {
194 strbuf_reset(&timebuf
);
195 strbuf_addf(&timebuf
, "%"PRItime
, time
);
200 tz
= local_tzoffset(time
);
202 if (mode
->type
== DATE_RAW
) {
203 strbuf_reset(&timebuf
);
204 strbuf_addf(&timebuf
, "%"PRItime
" %+05d", time
, tz
);
208 if (mode
->type
== DATE_RELATIVE
) {
211 strbuf_reset(&timebuf
);
212 gettimeofday(&now
, NULL
);
213 show_date_relative(time
, tz
, &now
, &timebuf
);
217 tm
= time_to_tm(time
, tz
);
219 tm
= time_to_tm(0, 0);
223 strbuf_reset(&timebuf
);
224 if (mode
->type
== DATE_SHORT
)
225 strbuf_addf(&timebuf
, "%04d-%02d-%02d", tm
->tm_year
+ 1900,
226 tm
->tm_mon
+ 1, tm
->tm_mday
);
227 else if (mode
->type
== DATE_ISO8601
)
228 strbuf_addf(&timebuf
, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
232 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
234 else if (mode
->type
== DATE_ISO8601_STRICT
) {
235 char sign
= (tz
>= 0) ? '+' : '-';
237 strbuf_addf(&timebuf
, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
241 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
242 sign
, tz
/ 100, tz
% 100);
243 } else if (mode
->type
== DATE_RFC2822
)
244 strbuf_addf(&timebuf
, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
245 weekday_names
[tm
->tm_wday
], tm
->tm_mday
,
246 month_names
[tm
->tm_mon
], tm
->tm_year
+ 1900,
247 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, tz
);
248 else if (mode
->type
== DATE_STRFTIME
)
249 strbuf_addftime(&timebuf
, mode
->strftime_fmt
, tm
);
251 strbuf_addf(&timebuf
, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
252 weekday_names
[tm
->tm_wday
],
253 month_names
[tm
->tm_mon
],
255 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
257 mode
->local
? 0 : ' ',
263 * Check these. And note how it doesn't do the summer-time conversion.
265 * In my world, it's always summer, and things are probably a bit off
268 static const struct {
272 } timezone_names
[] = {
273 { "IDLW", -12, 0, }, /* International Date Line West */
274 { "NT", -11, 0, }, /* Nome */
275 { "CAT", -10, 0, }, /* Central Alaska */
276 { "HST", -10, 0, }, /* Hawaii Standard */
277 { "HDT", -10, 1, }, /* Hawaii Daylight */
278 { "YST", -9, 0, }, /* Yukon Standard */
279 { "YDT", -9, 1, }, /* Yukon Daylight */
280 { "PST", -8, 0, }, /* Pacific Standard */
281 { "PDT", -8, 1, }, /* Pacific Daylight */
282 { "MST", -7, 0, }, /* Mountain Standard */
283 { "MDT", -7, 1, }, /* Mountain Daylight */
284 { "CST", -6, 0, }, /* Central Standard */
285 { "CDT", -6, 1, }, /* Central Daylight */
286 { "EST", -5, 0, }, /* Eastern Standard */
287 { "EDT", -5, 1, }, /* Eastern Daylight */
288 { "AST", -3, 0, }, /* Atlantic Standard */
289 { "ADT", -3, 1, }, /* Atlantic Daylight */
290 { "WAT", -1, 0, }, /* West Africa */
292 { "GMT", 0, 0, }, /* Greenwich Mean */
293 { "UTC", 0, 0, }, /* Universal (Coordinated) */
294 { "Z", 0, 0, }, /* Zulu, alias for UTC */
296 { "WET", 0, 0, }, /* Western European */
297 { "BST", 0, 1, }, /* British Summer */
298 { "CET", +1, 0, }, /* Central European */
299 { "MET", +1, 0, }, /* Middle European */
300 { "MEWT", +1, 0, }, /* Middle European Winter */
301 { "MEST", +1, 1, }, /* Middle European Summer */
302 { "CEST", +1, 1, }, /* Central European Summer */
303 { "MESZ", +1, 1, }, /* Middle European Summer */
304 { "FWT", +1, 0, }, /* French Winter */
305 { "FST", +1, 1, }, /* French Summer */
306 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
307 { "EEST", +2, 1, }, /* Eastern European Daylight */
308 { "WAST", +7, 0, }, /* West Australian Standard */
309 { "WADT", +7, 1, }, /* West Australian Daylight */
310 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
311 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
312 { "EAST", +10, 0, }, /* Eastern Australian Standard */
313 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
314 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
315 { "NZT", +12, 0, }, /* New Zealand */
316 { "NZST", +12, 0, }, /* New Zealand Standard */
317 { "NZDT", +12, 1, }, /* New Zealand Daylight */
318 { "IDLE", +12, 0, }, /* International Date Line East */
321 static int match_string(const char *date
, const char *str
)
325 for (i
= 0; *date
; date
++, str
++, i
++) {
328 if (toupper(*date
) == toupper(*str
))
337 static int skip_alpha(const char *date
)
342 } while (isalpha(date
[i
]));
347 * Parse month, weekday, or timezone name
349 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
353 for (i
= 0; i
< 12; i
++) {
354 int match
= match_string(date
, month_names
[i
]);
361 for (i
= 0; i
< 7; i
++) {
362 int match
= match_string(date
, weekday_names
[i
]);
369 for (i
= 0; i
< ARRAY_SIZE(timezone_names
); i
++) {
370 int match
= match_string(date
, timezone_names
[i
].name
);
371 if (match
>= 3 || match
== strlen(timezone_names
[i
].name
)) {
372 int off
= timezone_names
[i
].offset
;
374 /* This is bogus, but we like summer */
375 off
+= timezone_names
[i
].dst
;
377 /* Only use the tz name offset if we don't have anything better */
385 if (match_string(date
, "PM") == 2) {
386 tm
->tm_hour
= (tm
->tm_hour
% 12) + 12;
390 if (match_string(date
, "AM") == 2) {
391 tm
->tm_hour
= (tm
->tm_hour
% 12) + 0;
396 return skip_alpha(date
);
399 static int is_date(int year
, int month
, int day
, struct tm
*now_tm
, time_t now
, struct tm
*tm
)
401 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
402 struct tm check
= *tm
;
403 struct tm
*r
= (now_tm
? &check
: tm
);
406 r
->tm_mon
= month
- 1;
411 r
->tm_year
= now_tm
->tm_year
;
413 else if (year
>= 1970 && year
< 2100)
414 r
->tm_year
= year
- 1900;
415 else if (year
> 70 && year
< 100)
418 r
->tm_year
= year
+ 100;
424 specified
= tm_to_time_t(r
);
426 /* Be it commit time or author time, it does not make
427 * sense to specify timestamp way into the future. Make
428 * sure it is not later than ten days from now...
430 if ((specified
!= -1) && (now
+ 10*24*3600 < specified
))
432 tm
->tm_mon
= r
->tm_mon
;
433 tm
->tm_mday
= r
->tm_mday
;
435 tm
->tm_year
= r
->tm_year
;
441 static int match_multi_number(timestamp_t num
, char c
, const char *date
,
442 char *end
, struct tm
*tm
, time_t now
)
445 struct tm
*refuse_future
;
448 num2
= strtol(end
+1, &end
, 10);
450 if (*end
== c
&& isdigit(end
[1]))
451 num3
= strtol(end
+1, &end
, 10);
458 if (num
< 25 && num2
>= 0 && num2
< 60 && num3
>= 0 && num3
<= 60) {
471 refuse_future
= NULL
;
472 if (gmtime_r(&now
, &now_tm
))
473 refuse_future
= &now_tm
;
477 if (is_date(num
, num2
, num3
, NULL
, now
, tm
))
480 if (is_date(num
, num3
, num2
, NULL
, now
, tm
))
483 /* Our eastern European friends say dd.mm.yy[yy]
484 * is the norm there, so giving precedence to
485 * mm/dd/yy[yy] form only when separator is not '.'
488 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
490 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
491 if (is_date(num3
, num2
, num
, refuse_future
, now
, tm
))
493 /* Funny European mm.dd.yy */
495 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
503 * Have we filled in any part of the time/date yet?
504 * We just do a binary 'and' to see if the sign bit
505 * is set in all the values.
507 static inline int nodate(struct tm
*tm
)
509 return (tm
->tm_year
&
518 * We've seen a digit. Time? Year? Date?
520 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
526 num
= parse_timestamp(date
, &end
, 10);
529 * Seconds since 1970? We trigger on that for any numbers with
530 * more than 8 digits. This is because we don't want to rule out
531 * numbers like 20070606 as a YYYYMMDD date.
533 if (num
>= 100000000 && nodate(tm
)) {
535 if (gmtime_r(&time
, tm
)) {
542 * Check for special formats: num[-.:/]num[same]num
549 if (isdigit(end
[1])) {
550 int match
= match_multi_number(num
, *end
, date
, end
, tm
, 0);
557 * None of the special formats? Try to guess what
558 * the number meant. We use the number of digits
559 * to make a more educated guess..
564 } while (isdigit(date
[n
]));
566 /* Four-digit year or a timezone? */
568 if (num
<= 1400 && *offset
== -1) {
569 unsigned int minutes
= num
% 100;
570 unsigned int hours
= num
/ 100;
571 *offset
= hours
*60 + minutes
;
572 } else if (num
> 1900 && num
< 2100)
573 tm
->tm_year
= num
- 1900;
578 * Ignore lots of numerals. We took care of 4-digit years above.
579 * Days or months must be one or two digits.
585 * NOTE! We will give precedence to day-of-month over month or
586 * year numbers in the 1-12 range. So 05 is always "mday 5",
587 * unless we already have a mday..
589 * IOW, 01 Apr 05 parses as "April 1st, 2005".
591 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
596 /* Two-digit year? */
597 if (n
== 2 && tm
->tm_year
< 0) {
598 if (num
< 10 && tm
->tm_mday
>= 0) {
599 tm
->tm_year
= num
+ 100;
608 if (num
> 0 && num
< 13 && tm
->tm_mon
< 0)
614 static int match_tz(const char *date
, int *offp
)
617 int hour
= strtoul(date
+ 1, &end
, 10);
618 int n
= end
- (date
+ 1);
626 min
= 99; /* random crap */
627 } else if (*end
== ':') {
629 min
= strtoul(end
+ 1, &end
, 10);
630 if (end
- (date
+ 1) != 5)
631 min
= 99; /* random crap */
632 } /* otherwise we parsed "hh" */
635 * Don't accept any random crap. Even though some places have
636 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
637 * UTC+14), there is something wrong if hour part is much
638 * larger than that. We might also want to check that the
639 * minutes are divisible by 15 or something too. (Offset of
640 * Kathmandu, Nepal is UTC+5:45)
642 if (min
< 60 && hour
< 24) {
643 int offset
= hour
* 60 + min
;
651 static void date_string(timestamp_t date
, int offset
, struct strbuf
*buf
)
659 strbuf_addf(buf
, "%"PRItime
" %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
663 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
664 * only when it appears not as part of any other string.
666 static int match_object_header_date(const char *date
, timestamp_t
*timestamp
, int *offset
)
672 if (*date
< '0' || '9' < *date
)
674 stamp
= parse_timestamp(date
, &end
, 10);
675 if (*end
!= ' ' || stamp
== TIME_MAX
|| (end
[1] != '+' && end
[1] != '-'))
678 ofs
= strtol(date
, &end
, 10);
679 if ((*end
!= '\0' && (*end
!= '\n')) || end
!= date
+ 4)
681 ofs
= (ofs
/ 100) * 60 + (ofs
% 100);
689 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
690 (i.e. English) day/month names, and it doesn't work correctly with %z. */
691 int parse_date_basic(const char *date
, timestamp_t
*timestamp
, int *offset
)
695 timestamp_t dummy_timestamp
;
699 timestamp
= &dummy_timestamp
;
701 offset
= &dummy_offset
;
703 memset(&tm
, 0, sizeof(tm
));
715 !match_object_header_date(date
+ 1, timestamp
, offset
))
716 return 0; /* success */
719 unsigned char c
= *date
;
721 /* Stop at end of string or newline */
726 match
= match_alpha(date
, &tm
, offset
);
728 match
= match_digit(date
, &tm
, offset
, &tm_gmt
);
729 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
730 match
= match_tz(date
, offset
);
740 /* do not use mktime(), which uses local timezone, here */
741 *timestamp
= tm_to_time_t(&tm
);
742 if (*timestamp
== -1)
748 /* gmtime_r() in match_digit() may have clobbered it */
750 temp_time
= mktime(&tm
);
751 if ((time_t)*timestamp
> temp_time
) {
752 *offset
= ((time_t)*timestamp
- temp_time
) / 60;
754 *offset
= -(int)((temp_time
- (time_t)*timestamp
) / 60);
759 *timestamp
-= *offset
* 60;
760 return 0; /* success */
763 int parse_expiry_date(const char *date
, timestamp_t
*timestamp
)
767 if (!strcmp(date
, "never") || !strcmp(date
, "false"))
769 else if (!strcmp(date
, "all") || !strcmp(date
, "now"))
771 * We take over "now" here, which usually translates
772 * to the current timestamp. This is because the user
773 * really means to expire everything she has done in
774 * the past, and by definition reflogs are the record
775 * of the past, and there is nothing from the future
778 *timestamp
= TIME_MAX
;
780 *timestamp
= approxidate_careful(date
, &errors
);
785 int parse_date(const char *date
, struct strbuf
*result
)
787 timestamp_t timestamp
;
789 if (parse_date_basic(date
, ×tamp
, &offset
))
791 date_string(timestamp
, offset
, result
);
795 static enum date_mode_type
parse_date_type(const char *format
, const char **end
)
797 if (skip_prefix(format
, "relative", end
))
798 return DATE_RELATIVE
;
799 if (skip_prefix(format
, "iso8601-strict", end
) ||
800 skip_prefix(format
, "iso-strict", end
))
801 return DATE_ISO8601_STRICT
;
802 if (skip_prefix(format
, "iso8601", end
) ||
803 skip_prefix(format
, "iso", end
))
805 if (skip_prefix(format
, "rfc2822", end
) ||
806 skip_prefix(format
, "rfc", end
))
808 if (skip_prefix(format
, "short", end
))
810 if (skip_prefix(format
, "default", end
))
812 if (skip_prefix(format
, "raw", end
))
814 if (skip_prefix(format
, "unix", end
))
816 if (skip_prefix(format
, "format", end
))
817 return DATE_STRFTIME
;
819 die("unknown date format %s", format
);
822 void parse_date_format(const char *format
, struct date_mode
*mode
)
826 /* historical alias */
827 if (!strcmp(format
, "local"))
828 format
= "default-local";
830 mode
->type
= parse_date_type(format
, &p
);
833 if (skip_prefix(p
, "-local", &p
))
836 if (mode
->type
== DATE_STRFTIME
) {
837 if (!skip_prefix(p
, ":", &p
))
838 die("date format missing colon separator: %s", format
);
839 mode
->strftime_fmt
= xstrdup(p
);
841 die("unknown date format %s", format
);
844 void datestamp(struct strbuf
*out
)
851 offset
= tm_to_time_t(localtime(&now
)) - now
;
854 date_string(now
, offset
, out
);
858 * Relative time update (eg "2 days ago"). If we haven't set the time
859 * yet, we need to set it from current time.
861 static time_t update_tm(struct tm
*tm
, struct tm
*now
, time_t sec
)
866 tm
->tm_mday
= now
->tm_mday
;
868 tm
->tm_mon
= now
->tm_mon
;
869 if (tm
->tm_year
< 0) {
870 tm
->tm_year
= now
->tm_year
;
871 if (tm
->tm_mon
> now
->tm_mon
)
875 n
= mktime(tm
) - sec
;
880 static void date_now(struct tm
*tm
, struct tm
*now
, int *num
)
882 update_tm(tm
, now
, 0);
885 static void date_yesterday(struct tm
*tm
, struct tm
*now
, int *num
)
887 update_tm(tm
, now
, 24*60*60);
890 static void date_time(struct tm
*tm
, struct tm
*now
, int hour
)
892 if (tm
->tm_hour
< hour
)
893 date_yesterday(tm
, now
, NULL
);
899 static void date_midnight(struct tm
*tm
, struct tm
*now
, int *num
)
901 date_time(tm
, now
, 0);
904 static void date_noon(struct tm
*tm
, struct tm
*now
, int *num
)
906 date_time(tm
, now
, 12);
909 static void date_tea(struct tm
*tm
, struct tm
*now
, int *num
)
911 date_time(tm
, now
, 17);
914 static void date_pm(struct tm
*tm
, struct tm
*now
, int *num
)
925 tm
->tm_hour
= (hour
% 12) + 12;
928 static void date_am(struct tm
*tm
, struct tm
*now
, int *num
)
939 tm
->tm_hour
= (hour
% 12);
942 static void date_never(struct tm
*tm
, struct tm
*now
, int *num
)
948 static const struct special
{
950 void (*fn
)(struct tm
*, struct tm
*, int *);
952 { "yesterday", date_yesterday
},
953 { "noon", date_noon
},
954 { "midnight", date_midnight
},
958 { "never", date_never
},
963 static const char *number_name
[] = {
964 "zero", "one", "two", "three", "four",
965 "five", "six", "seven", "eight", "nine", "ten",
968 static const struct typelen
{
975 { "days", 24*60*60 },
976 { "weeks", 7*24*60*60 },
980 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, struct tm
*now
, int *num
, int *touched
)
982 const struct typelen
*tl
;
983 const struct special
*s
;
984 const char *end
= date
;
987 while (isalpha(*++end
))
990 for (i
= 0; i
< 12; i
++) {
991 int match
= match_string(date
, month_names
[i
]);
999 for (s
= special
; s
->name
; s
++) {
1000 int len
= strlen(s
->name
);
1001 if (match_string(date
, s
->name
) == len
) {
1002 s
->fn(tm
, now
, num
);
1009 for (i
= 1; i
< 11; i
++) {
1010 int len
= strlen(number_name
[i
]);
1011 if (match_string(date
, number_name
[i
]) == len
) {
1017 if (match_string(date
, "last") == 4) {
1026 int len
= strlen(tl
->type
);
1027 if (match_string(date
, tl
->type
) >= len
-1) {
1028 update_tm(tm
, now
, tl
->length
* *num
);
1036 for (i
= 0; i
< 7; i
++) {
1037 int match
= match_string(date
, weekday_names
[i
]);
1039 int diff
, n
= *num
-1;
1042 diff
= tm
->tm_wday
- i
;
1047 update_tm(tm
, now
, diff
* 24 * 60 * 60);
1053 if (match_string(date
, "months") >= 5) {
1055 update_tm(tm
, now
, 0); /* fill in date fields if needed */
1056 n
= tm
->tm_mon
- *num
;
1067 if (match_string(date
, "years") >= 4) {
1068 update_tm(tm
, now
, 0); /* fill in date fields if needed */
1069 tm
->tm_year
-= *num
;
1078 static const char *approxidate_digit(const char *date
, struct tm
*tm
, int *num
,
1082 timestamp_t number
= parse_timestamp(date
, &end
, 10);
1089 if (isdigit(end
[1])) {
1090 int match
= match_multi_number(number
, *end
, date
, end
,
1093 return date
+ match
;
1097 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1098 if (date
[0] != '0' || end
- date
<= 2)
1104 * Do we have a pending number at the end, or when
1105 * we see a new one? Let's assume it's a month day,
1106 * as in "Dec 6, 1992"
1108 static void pending_number(struct tm
*tm
, int *num
)
1114 if (tm
->tm_mday
< 0 && number
< 32)
1115 tm
->tm_mday
= number
;
1116 else if (tm
->tm_mon
< 0 && number
< 13)
1117 tm
->tm_mon
= number
-1;
1118 else if (tm
->tm_year
< 0) {
1119 if (number
> 1969 && number
< 2100)
1120 tm
->tm_year
= number
- 1900;
1121 else if (number
> 69 && number
< 100)
1122 tm
->tm_year
= number
;
1123 else if (number
< 38)
1124 tm
->tm_year
= 100 + number
;
1125 /* We screw up for number = 00 ? */
1130 static timestamp_t
approxidate_str(const char *date
,
1131 const struct timeval
*tv
,
1139 time_sec
= tv
->tv_sec
;
1140 localtime_r(&time_sec
, &tm
);
1148 unsigned char c
= *date
;
1153 pending_number(&tm
, &number
);
1154 date
= approxidate_digit(date
-1, &tm
, &number
, time_sec
);
1159 date
= approxidate_alpha(date
-1, &tm
, &now
, &number
, &touched
);
1161 pending_number(&tm
, &number
);
1164 return (timestamp_t
)update_tm(&tm
, &now
, 0);
1167 timestamp_t
approxidate_relative(const char *date
, const struct timeval
*tv
)
1169 timestamp_t timestamp
;
1173 if (!parse_date_basic(date
, ×tamp
, &offset
))
1175 return approxidate_str(date
, tv
, &errors
);
1178 timestamp_t
approxidate_careful(const char *date
, int *error_ret
)
1181 timestamp_t timestamp
;
1187 if (!parse_date_basic(date
, ×tamp
, &offset
)) {
1192 gettimeofday(&tv
, NULL
);
1193 return approxidate_str(date
, &tv
, error_ret
);
1196 int date_overflows(timestamp_t t
)
1200 /* If we overflowed our timestamp data type, that's bad... */
1201 if ((uintmax_t)t
>= TIME_MAX
)
1205 * ...but we also are going to feed the result to system
1206 * functions that expect time_t, which is often "signed long".
1207 * Make sure that we fit into time_t, as well.
1210 return t
!= sys
|| (t
< 1) != (sys
< 1);