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(unsigned long time
, int tz
)
46 minutes
= tz
< 0 ? -tz
: tz
;
47 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
48 minutes
= tz
< 0 ? -minutes
: minutes
;
49 return time
+ minutes
* 60;
53 * The "tz" thing is passed in as this strange "decimal parse of tz"
54 * thing, which means that tz -0100 is passed in as the integer -100,
55 * even though it means "sixty minutes off"
57 static struct tm
*time_to_tm(unsigned long time
, int tz
)
59 time_t t
= gm_time_t(time
, tz
);
64 * What value of "tz" was in effect back then at "time" in the
67 static int local_tzoffset(unsigned long time
)
75 t_local
= tm_to_time_t(&tm
);
78 return 0; /* error; just use +0000 */
86 offset
/= 60; /* in minutes */
87 offset
= (offset
% 60) + ((offset
/ 60) * 100);
88 return offset
* eastwest
;
91 void show_date_relative(unsigned long time
, int tz
,
92 const struct timeval
*now
,
93 struct strbuf
*timebuf
)
96 if (now
->tv_sec
< time
) {
97 strbuf_addstr(timebuf
, _("in the future"));
100 diff
= now
->tv_sec
- time
;
103 Q_("%lu second ago", "%lu seconds ago", diff
), diff
);
106 /* Turn it into minutes */
107 diff
= (diff
+ 30) / 60;
110 Q_("%lu minute ago", "%lu minutes ago", diff
), diff
);
113 /* Turn it into hours */
114 diff
= (diff
+ 30) / 60;
117 Q_("%lu hour ago", "%lu hours ago", diff
), diff
);
120 /* We deal with number of days from here on */
121 diff
= (diff
+ 12) / 24;
124 Q_("%lu day ago", "%lu days ago", diff
), diff
);
127 /* Say weeks for the past 10 weeks or so */
130 Q_("%lu week ago", "%lu weeks ago", (diff
+ 3) / 7),
134 /* Say months for the past 12 months or so */
137 Q_("%lu month ago", "%lu months ago", (diff
+ 15) / 30),
141 /* Give years and months for 5 years or so */
143 unsigned long totalmonths
= (diff
* 12 * 2 + 365) / (365 * 2);
144 unsigned long years
= totalmonths
/ 12;
145 unsigned long months
= totalmonths
% 12;
147 struct strbuf sb
= STRBUF_INIT
;
148 strbuf_addf(&sb
, Q_("%lu year", "%lu years", years
), years
);
150 /* TRANSLATORS: "%s" is "<n> years" */
151 Q_("%s, %lu month ago", "%s, %lu months ago", months
),
156 Q_("%lu year ago", "%lu years ago", years
), years
);
159 /* Otherwise, just years. Centuries is probably overkill. */
161 Q_("%lu year ago", "%lu years ago", (diff
+ 183) / 365),
165 struct date_mode
*date_mode_from_type(enum date_mode_type type
)
167 static struct date_mode mode
;
168 if (type
== DATE_STRFTIME
)
169 die("BUG: cannot create anonymous strftime date_mode struct");
175 const char *show_date(unsigned long time
, int tz
, const struct date_mode
*mode
)
178 static struct strbuf timebuf
= STRBUF_INIT
;
180 if (mode
->type
== DATE_UNIX
) {
181 strbuf_reset(&timebuf
);
182 strbuf_addf(&timebuf
, "%lu", time
);
187 tz
= local_tzoffset(time
);
189 if (mode
->type
== DATE_RAW
) {
190 strbuf_reset(&timebuf
);
191 strbuf_addf(&timebuf
, "%lu %+05d", time
, tz
);
195 if (mode
->type
== DATE_RELATIVE
) {
198 strbuf_reset(&timebuf
);
199 gettimeofday(&now
, NULL
);
200 show_date_relative(time
, tz
, &now
, &timebuf
);
204 tm
= time_to_tm(time
, tz
);
206 tm
= time_to_tm(0, 0);
210 strbuf_reset(&timebuf
);
211 if (mode
->type
== DATE_SHORT
)
212 strbuf_addf(&timebuf
, "%04d-%02d-%02d", tm
->tm_year
+ 1900,
213 tm
->tm_mon
+ 1, tm
->tm_mday
);
214 else if (mode
->type
== DATE_ISO8601
)
215 strbuf_addf(&timebuf
, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
219 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
221 else if (mode
->type
== DATE_ISO8601_STRICT
) {
222 char sign
= (tz
>= 0) ? '+' : '-';
224 strbuf_addf(&timebuf
, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
228 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
229 sign
, tz
/ 100, tz
% 100);
230 } else if (mode
->type
== DATE_RFC2822
)
231 strbuf_addf(&timebuf
, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
232 weekday_names
[tm
->tm_wday
], tm
->tm_mday
,
233 month_names
[tm
->tm_mon
], tm
->tm_year
+ 1900,
234 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, tz
);
235 else if (mode
->type
== DATE_STRFTIME
)
236 strbuf_addftime(&timebuf
, mode
->strftime_fmt
, tm
, tz
, "");
238 strbuf_addf(&timebuf
, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
239 weekday_names
[tm
->tm_wday
],
240 month_names
[tm
->tm_mon
],
242 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
244 mode
->local
? 0 : ' ',
250 * Check these. And note how it doesn't do the summer-time conversion.
252 * In my world, it's always summer, and things are probably a bit off
255 static const struct {
259 } timezone_names
[] = {
260 { "IDLW", -12, 0, }, /* International Date Line West */
261 { "NT", -11, 0, }, /* Nome */
262 { "CAT", -10, 0, }, /* Central Alaska */
263 { "HST", -10, 0, }, /* Hawaii Standard */
264 { "HDT", -10, 1, }, /* Hawaii Daylight */
265 { "YST", -9, 0, }, /* Yukon Standard */
266 { "YDT", -9, 1, }, /* Yukon Daylight */
267 { "PST", -8, 0, }, /* Pacific Standard */
268 { "PDT", -8, 1, }, /* Pacific Daylight */
269 { "MST", -7, 0, }, /* Mountain Standard */
270 { "MDT", -7, 1, }, /* Mountain Daylight */
271 { "CST", -6, 0, }, /* Central Standard */
272 { "CDT", -6, 1, }, /* Central Daylight */
273 { "EST", -5, 0, }, /* Eastern Standard */
274 { "EDT", -5, 1, }, /* Eastern Daylight */
275 { "AST", -3, 0, }, /* Atlantic Standard */
276 { "ADT", -3, 1, }, /* Atlantic Daylight */
277 { "WAT", -1, 0, }, /* West Africa */
279 { "GMT", 0, 0, }, /* Greenwich Mean */
280 { "UTC", 0, 0, }, /* Universal (Coordinated) */
281 { "Z", 0, 0, }, /* Zulu, alias for UTC */
283 { "WET", 0, 0, }, /* Western European */
284 { "BST", 0, 1, }, /* British Summer */
285 { "CET", +1, 0, }, /* Central European */
286 { "MET", +1, 0, }, /* Middle European */
287 { "MEWT", +1, 0, }, /* Middle European Winter */
288 { "MEST", +1, 1, }, /* Middle European Summer */
289 { "CEST", +1, 1, }, /* Central European Summer */
290 { "MESZ", +1, 1, }, /* Middle European Summer */
291 { "FWT", +1, 0, }, /* French Winter */
292 { "FST", +1, 1, }, /* French Summer */
293 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
294 { "EEST", +2, 1, }, /* Eastern European Daylight */
295 { "WAST", +7, 0, }, /* West Australian Standard */
296 { "WADT", +7, 1, }, /* West Australian Daylight */
297 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
298 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
299 { "EAST", +10, 0, }, /* Eastern Australian Standard */
300 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
301 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
302 { "NZT", +12, 0, }, /* New Zealand */
303 { "NZST", +12, 0, }, /* New Zealand Standard */
304 { "NZDT", +12, 1, }, /* New Zealand Daylight */
305 { "IDLE", +12, 0, }, /* International Date Line East */
308 static int match_string(const char *date
, const char *str
)
312 for (i
= 0; *date
; date
++, str
++, i
++) {
315 if (toupper(*date
) == toupper(*str
))
324 static int skip_alpha(const char *date
)
329 } while (isalpha(date
[i
]));
334 * Parse month, weekday, or timezone name
336 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
340 for (i
= 0; i
< 12; i
++) {
341 int match
= match_string(date
, month_names
[i
]);
348 for (i
= 0; i
< 7; i
++) {
349 int match
= match_string(date
, weekday_names
[i
]);
356 for (i
= 0; i
< ARRAY_SIZE(timezone_names
); i
++) {
357 int match
= match_string(date
, timezone_names
[i
].name
);
358 if (match
>= 3 || match
== strlen(timezone_names
[i
].name
)) {
359 int off
= timezone_names
[i
].offset
;
361 /* This is bogus, but we like summer */
362 off
+= timezone_names
[i
].dst
;
364 /* Only use the tz name offset if we don't have anything better */
372 if (match_string(date
, "PM") == 2) {
373 tm
->tm_hour
= (tm
->tm_hour
% 12) + 12;
377 if (match_string(date
, "AM") == 2) {
378 tm
->tm_hour
= (tm
->tm_hour
% 12) + 0;
383 return skip_alpha(date
);
386 static int is_date(int year
, int month
, int day
, struct tm
*now_tm
, time_t now
, struct tm
*tm
)
388 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
389 struct tm check
= *tm
;
390 struct tm
*r
= (now_tm
? &check
: tm
);
393 r
->tm_mon
= month
- 1;
398 r
->tm_year
= now_tm
->tm_year
;
400 else if (year
>= 1970 && year
< 2100)
401 r
->tm_year
= year
- 1900;
402 else if (year
> 70 && year
< 100)
405 r
->tm_year
= year
+ 100;
411 specified
= tm_to_time_t(r
);
413 /* Be it commit time or author time, it does not make
414 * sense to specify timestamp way into the future. Make
415 * sure it is not later than ten days from now...
417 if ((specified
!= -1) && (now
+ 10*24*3600 < specified
))
419 tm
->tm_mon
= r
->tm_mon
;
420 tm
->tm_mday
= r
->tm_mday
;
422 tm
->tm_year
= r
->tm_year
;
428 static int match_multi_number(unsigned long num
, char c
, const char *date
,
429 char *end
, struct tm
*tm
, time_t now
)
432 struct tm
*refuse_future
;
435 num2
= strtol(end
+1, &end
, 10);
437 if (*end
== c
&& isdigit(end
[1]))
438 num3
= strtol(end
+1, &end
, 10);
445 if (num
< 25 && num2
>= 0 && num2
< 60 && num3
>= 0 && num3
<= 60) {
458 refuse_future
= NULL
;
459 if (gmtime_r(&now
, &now_tm
))
460 refuse_future
= &now_tm
;
464 if (is_date(num
, num2
, num3
, NULL
, now
, tm
))
467 if (is_date(num
, num3
, num2
, NULL
, now
, tm
))
470 /* Our eastern European friends say dd.mm.yy[yy]
471 * is the norm there, so giving precedence to
472 * mm/dd/yy[yy] form only when separator is not '.'
475 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
477 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
478 if (is_date(num3
, num2
, num
, refuse_future
, now
, tm
))
480 /* Funny European mm.dd.yy */
482 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
490 * Have we filled in any part of the time/date yet?
491 * We just do a binary 'and' to see if the sign bit
492 * is set in all the values.
494 static inline int nodate(struct tm
*tm
)
496 return (tm
->tm_year
&
505 * We've seen a digit. Time? Year? Date?
507 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
513 num
= strtoul(date
, &end
, 10);
516 * Seconds since 1970? We trigger on that for any numbers with
517 * more than 8 digits. This is because we don't want to rule out
518 * numbers like 20070606 as a YYYYMMDD date.
520 if (num
>= 100000000 && nodate(tm
)) {
522 if (gmtime_r(&time
, tm
)) {
529 * Check for special formats: num[-.:/]num[same]num
536 if (isdigit(end
[1])) {
537 int match
= match_multi_number(num
, *end
, date
, end
, tm
, 0);
544 * None of the special formats? Try to guess what
545 * the number meant. We use the number of digits
546 * to make a more educated guess..
551 } while (isdigit(date
[n
]));
553 /* Four-digit year or a timezone? */
555 if (num
<= 1400 && *offset
== -1) {
556 unsigned int minutes
= num
% 100;
557 unsigned int hours
= num
/ 100;
558 *offset
= hours
*60 + minutes
;
559 } else if (num
> 1900 && num
< 2100)
560 tm
->tm_year
= num
- 1900;
565 * Ignore lots of numerals. We took care of 4-digit years above.
566 * Days or months must be one or two digits.
572 * NOTE! We will give precedence to day-of-month over month or
573 * year numbers in the 1-12 range. So 05 is always "mday 5",
574 * unless we already have a mday..
576 * IOW, 01 Apr 05 parses as "April 1st, 2005".
578 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
583 /* Two-digit year? */
584 if (n
== 2 && tm
->tm_year
< 0) {
585 if (num
< 10 && tm
->tm_mday
>= 0) {
586 tm
->tm_year
= num
+ 100;
595 if (num
> 0 && num
< 13 && tm
->tm_mon
< 0)
601 static int match_tz(const char *date
, int *offp
)
604 int hour
= strtoul(date
+ 1, &end
, 10);
605 int n
= end
- (date
+ 1);
613 min
= 99; /* random crap */
614 } else if (*end
== ':') {
616 min
= strtoul(end
+ 1, &end
, 10);
617 if (end
- (date
+ 1) != 5)
618 min
= 99; /* random crap */
619 } /* otherwise we parsed "hh" */
622 * Don't accept any random crap. Even though some places have
623 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
624 * UTC+14), there is something wrong if hour part is much
625 * larger than that. We might also want to check that the
626 * minutes are divisible by 15 or something too. (Offset of
627 * Kathmandu, Nepal is UTC+5:45)
629 if (min
< 60 && hour
< 24) {
630 int offset
= hour
* 60 + min
;
638 static void date_string(unsigned long date
, int offset
, struct strbuf
*buf
)
646 strbuf_addf(buf
, "%lu %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
650 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
651 * only when it appears not as part of any other string.
653 static int match_object_header_date(const char *date
, unsigned long *timestamp
, int *offset
)
659 if (*date
< '0' || '9' < *date
)
661 stamp
= strtoul(date
, &end
, 10);
662 if (*end
!= ' ' || stamp
== ULONG_MAX
|| (end
[1] != '+' && end
[1] != '-'))
665 ofs
= strtol(date
, &end
, 10);
666 if ((*end
!= '\0' && (*end
!= '\n')) || end
!= date
+ 4)
668 ofs
= (ofs
/ 100) * 60 + (ofs
% 100);
676 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
677 (i.e. English) day/month names, and it doesn't work correctly with %z. */
678 int parse_date_basic(const char *date
, unsigned long *timestamp
, int *offset
)
682 unsigned long dummy_timestamp
;
686 timestamp
= &dummy_timestamp
;
688 offset
= &dummy_offset
;
690 memset(&tm
, 0, sizeof(tm
));
702 !match_object_header_date(date
+ 1, timestamp
, offset
))
703 return 0; /* success */
706 unsigned char c
= *date
;
708 /* Stop at end of string or newline */
713 match
= match_alpha(date
, &tm
, offset
);
715 match
= match_digit(date
, &tm
, offset
, &tm_gmt
);
716 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
717 match
= match_tz(date
, offset
);
727 /* do not use mktime(), which uses local timezone, here */
728 *timestamp
= tm_to_time_t(&tm
);
729 if (*timestamp
== -1)
735 /* gmtime_r() in match_digit() may have clobbered it */
737 temp_time
= mktime(&tm
);
738 if ((time_t)*timestamp
> temp_time
) {
739 *offset
= ((time_t)*timestamp
- temp_time
) / 60;
741 *offset
= -(int)((temp_time
- (time_t)*timestamp
) / 60);
746 *timestamp
-= *offset
* 60;
747 return 0; /* success */
750 int parse_expiry_date(const char *date
, unsigned long *timestamp
)
754 if (!strcmp(date
, "never") || !strcmp(date
, "false"))
756 else if (!strcmp(date
, "all") || !strcmp(date
, "now"))
758 * We take over "now" here, which usually translates
759 * to the current timestamp. This is because the user
760 * really means to expire everything she has done in
761 * the past, and by definition reflogs are the record
762 * of the past, and there is nothing from the future
765 *timestamp
= ULONG_MAX
;
767 *timestamp
= approxidate_careful(date
, &errors
);
772 int parse_date(const char *date
, struct strbuf
*result
)
774 unsigned long timestamp
;
776 if (parse_date_basic(date
, ×tamp
, &offset
))
778 date_string(timestamp
, offset
, result
);
782 static enum date_mode_type
parse_date_type(const char *format
, const char **end
)
784 if (skip_prefix(format
, "relative", end
))
785 return DATE_RELATIVE
;
786 if (skip_prefix(format
, "iso8601-strict", end
) ||
787 skip_prefix(format
, "iso-strict", end
))
788 return DATE_ISO8601_STRICT
;
789 if (skip_prefix(format
, "iso8601", end
) ||
790 skip_prefix(format
, "iso", end
))
792 if (skip_prefix(format
, "rfc2822", end
) ||
793 skip_prefix(format
, "rfc", end
))
795 if (skip_prefix(format
, "short", end
))
797 if (skip_prefix(format
, "default", end
))
799 if (skip_prefix(format
, "raw", end
))
801 if (skip_prefix(format
, "unix", end
))
803 if (skip_prefix(format
, "format", end
))
804 return DATE_STRFTIME
;
806 die("unknown date format %s", format
);
809 void parse_date_format(const char *format
, struct date_mode
*mode
)
813 /* historical alias */
814 if (!strcmp(format
, "local"))
815 format
= "default-local";
817 mode
->type
= parse_date_type(format
, &p
);
820 if (skip_prefix(p
, "-local", &p
))
823 if (mode
->type
== DATE_STRFTIME
) {
824 if (!skip_prefix(p
, ":", &p
))
825 die("date format missing colon separator: %s", format
);
826 mode
->strftime_fmt
= xstrdup(p
);
828 die("unknown date format %s", format
);
831 void datestamp(struct strbuf
*out
)
838 offset
= tm_to_time_t(localtime(&now
)) - now
;
841 date_string(now
, offset
, out
);
845 * Relative time update (eg "2 days ago"). If we haven't set the time
846 * yet, we need to set it from current time.
848 static unsigned long update_tm(struct tm
*tm
, struct tm
*now
, unsigned long sec
)
853 tm
->tm_mday
= now
->tm_mday
;
855 tm
->tm_mon
= now
->tm_mon
;
856 if (tm
->tm_year
< 0) {
857 tm
->tm_year
= now
->tm_year
;
858 if (tm
->tm_mon
> now
->tm_mon
)
862 n
= mktime(tm
) - sec
;
867 static void date_now(struct tm
*tm
, struct tm
*now
, int *num
)
869 update_tm(tm
, now
, 0);
872 static void date_yesterday(struct tm
*tm
, struct tm
*now
, int *num
)
874 update_tm(tm
, now
, 24*60*60);
877 static void date_time(struct tm
*tm
, struct tm
*now
, int hour
)
879 if (tm
->tm_hour
< hour
)
880 date_yesterday(tm
, now
, NULL
);
886 static void date_midnight(struct tm
*tm
, struct tm
*now
, int *num
)
888 date_time(tm
, now
, 0);
891 static void date_noon(struct tm
*tm
, struct tm
*now
, int *num
)
893 date_time(tm
, now
, 12);
896 static void date_tea(struct tm
*tm
, struct tm
*now
, int *num
)
898 date_time(tm
, now
, 17);
901 static void date_pm(struct tm
*tm
, struct tm
*now
, int *num
)
912 tm
->tm_hour
= (hour
% 12) + 12;
915 static void date_am(struct tm
*tm
, struct tm
*now
, int *num
)
926 tm
->tm_hour
= (hour
% 12);
929 static void date_never(struct tm
*tm
, struct tm
*now
, int *num
)
935 static const struct special
{
937 void (*fn
)(struct tm
*, struct tm
*, int *);
939 { "yesterday", date_yesterday
},
940 { "noon", date_noon
},
941 { "midnight", date_midnight
},
945 { "never", date_never
},
950 static const char *number_name
[] = {
951 "zero", "one", "two", "three", "four",
952 "five", "six", "seven", "eight", "nine", "ten",
955 static const struct typelen
{
962 { "days", 24*60*60 },
963 { "weeks", 7*24*60*60 },
967 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, struct tm
*now
, int *num
, int *touched
)
969 const struct typelen
*tl
;
970 const struct special
*s
;
971 const char *end
= date
;
974 while (isalpha(*++end
))
977 for (i
= 0; i
< 12; i
++) {
978 int match
= match_string(date
, month_names
[i
]);
986 for (s
= special
; s
->name
; s
++) {
987 int len
= strlen(s
->name
);
988 if (match_string(date
, s
->name
) == len
) {
996 for (i
= 1; i
< 11; i
++) {
997 int len
= strlen(number_name
[i
]);
998 if (match_string(date
, number_name
[i
]) == len
) {
1004 if (match_string(date
, "last") == 4) {
1013 int len
= strlen(tl
->type
);
1014 if (match_string(date
, tl
->type
) >= len
-1) {
1015 update_tm(tm
, now
, tl
->length
* *num
);
1023 for (i
= 0; i
< 7; i
++) {
1024 int match
= match_string(date
, weekday_names
[i
]);
1026 int diff
, n
= *num
-1;
1029 diff
= tm
->tm_wday
- i
;
1034 update_tm(tm
, now
, diff
* 24 * 60 * 60);
1040 if (match_string(date
, "months") >= 5) {
1042 update_tm(tm
, now
, 0); /* fill in date fields if needed */
1043 n
= tm
->tm_mon
- *num
;
1054 if (match_string(date
, "years") >= 4) {
1055 update_tm(tm
, now
, 0); /* fill in date fields if needed */
1056 tm
->tm_year
-= *num
;
1065 static const char *approxidate_digit(const char *date
, struct tm
*tm
, int *num
,
1069 unsigned long number
= strtoul(date
, &end
, 10);
1076 if (isdigit(end
[1])) {
1077 int match
= match_multi_number(number
, *end
, date
, end
,
1080 return date
+ match
;
1084 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1085 if (date
[0] != '0' || end
- date
<= 2)
1091 * Do we have a pending number at the end, or when
1092 * we see a new one? Let's assume it's a month day,
1093 * as in "Dec 6, 1992"
1095 static void pending_number(struct tm
*tm
, int *num
)
1101 if (tm
->tm_mday
< 0 && number
< 32)
1102 tm
->tm_mday
= number
;
1103 else if (tm
->tm_mon
< 0 && number
< 13)
1104 tm
->tm_mon
= number
-1;
1105 else if (tm
->tm_year
< 0) {
1106 if (number
> 1969 && number
< 2100)
1107 tm
->tm_year
= number
- 1900;
1108 else if (number
> 69 && number
< 100)
1109 tm
->tm_year
= number
;
1110 else if (number
< 38)
1111 tm
->tm_year
= 100 + number
;
1112 /* We screw up for number = 00 ? */
1117 static unsigned long approxidate_str(const char *date
,
1118 const struct timeval
*tv
,
1126 time_sec
= tv
->tv_sec
;
1127 localtime_r(&time_sec
, &tm
);
1135 unsigned char c
= *date
;
1140 pending_number(&tm
, &number
);
1141 date
= approxidate_digit(date
-1, &tm
, &number
, time_sec
);
1146 date
= approxidate_alpha(date
-1, &tm
, &now
, &number
, &touched
);
1148 pending_number(&tm
, &number
);
1151 return update_tm(&tm
, &now
, 0);
1154 unsigned long approxidate_relative(const char *date
, const struct timeval
*tv
)
1156 unsigned long timestamp
;
1160 if (!parse_date_basic(date
, ×tamp
, &offset
))
1162 return approxidate_str(date
, tv
, &errors
);
1165 unsigned long approxidate_careful(const char *date
, int *error_ret
)
1168 unsigned long timestamp
;
1174 if (!parse_date_basic(date
, ×tamp
, &offset
)) {
1179 gettimeofday(&tv
, NULL
);
1180 return approxidate_str(date
, &tv
, error_ret
);
1183 int date_overflows(unsigned long t
)
1187 /* If we overflowed our unsigned long, that's bad... */
1192 * ...but we also are going to feed the result to system
1193 * functions that expect time_t, which is often "signed long".
1194 * Make sure that we fit into time_t, as well.
1197 return t
!= sys
|| (t
< 1) != (sys
< 1);