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 * What value of "tz" was in effect back then at "time" in the
83 static int local_tzoffset(timestamp_t time
)
89 if (date_overflows(time
))
90 die("Timestamp too large for this system: %"PRItime
, time
);
94 t_local
= tm_to_time_t(&tm
);
97 return 0; /* error; just use +0000 */
100 offset
= t
- t_local
;
103 offset
= t_local
- t
;
105 offset
/= 60; /* in minutes */
106 offset
= (offset
% 60) + ((offset
/ 60) * 100);
107 return offset
* eastwest
;
110 void show_date_relative(timestamp_t time
, int tz
,
111 const struct timeval
*now
,
112 struct strbuf
*timebuf
)
115 if (now
->tv_sec
< time
) {
116 strbuf_addstr(timebuf
, _("in the future"));
119 diff
= now
->tv_sec
- time
;
122 Q_("%"PRItime
" second ago", "%"PRItime
" seconds ago", diff
), diff
);
125 /* Turn it into minutes */
126 diff
= (diff
+ 30) / 60;
129 Q_("%"PRItime
" minute ago", "%"PRItime
" minutes ago", diff
), diff
);
132 /* Turn it into hours */
133 diff
= (diff
+ 30) / 60;
136 Q_("%"PRItime
" hour ago", "%"PRItime
" hours ago", diff
), diff
);
139 /* We deal with number of days from here on */
140 diff
= (diff
+ 12) / 24;
143 Q_("%"PRItime
" day ago", "%"PRItime
" days ago", diff
), diff
);
146 /* Say weeks for the past 10 weeks or so */
149 Q_("%"PRItime
" week ago", "%"PRItime
" weeks ago", (diff
+ 3) / 7),
153 /* Say months for the past 12 months or so */
156 Q_("%"PRItime
" month ago", "%"PRItime
" months ago", (diff
+ 15) / 30),
160 /* Give years and months for 5 years or so */
162 timestamp_t totalmonths
= (diff
* 12 * 2 + 365) / (365 * 2);
163 timestamp_t years
= totalmonths
/ 12;
164 timestamp_t months
= totalmonths
% 12;
166 struct strbuf sb
= STRBUF_INIT
;
167 strbuf_addf(&sb
, Q_("%"PRItime
" year", "%"PRItime
" years", years
), years
);
169 /* TRANSLATORS: "%s" is "<n> years" */
170 Q_("%s, %"PRItime
" month ago", "%s, %"PRItime
" months ago", months
),
175 Q_("%"PRItime
" year ago", "%"PRItime
" years ago", years
), years
);
178 /* Otherwise, just years. Centuries is probably overkill. */
180 Q_("%"PRItime
" year ago", "%"PRItime
" years ago", (diff
+ 183) / 365),
184 struct date_mode
*date_mode_from_type(enum date_mode_type type
)
186 static struct date_mode mode
;
187 if (type
== DATE_STRFTIME
)
188 die("BUG: cannot create anonymous strftime date_mode struct");
194 const char *show_date(timestamp_t time
, int tz
, const struct date_mode
*mode
)
197 static struct strbuf timebuf
= STRBUF_INIT
;
199 if (mode
->type
== DATE_UNIX
) {
200 strbuf_reset(&timebuf
);
201 strbuf_addf(&timebuf
, "%"PRItime
, time
);
206 tz
= local_tzoffset(time
);
208 if (mode
->type
== DATE_RAW
) {
209 strbuf_reset(&timebuf
);
210 strbuf_addf(&timebuf
, "%"PRItime
" %+05d", time
, tz
);
214 if (mode
->type
== DATE_RELATIVE
) {
217 strbuf_reset(&timebuf
);
218 gettimeofday(&now
, NULL
);
219 show_date_relative(time
, tz
, &now
, &timebuf
);
224 tm
= time_to_tm_local(time
);
226 tm
= time_to_tm(time
, tz
);
228 tm
= time_to_tm(0, 0);
232 strbuf_reset(&timebuf
);
233 if (mode
->type
== DATE_SHORT
)
234 strbuf_addf(&timebuf
, "%04d-%02d-%02d", tm
->tm_year
+ 1900,
235 tm
->tm_mon
+ 1, tm
->tm_mday
);
236 else if (mode
->type
== DATE_ISO8601
)
237 strbuf_addf(&timebuf
, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
241 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
243 else if (mode
->type
== DATE_ISO8601_STRICT
) {
244 char sign
= (tz
>= 0) ? '+' : '-';
246 strbuf_addf(&timebuf
, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
250 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
251 sign
, tz
/ 100, tz
% 100);
252 } else if (mode
->type
== DATE_RFC2822
)
253 strbuf_addf(&timebuf
, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
254 weekday_names
[tm
->tm_wday
], tm
->tm_mday
,
255 month_names
[tm
->tm_mon
], tm
->tm_year
+ 1900,
256 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, tz
);
257 else if (mode
->type
== DATE_STRFTIME
)
258 strbuf_addftime(&timebuf
, mode
->strftime_fmt
, tm
, tz
,
261 strbuf_addf(&timebuf
, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
262 weekday_names
[tm
->tm_wday
],
263 month_names
[tm
->tm_mon
],
265 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
267 mode
->local
? 0 : ' ',
273 * Check these. And note how it doesn't do the summer-time conversion.
275 * In my world, it's always summer, and things are probably a bit off
278 static const struct {
282 } timezone_names
[] = {
283 { "IDLW", -12, 0, }, /* International Date Line West */
284 { "NT", -11, 0, }, /* Nome */
285 { "CAT", -10, 0, }, /* Central Alaska */
286 { "HST", -10, 0, }, /* Hawaii Standard */
287 { "HDT", -10, 1, }, /* Hawaii Daylight */
288 { "YST", -9, 0, }, /* Yukon Standard */
289 { "YDT", -9, 1, }, /* Yukon Daylight */
290 { "PST", -8, 0, }, /* Pacific Standard */
291 { "PDT", -8, 1, }, /* Pacific Daylight */
292 { "MST", -7, 0, }, /* Mountain Standard */
293 { "MDT", -7, 1, }, /* Mountain Daylight */
294 { "CST", -6, 0, }, /* Central Standard */
295 { "CDT", -6, 1, }, /* Central Daylight */
296 { "EST", -5, 0, }, /* Eastern Standard */
297 { "EDT", -5, 1, }, /* Eastern Daylight */
298 { "AST", -3, 0, }, /* Atlantic Standard */
299 { "ADT", -3, 1, }, /* Atlantic Daylight */
300 { "WAT", -1, 0, }, /* West Africa */
302 { "GMT", 0, 0, }, /* Greenwich Mean */
303 { "UTC", 0, 0, }, /* Universal (Coordinated) */
304 { "Z", 0, 0, }, /* Zulu, alias for UTC */
306 { "WET", 0, 0, }, /* Western European */
307 { "BST", 0, 1, }, /* British Summer */
308 { "CET", +1, 0, }, /* Central European */
309 { "MET", +1, 0, }, /* Middle European */
310 { "MEWT", +1, 0, }, /* Middle European Winter */
311 { "MEST", +1, 1, }, /* Middle European Summer */
312 { "CEST", +1, 1, }, /* Central European Summer */
313 { "MESZ", +1, 1, }, /* Middle European Summer */
314 { "FWT", +1, 0, }, /* French Winter */
315 { "FST", +1, 1, }, /* French Summer */
316 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
317 { "EEST", +2, 1, }, /* Eastern European Daylight */
318 { "WAST", +7, 0, }, /* West Australian Standard */
319 { "WADT", +7, 1, }, /* West Australian Daylight */
320 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
321 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
322 { "EAST", +10, 0, }, /* Eastern Australian Standard */
323 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
324 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
325 { "NZT", +12, 0, }, /* New Zealand */
326 { "NZST", +12, 0, }, /* New Zealand Standard */
327 { "NZDT", +12, 1, }, /* New Zealand Daylight */
328 { "IDLE", +12, 0, }, /* International Date Line East */
331 static int match_string(const char *date
, const char *str
)
335 for (i
= 0; *date
; date
++, str
++, i
++) {
338 if (toupper(*date
) == toupper(*str
))
347 static int skip_alpha(const char *date
)
352 } while (isalpha(date
[i
]));
357 * Parse month, weekday, or timezone name
359 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
363 for (i
= 0; i
< 12; i
++) {
364 int match
= match_string(date
, month_names
[i
]);
371 for (i
= 0; i
< 7; i
++) {
372 int match
= match_string(date
, weekday_names
[i
]);
379 for (i
= 0; i
< ARRAY_SIZE(timezone_names
); i
++) {
380 int match
= match_string(date
, timezone_names
[i
].name
);
381 if (match
>= 3 || match
== strlen(timezone_names
[i
].name
)) {
382 int off
= timezone_names
[i
].offset
;
384 /* This is bogus, but we like summer */
385 off
+= timezone_names
[i
].dst
;
387 /* Only use the tz name offset if we don't have anything better */
395 if (match_string(date
, "PM") == 2) {
396 tm
->tm_hour
= (tm
->tm_hour
% 12) + 12;
400 if (match_string(date
, "AM") == 2) {
401 tm
->tm_hour
= (tm
->tm_hour
% 12) + 0;
406 return skip_alpha(date
);
409 static int is_date(int year
, int month
, int day
, struct tm
*now_tm
, time_t now
, struct tm
*tm
)
411 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
412 struct tm check
= *tm
;
413 struct tm
*r
= (now_tm
? &check
: tm
);
416 r
->tm_mon
= month
- 1;
421 r
->tm_year
= now_tm
->tm_year
;
423 else if (year
>= 1970 && year
< 2100)
424 r
->tm_year
= year
- 1900;
425 else if (year
> 70 && year
< 100)
428 r
->tm_year
= year
+ 100;
434 specified
= tm_to_time_t(r
);
436 /* Be it commit time or author time, it does not make
437 * sense to specify timestamp way into the future. Make
438 * sure it is not later than ten days from now...
440 if ((specified
!= -1) && (now
+ 10*24*3600 < specified
))
442 tm
->tm_mon
= r
->tm_mon
;
443 tm
->tm_mday
= r
->tm_mday
;
445 tm
->tm_year
= r
->tm_year
;
451 static int match_multi_number(timestamp_t num
, char c
, const char *date
,
452 char *end
, struct tm
*tm
, time_t now
)
455 struct tm
*refuse_future
;
458 num2
= strtol(end
+1, &end
, 10);
460 if (*end
== c
&& isdigit(end
[1]))
461 num3
= strtol(end
+1, &end
, 10);
468 if (num
< 25 && num2
>= 0 && num2
< 60 && num3
>= 0 && num3
<= 60) {
481 refuse_future
= NULL
;
482 if (gmtime_r(&now
, &now_tm
))
483 refuse_future
= &now_tm
;
487 if (is_date(num
, num2
, num3
, NULL
, now
, tm
))
490 if (is_date(num
, num3
, num2
, NULL
, now
, tm
))
493 /* Our eastern European friends say dd.mm.yy[yy]
494 * is the norm there, so giving precedence to
495 * mm/dd/yy[yy] form only when separator is not '.'
498 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
500 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
501 if (is_date(num3
, num2
, num
, refuse_future
, now
, tm
))
503 /* Funny European mm.dd.yy */
505 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
513 * Have we filled in any part of the time/date yet?
514 * We just do a binary 'and' to see if the sign bit
515 * is set in all the values.
517 static inline int nodate(struct tm
*tm
)
519 return (tm
->tm_year
&
528 * We've seen a digit. Time? Year? Date?
530 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
536 num
= parse_timestamp(date
, &end
, 10);
539 * Seconds since 1970? We trigger on that for any numbers with
540 * more than 8 digits. This is because we don't want to rule out
541 * numbers like 20070606 as a YYYYMMDD date.
543 if (num
>= 100000000 && nodate(tm
)) {
545 if (gmtime_r(&time
, tm
)) {
552 * Check for special formats: num[-.:/]num[same]num
559 if (isdigit(end
[1])) {
560 int match
= match_multi_number(num
, *end
, date
, end
, tm
, 0);
567 * None of the special formats? Try to guess what
568 * the number meant. We use the number of digits
569 * to make a more educated guess..
574 } while (isdigit(date
[n
]));
576 /* Four-digit year or a timezone? */
578 if (num
<= 1400 && *offset
== -1) {
579 unsigned int minutes
= num
% 100;
580 unsigned int hours
= num
/ 100;
581 *offset
= hours
*60 + minutes
;
582 } else if (num
> 1900 && num
< 2100)
583 tm
->tm_year
= num
- 1900;
588 * Ignore lots of numerals. We took care of 4-digit years above.
589 * Days or months must be one or two digits.
595 * NOTE! We will give precedence to day-of-month over month or
596 * year numbers in the 1-12 range. So 05 is always "mday 5",
597 * unless we already have a mday..
599 * IOW, 01 Apr 05 parses as "April 1st, 2005".
601 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
606 /* Two-digit year? */
607 if (n
== 2 && tm
->tm_year
< 0) {
608 if (num
< 10 && tm
->tm_mday
>= 0) {
609 tm
->tm_year
= num
+ 100;
618 if (num
> 0 && num
< 13 && tm
->tm_mon
< 0)
624 static int match_tz(const char *date
, int *offp
)
627 int hour
= strtoul(date
+ 1, &end
, 10);
628 int n
= end
- (date
+ 1);
636 min
= 99; /* random crap */
637 } else if (*end
== ':') {
639 min
= strtoul(end
+ 1, &end
, 10);
640 if (end
- (date
+ 1) != 5)
641 min
= 99; /* random crap */
642 } /* otherwise we parsed "hh" */
645 * Don't accept any random crap. Even though some places have
646 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
647 * UTC+14), there is something wrong if hour part is much
648 * larger than that. We might also want to check that the
649 * minutes are divisible by 15 or something too. (Offset of
650 * Kathmandu, Nepal is UTC+5:45)
652 if (min
< 60 && hour
< 24) {
653 int offset
= hour
* 60 + min
;
661 static void date_string(timestamp_t date
, int offset
, struct strbuf
*buf
)
669 strbuf_addf(buf
, "%"PRItime
" %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
673 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
674 * only when it appears not as part of any other string.
676 static int match_object_header_date(const char *date
, timestamp_t
*timestamp
, int *offset
)
682 if (*date
< '0' || '9' < *date
)
684 stamp
= parse_timestamp(date
, &end
, 10);
685 if (*end
!= ' ' || stamp
== TIME_MAX
|| (end
[1] != '+' && end
[1] != '-'))
688 ofs
= strtol(date
, &end
, 10);
689 if ((*end
!= '\0' && (*end
!= '\n')) || end
!= date
+ 4)
691 ofs
= (ofs
/ 100) * 60 + (ofs
% 100);
699 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
700 (i.e. English) day/month names, and it doesn't work correctly with %z. */
701 int parse_date_basic(const char *date
, timestamp_t
*timestamp
, int *offset
)
705 timestamp_t dummy_timestamp
;
709 timestamp
= &dummy_timestamp
;
711 offset
= &dummy_offset
;
713 memset(&tm
, 0, sizeof(tm
));
725 !match_object_header_date(date
+ 1, timestamp
, offset
))
726 return 0; /* success */
729 unsigned char c
= *date
;
731 /* Stop at end of string or newline */
736 match
= match_alpha(date
, &tm
, offset
);
738 match
= match_digit(date
, &tm
, offset
, &tm_gmt
);
739 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
740 match
= match_tz(date
, offset
);
750 /* do not use mktime(), which uses local timezone, here */
751 *timestamp
= tm_to_time_t(&tm
);
752 if (*timestamp
== -1)
758 /* gmtime_r() in match_digit() may have clobbered it */
760 temp_time
= mktime(&tm
);
761 if ((time_t)*timestamp
> temp_time
) {
762 *offset
= ((time_t)*timestamp
- temp_time
) / 60;
764 *offset
= -(int)((temp_time
- (time_t)*timestamp
) / 60);
769 *timestamp
-= *offset
* 60;
770 return 0; /* success */
773 int parse_expiry_date(const char *date
, timestamp_t
*timestamp
)
777 if (!strcmp(date
, "never") || !strcmp(date
, "false"))
779 else if (!strcmp(date
, "all") || !strcmp(date
, "now"))
781 * We take over "now" here, which usually translates
782 * to the current timestamp. This is because the user
783 * really means to expire everything she has done in
784 * the past, and by definition reflogs are the record
785 * of the past, and there is nothing from the future
788 *timestamp
= TIME_MAX
;
790 *timestamp
= approxidate_careful(date
, &errors
);
795 int parse_date(const char *date
, struct strbuf
*result
)
797 timestamp_t timestamp
;
799 if (parse_date_basic(date
, ×tamp
, &offset
))
801 date_string(timestamp
, offset
, result
);
805 static enum date_mode_type
parse_date_type(const char *format
, const char **end
)
807 if (skip_prefix(format
, "relative", end
))
808 return DATE_RELATIVE
;
809 if (skip_prefix(format
, "iso8601-strict", end
) ||
810 skip_prefix(format
, "iso-strict", end
))
811 return DATE_ISO8601_STRICT
;
812 if (skip_prefix(format
, "iso8601", end
) ||
813 skip_prefix(format
, "iso", end
))
815 if (skip_prefix(format
, "rfc2822", end
) ||
816 skip_prefix(format
, "rfc", end
))
818 if (skip_prefix(format
, "short", end
))
820 if (skip_prefix(format
, "default", end
))
822 if (skip_prefix(format
, "raw", end
))
824 if (skip_prefix(format
, "unix", end
))
826 if (skip_prefix(format
, "format", end
))
827 return DATE_STRFTIME
;
829 die("unknown date format %s", format
);
832 void parse_date_format(const char *format
, struct date_mode
*mode
)
836 /* historical alias */
837 if (!strcmp(format
, "local"))
838 format
= "default-local";
840 mode
->type
= parse_date_type(format
, &p
);
843 if (skip_prefix(p
, "-local", &p
))
846 if (mode
->type
== DATE_STRFTIME
) {
847 if (!skip_prefix(p
, ":", &p
))
848 die("date format missing colon separator: %s", format
);
849 mode
->strftime_fmt
= xstrdup(p
);
851 die("unknown date format %s", format
);
854 void datestamp(struct strbuf
*out
)
861 offset
= tm_to_time_t(localtime(&now
)) - now
;
864 date_string(now
, offset
, out
);
868 * Relative time update (eg "2 days ago"). If we haven't set the time
869 * yet, we need to set it from current time.
871 static time_t update_tm(struct tm
*tm
, struct tm
*now
, time_t sec
)
876 tm
->tm_mday
= now
->tm_mday
;
878 tm
->tm_mon
= now
->tm_mon
;
879 if (tm
->tm_year
< 0) {
880 tm
->tm_year
= now
->tm_year
;
881 if (tm
->tm_mon
> now
->tm_mon
)
885 n
= mktime(tm
) - sec
;
890 static void date_now(struct tm
*tm
, struct tm
*now
, int *num
)
892 update_tm(tm
, now
, 0);
895 static void date_yesterday(struct tm
*tm
, struct tm
*now
, int *num
)
897 update_tm(tm
, now
, 24*60*60);
900 static void date_time(struct tm
*tm
, struct tm
*now
, int hour
)
902 if (tm
->tm_hour
< hour
)
903 date_yesterday(tm
, now
, NULL
);
909 static void date_midnight(struct tm
*tm
, struct tm
*now
, int *num
)
911 date_time(tm
, now
, 0);
914 static void date_noon(struct tm
*tm
, struct tm
*now
, int *num
)
916 date_time(tm
, now
, 12);
919 static void date_tea(struct tm
*tm
, struct tm
*now
, int *num
)
921 date_time(tm
, now
, 17);
924 static void date_pm(struct tm
*tm
, struct tm
*now
, int *num
)
935 tm
->tm_hour
= (hour
% 12) + 12;
938 static void date_am(struct tm
*tm
, struct tm
*now
, int *num
)
949 tm
->tm_hour
= (hour
% 12);
952 static void date_never(struct tm
*tm
, struct tm
*now
, int *num
)
958 static const struct special
{
960 void (*fn
)(struct tm
*, struct tm
*, int *);
962 { "yesterday", date_yesterday
},
963 { "noon", date_noon
},
964 { "midnight", date_midnight
},
968 { "never", date_never
},
973 static const char *number_name
[] = {
974 "zero", "one", "two", "three", "four",
975 "five", "six", "seven", "eight", "nine", "ten",
978 static const struct typelen
{
985 { "days", 24*60*60 },
986 { "weeks", 7*24*60*60 },
990 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, struct tm
*now
, int *num
, int *touched
)
992 const struct typelen
*tl
;
993 const struct special
*s
;
994 const char *end
= date
;
997 while (isalpha(*++end
))
1000 for (i
= 0; i
< 12; i
++) {
1001 int match
= match_string(date
, month_names
[i
]);
1009 for (s
= special
; s
->name
; s
++) {
1010 int len
= strlen(s
->name
);
1011 if (match_string(date
, s
->name
) == len
) {
1012 s
->fn(tm
, now
, num
);
1019 for (i
= 1; i
< 11; i
++) {
1020 int len
= strlen(number_name
[i
]);
1021 if (match_string(date
, number_name
[i
]) == len
) {
1027 if (match_string(date
, "last") == 4) {
1036 int len
= strlen(tl
->type
);
1037 if (match_string(date
, tl
->type
) >= len
-1) {
1038 update_tm(tm
, now
, tl
->length
* *num
);
1046 for (i
= 0; i
< 7; i
++) {
1047 int match
= match_string(date
, weekday_names
[i
]);
1049 int diff
, n
= *num
-1;
1052 diff
= tm
->tm_wday
- i
;
1057 update_tm(tm
, now
, diff
* 24 * 60 * 60);
1063 if (match_string(date
, "months") >= 5) {
1065 update_tm(tm
, now
, 0); /* fill in date fields if needed */
1066 n
= tm
->tm_mon
- *num
;
1077 if (match_string(date
, "years") >= 4) {
1078 update_tm(tm
, now
, 0); /* fill in date fields if needed */
1079 tm
->tm_year
-= *num
;
1088 static const char *approxidate_digit(const char *date
, struct tm
*tm
, int *num
,
1092 timestamp_t number
= parse_timestamp(date
, &end
, 10);
1099 if (isdigit(end
[1])) {
1100 int match
= match_multi_number(number
, *end
, date
, end
,
1103 return date
+ match
;
1107 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1108 if (date
[0] != '0' || end
- date
<= 2)
1114 * Do we have a pending number at the end, or when
1115 * we see a new one? Let's assume it's a month day,
1116 * as in "Dec 6, 1992"
1118 static void pending_number(struct tm
*tm
, int *num
)
1124 if (tm
->tm_mday
< 0 && number
< 32)
1125 tm
->tm_mday
= number
;
1126 else if (tm
->tm_mon
< 0 && number
< 13)
1127 tm
->tm_mon
= number
-1;
1128 else if (tm
->tm_year
< 0) {
1129 if (number
> 1969 && number
< 2100)
1130 tm
->tm_year
= number
- 1900;
1131 else if (number
> 69 && number
< 100)
1132 tm
->tm_year
= number
;
1133 else if (number
< 38)
1134 tm
->tm_year
= 100 + number
;
1135 /* We screw up for number = 00 ? */
1140 static timestamp_t
approxidate_str(const char *date
,
1141 const struct timeval
*tv
,
1149 time_sec
= tv
->tv_sec
;
1150 localtime_r(&time_sec
, &tm
);
1158 unsigned char c
= *date
;
1163 pending_number(&tm
, &number
);
1164 date
= approxidate_digit(date
-1, &tm
, &number
, time_sec
);
1169 date
= approxidate_alpha(date
-1, &tm
, &now
, &number
, &touched
);
1171 pending_number(&tm
, &number
);
1174 return (timestamp_t
)update_tm(&tm
, &now
, 0);
1177 timestamp_t
approxidate_relative(const char *date
, const struct timeval
*tv
)
1179 timestamp_t timestamp
;
1183 if (!parse_date_basic(date
, ×tamp
, &offset
))
1185 return approxidate_str(date
, tv
, &errors
);
1188 timestamp_t
approxidate_careful(const char *date
, int *error_ret
)
1191 timestamp_t timestamp
;
1197 if (!parse_date_basic(date
, ×tamp
, &offset
)) {
1202 gettimeofday(&tv
, NULL
);
1203 return approxidate_str(date
, &tv
, error_ret
);
1206 int date_overflows(timestamp_t t
)
1210 /* If we overflowed our timestamp data type, that's bad... */
1211 if ((uintmax_t)t
>= TIME_MAX
)
1215 * ...but we also are going to feed the result to system
1216 * functions that expect time_t, which is often "signed long".
1217 * Make sure that we fit into time_t, as well.
1220 return t
!= sys
|| (t
< 1) != (sys
< 1);