]>
git.ipfire.org Git - thirdparty/git.git/blob - date.c
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
);
84 offset
/= 60; /* in minutes */
85 offset
= (offset
% 60) + ((offset
/ 60) * 100);
86 return offset
* eastwest
;
89 void show_date_relative(unsigned long time
, int tz
,
90 const struct timeval
*now
,
91 struct strbuf
*timebuf
)
94 if (now
->tv_sec
< time
) {
95 strbuf_addstr(timebuf
, _("in the future"));
98 diff
= now
->tv_sec
- time
;
101 Q_("%lu second ago", "%lu seconds ago", diff
), diff
);
104 /* Turn it into minutes */
105 diff
= (diff
+ 30) / 60;
108 Q_("%lu minute ago", "%lu minutes ago", diff
), diff
);
111 /* Turn it into hours */
112 diff
= (diff
+ 30) / 60;
115 Q_("%lu hour ago", "%lu hours ago", diff
), diff
);
118 /* We deal with number of days from here on */
119 diff
= (diff
+ 12) / 24;
122 Q_("%lu day ago", "%lu days ago", diff
), diff
);
125 /* Say weeks for the past 10 weeks or so */
128 Q_("%lu week ago", "%lu weeks ago", (diff
+ 3) / 7),
132 /* Say months for the past 12 months or so */
135 Q_("%lu month ago", "%lu months ago", (diff
+ 15) / 30),
139 /* Give years and months for 5 years or so */
141 unsigned long totalmonths
= (diff
* 12 * 2 + 365) / (365 * 2);
142 unsigned long years
= totalmonths
/ 12;
143 unsigned long months
= totalmonths
% 12;
145 struct strbuf sb
= STRBUF_INIT
;
146 strbuf_addf(&sb
, Q_("%lu year", "%lu years", years
), years
);
148 /* TRANSLATORS: "%s" is "<n> years" */
149 Q_("%s, %lu month ago", "%s, %lu months ago", months
),
154 Q_("%lu year ago", "%lu years ago", years
), years
);
157 /* Otherwise, just years. Centuries is probably overkill. */
159 Q_("%lu year ago", "%lu years ago", (diff
+ 183) / 365),
163 struct date_mode
*date_mode_from_type(enum date_mode_type type
)
165 static struct date_mode mode
;
170 const char *show_date(unsigned long time
, int tz
, const struct date_mode
*mode
)
173 static struct strbuf timebuf
= STRBUF_INIT
;
175 if (mode
->type
== DATE_RAW
) {
176 strbuf_reset(&timebuf
);
177 strbuf_addf(&timebuf
, "%lu %+05d", time
, tz
);
181 if (mode
->type
== DATE_RELATIVE
) {
184 strbuf_reset(&timebuf
);
185 gettimeofday(&now
, NULL
);
186 show_date_relative(time
, tz
, &now
, &timebuf
);
190 if (mode
->type
== DATE_LOCAL
)
191 tz
= local_tzoffset(time
);
193 tm
= time_to_tm(time
, tz
);
195 tm
= time_to_tm(0, 0);
199 strbuf_reset(&timebuf
);
200 if (mode
->type
== DATE_SHORT
)
201 strbuf_addf(&timebuf
, "%04d-%02d-%02d", tm
->tm_year
+ 1900,
202 tm
->tm_mon
+ 1, tm
->tm_mday
);
203 else if (mode
->type
== DATE_ISO8601
)
204 strbuf_addf(&timebuf
, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
208 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
210 else if (mode
->type
== DATE_ISO8601_STRICT
) {
211 char sign
= (tz
>= 0) ? '+' : '-';
213 strbuf_addf(&timebuf
, "%04d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
217 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
218 sign
, tz
/ 100, tz
% 100);
219 } else if (mode
->type
== DATE_RFC2822
)
220 strbuf_addf(&timebuf
, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
221 weekday_names
[tm
->tm_wday
], tm
->tm_mday
,
222 month_names
[tm
->tm_mon
], tm
->tm_year
+ 1900,
223 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, tz
);
225 strbuf_addf(&timebuf
, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
226 weekday_names
[tm
->tm_wday
],
227 month_names
[tm
->tm_mon
],
229 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
231 (mode
->type
== DATE_LOCAL
) ? 0 : ' ',
237 * Check these. And note how it doesn't do the summer-time conversion.
239 * In my world, it's always summer, and things are probably a bit off
242 static const struct {
246 } timezone_names
[] = {
247 { "IDLW", -12, 0, }, /* International Date Line West */
248 { "NT", -11, 0, }, /* Nome */
249 { "CAT", -10, 0, }, /* Central Alaska */
250 { "HST", -10, 0, }, /* Hawaii Standard */
251 { "HDT", -10, 1, }, /* Hawaii Daylight */
252 { "YST", -9, 0, }, /* Yukon Standard */
253 { "YDT", -9, 1, }, /* Yukon Daylight */
254 { "PST", -8, 0, }, /* Pacific Standard */
255 { "PDT", -8, 1, }, /* Pacific Daylight */
256 { "MST", -7, 0, }, /* Mountain Standard */
257 { "MDT", -7, 1, }, /* Mountain Daylight */
258 { "CST", -6, 0, }, /* Central Standard */
259 { "CDT", -6, 1, }, /* Central Daylight */
260 { "EST", -5, 0, }, /* Eastern Standard */
261 { "EDT", -5, 1, }, /* Eastern Daylight */
262 { "AST", -3, 0, }, /* Atlantic Standard */
263 { "ADT", -3, 1, }, /* Atlantic Daylight */
264 { "WAT", -1, 0, }, /* West Africa */
266 { "GMT", 0, 0, }, /* Greenwich Mean */
267 { "UTC", 0, 0, }, /* Universal (Coordinated) */
268 { "Z", 0, 0, }, /* Zulu, alias for UTC */
270 { "WET", 0, 0, }, /* Western European */
271 { "BST", 0, 1, }, /* British Summer */
272 { "CET", +1, 0, }, /* Central European */
273 { "MET", +1, 0, }, /* Middle European */
274 { "MEWT", +1, 0, }, /* Middle European Winter */
275 { "MEST", +1, 1, }, /* Middle European Summer */
276 { "CEST", +1, 1, }, /* Central European Summer */
277 { "MESZ", +1, 1, }, /* Middle European Summer */
278 { "FWT", +1, 0, }, /* French Winter */
279 { "FST", +1, 1, }, /* French Summer */
280 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
281 { "EEST", +2, 1, }, /* Eastern European Daylight */
282 { "WAST", +7, 0, }, /* West Australian Standard */
283 { "WADT", +7, 1, }, /* West Australian Daylight */
284 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
285 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
286 { "EAST", +10, 0, }, /* Eastern Australian Standard */
287 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
288 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
289 { "NZT", +12, 0, }, /* New Zealand */
290 { "NZST", +12, 0, }, /* New Zealand Standard */
291 { "NZDT", +12, 1, }, /* New Zealand Daylight */
292 { "IDLE", +12, 0, }, /* International Date Line East */
295 static int match_string(const char *date
, const char *str
)
299 for (i
= 0; *date
; date
++, str
++, i
++) {
302 if (toupper(*date
) == toupper(*str
))
311 static int skip_alpha(const char *date
)
316 } while (isalpha(date
[i
]));
321 * Parse month, weekday, or timezone name
323 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
327 for (i
= 0; i
< 12; i
++) {
328 int match
= match_string(date
, month_names
[i
]);
335 for (i
= 0; i
< 7; i
++) {
336 int match
= match_string(date
, weekday_names
[i
]);
343 for (i
= 0; i
< ARRAY_SIZE(timezone_names
); i
++) {
344 int match
= match_string(date
, timezone_names
[i
].name
);
345 if (match
>= 3 || match
== strlen(timezone_names
[i
].name
)) {
346 int off
= timezone_names
[i
].offset
;
348 /* This is bogus, but we like summer */
349 off
+= timezone_names
[i
].dst
;
351 /* Only use the tz name offset if we don't have anything better */
359 if (match_string(date
, "PM") == 2) {
360 tm
->tm_hour
= (tm
->tm_hour
% 12) + 12;
364 if (match_string(date
, "AM") == 2) {
365 tm
->tm_hour
= (tm
->tm_hour
% 12) + 0;
370 return skip_alpha(date
);
373 static int is_date(int year
, int month
, int day
, struct tm
*now_tm
, time_t now
, struct tm
*tm
)
375 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
376 struct tm check
= *tm
;
377 struct tm
*r
= (now_tm
? &check
: tm
);
380 r
->tm_mon
= month
- 1;
385 r
->tm_year
= now_tm
->tm_year
;
387 else if (year
>= 1970 && year
< 2100)
388 r
->tm_year
= year
- 1900;
389 else if (year
> 70 && year
< 100)
392 r
->tm_year
= year
+ 100;
398 specified
= tm_to_time_t(r
);
400 /* Be it commit time or author time, it does not make
401 * sense to specify timestamp way into the future. Make
402 * sure it is not later than ten days from now...
404 if ((specified
!= -1) && (now
+ 10*24*3600 < specified
))
406 tm
->tm_mon
= r
->tm_mon
;
407 tm
->tm_mday
= r
->tm_mday
;
409 tm
->tm_year
= r
->tm_year
;
415 static int match_multi_number(unsigned long num
, char c
, const char *date
,
416 char *end
, struct tm
*tm
, time_t now
)
419 struct tm
*refuse_future
;
422 num2
= strtol(end
+1, &end
, 10);
424 if (*end
== c
&& isdigit(end
[1]))
425 num3
= strtol(end
+1, &end
, 10);
432 if (num
< 25 && num2
>= 0 && num2
< 60 && num3
>= 0 && num3
<= 60) {
445 refuse_future
= NULL
;
446 if (gmtime_r(&now
, &now_tm
))
447 refuse_future
= &now_tm
;
451 if (is_date(num
, num2
, num3
, NULL
, now
, tm
))
454 if (is_date(num
, num3
, num2
, NULL
, now
, tm
))
457 /* Our eastern European friends say dd.mm.yy[yy]
458 * is the norm there, so giving precedence to
459 * mm/dd/yy[yy] form only when separator is not '.'
462 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
464 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
465 if (is_date(num3
, num2
, num
, refuse_future
, now
, tm
))
467 /* Funny European mm.dd.yy */
469 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
477 * Have we filled in any part of the time/date yet?
478 * We just do a binary 'and' to see if the sign bit
479 * is set in all the values.
481 static inline int nodate(struct tm
*tm
)
483 return (tm
->tm_year
&
492 * We've seen a digit. Time? Year? Date?
494 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
500 num
= strtoul(date
, &end
, 10);
503 * Seconds since 1970? We trigger on that for any numbers with
504 * more than 8 digits. This is because we don't want to rule out
505 * numbers like 20070606 as a YYYYMMDD date.
507 if (num
>= 100000000 && nodate(tm
)) {
509 if (gmtime_r(&time
, tm
)) {
516 * Check for special formats: num[-.:/]num[same]num
523 if (isdigit(end
[1])) {
524 int match
= match_multi_number(num
, *end
, date
, end
, tm
, 0);
531 * None of the special formats? Try to guess what
532 * the number meant. We use the number of digits
533 * to make a more educated guess..
538 } while (isdigit(date
[n
]));
540 /* Four-digit year or a timezone? */
542 if (num
<= 1400 && *offset
== -1) {
543 unsigned int minutes
= num
% 100;
544 unsigned int hours
= num
/ 100;
545 *offset
= hours
*60 + minutes
;
546 } else if (num
> 1900 && num
< 2100)
547 tm
->tm_year
= num
- 1900;
552 * Ignore lots of numerals. We took care of 4-digit years above.
553 * Days or months must be one or two digits.
559 * NOTE! We will give precedence to day-of-month over month or
560 * year numbers in the 1-12 range. So 05 is always "mday 5",
561 * unless we already have a mday..
563 * IOW, 01 Apr 05 parses as "April 1st, 2005".
565 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
570 /* Two-digit year? */
571 if (n
== 2 && tm
->tm_year
< 0) {
572 if (num
< 10 && tm
->tm_mday
>= 0) {
573 tm
->tm_year
= num
+ 100;
582 if (num
> 0 && num
< 13 && tm
->tm_mon
< 0)
588 static int match_tz(const char *date
, int *offp
)
591 int hour
= strtoul(date
+ 1, &end
, 10);
592 int n
= end
- (date
+ 1);
600 min
= 99; /* random crap */
601 } else if (*end
== ':') {
603 min
= strtoul(end
+ 1, &end
, 10);
604 if (end
- (date
+ 1) != 5)
605 min
= 99; /* random crap */
606 } /* otherwise we parsed "hh" */
609 * Don't accept any random crap. Even though some places have
610 * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
611 * UTC+14), there is something wrong if hour part is much
612 * larger than that. We might also want to check that the
613 * minutes are divisible by 15 or something too. (Offset of
614 * Kathmandu, Nepal is UTC+5:45)
616 if (min
< 60 && hour
< 24) {
617 int offset
= hour
* 60 + min
;
625 static void date_string(unsigned long date
, int offset
, struct strbuf
*buf
)
633 strbuf_addf(buf
, "%lu %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
637 * Parse a string like "0 +0000" as ancient timestamp near epoch, but
638 * only when it appears not as part of any other string.
640 static int match_object_header_date(const char *date
, unsigned long *timestamp
, int *offset
)
646 if (*date
< '0' || '9' < *date
)
648 stamp
= strtoul(date
, &end
, 10);
649 if (*end
!= ' ' || stamp
== ULONG_MAX
|| (end
[1] != '+' && end
[1] != '-'))
652 ofs
= strtol(date
, &end
, 10);
653 if ((*end
!= '\0' && (*end
!= '\n')) || end
!= date
+ 4)
655 ofs
= (ofs
/ 100) * 60 + (ofs
% 100);
663 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
664 (i.e. English) day/month names, and it doesn't work correctly with %z. */
665 int parse_date_basic(const char *date
, unsigned long *timestamp
, int *offset
)
669 unsigned long dummy_timestamp
;
673 timestamp
= &dummy_timestamp
;
675 offset
= &dummy_offset
;
677 memset(&tm
, 0, sizeof(tm
));
689 !match_object_header_date(date
+ 1, timestamp
, offset
))
690 return 0; /* success */
693 unsigned char c
= *date
;
695 /* Stop at end of string or newline */
700 match
= match_alpha(date
, &tm
, offset
);
702 match
= match_digit(date
, &tm
, offset
, &tm_gmt
);
703 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
704 match
= match_tz(date
, offset
);
714 /* do not use mktime(), which uses local timezone, here */
715 *timestamp
= tm_to_time_t(&tm
);
716 if (*timestamp
== -1)
722 /* gmtime_r() in match_digit() may have clobbered it */
724 temp_time
= mktime(&tm
);
725 if ((time_t)*timestamp
> temp_time
) {
726 *offset
= ((time_t)*timestamp
- temp_time
) / 60;
728 *offset
= -(int)((temp_time
- (time_t)*timestamp
) / 60);
733 *timestamp
-= *offset
* 60;
734 return 0; /* success */
737 int parse_expiry_date(const char *date
, unsigned long *timestamp
)
741 if (!strcmp(date
, "never") || !strcmp(date
, "false"))
743 else if (!strcmp(date
, "all") || !strcmp(date
, "now"))
745 * We take over "now" here, which usually translates
746 * to the current timestamp. This is because the user
747 * really means to expire everything she has done in
748 * the past, and by definition reflogs are the record
749 * of the past, and there is nothing from the future
752 *timestamp
= ULONG_MAX
;
754 *timestamp
= approxidate_careful(date
, &errors
);
759 int parse_date(const char *date
, struct strbuf
*result
)
761 unsigned long timestamp
;
763 if (parse_date_basic(date
, ×tamp
, &offset
))
765 date_string(timestamp
, offset
, result
);
769 void parse_date_format(const char *format
, struct date_mode
*mode
)
771 if (!strcmp(format
, "relative"))
772 mode
->type
= DATE_RELATIVE
;
773 else if (!strcmp(format
, "iso8601") ||
774 !strcmp(format
, "iso"))
775 mode
->type
= DATE_ISO8601
;
776 else if (!strcmp(format
, "iso8601-strict") ||
777 !strcmp(format
, "iso-strict"))
778 mode
->type
= DATE_ISO8601_STRICT
;
779 else if (!strcmp(format
, "rfc2822") ||
780 !strcmp(format
, "rfc"))
781 mode
->type
= DATE_RFC2822
;
782 else if (!strcmp(format
, "short"))
783 mode
->type
= DATE_SHORT
;
784 else if (!strcmp(format
, "local"))
785 mode
->type
= DATE_LOCAL
;
786 else if (!strcmp(format
, "default"))
787 mode
->type
= DATE_NORMAL
;
788 else if (!strcmp(format
, "raw"))
789 mode
->type
= DATE_RAW
;
791 die("unknown date format %s", format
);
794 void datestamp(struct strbuf
*out
)
801 offset
= tm_to_time_t(localtime(&now
)) - now
;
804 date_string(now
, offset
, out
);
808 * Relative time update (eg "2 days ago"). If we haven't set the time
809 * yet, we need to set it from current time.
811 static unsigned long update_tm(struct tm
*tm
, struct tm
*now
, unsigned long sec
)
816 tm
->tm_mday
= now
->tm_mday
;
818 tm
->tm_mon
= now
->tm_mon
;
819 if (tm
->tm_year
< 0) {
820 tm
->tm_year
= now
->tm_year
;
821 if (tm
->tm_mon
> now
->tm_mon
)
825 n
= mktime(tm
) - sec
;
830 static void date_now(struct tm
*tm
, struct tm
*now
, int *num
)
832 update_tm(tm
, now
, 0);
835 static void date_yesterday(struct tm
*tm
, struct tm
*now
, int *num
)
837 update_tm(tm
, now
, 24*60*60);
840 static void date_time(struct tm
*tm
, struct tm
*now
, int hour
)
842 if (tm
->tm_hour
< hour
)
843 date_yesterday(tm
, now
, NULL
);
849 static void date_midnight(struct tm
*tm
, struct tm
*now
, int *num
)
851 date_time(tm
, now
, 0);
854 static void date_noon(struct tm
*tm
, struct tm
*now
, int *num
)
856 date_time(tm
, now
, 12);
859 static void date_tea(struct tm
*tm
, struct tm
*now
, int *num
)
861 date_time(tm
, now
, 17);
864 static void date_pm(struct tm
*tm
, struct tm
*now
, int *num
)
875 tm
->tm_hour
= (hour
% 12) + 12;
878 static void date_am(struct tm
*tm
, struct tm
*now
, int *num
)
889 tm
->tm_hour
= (hour
% 12);
892 static void date_never(struct tm
*tm
, struct tm
*now
, int *num
)
898 static const struct special
{
900 void (*fn
)(struct tm
*, struct tm
*, int *);
902 { "yesterday", date_yesterday
},
903 { "noon", date_noon
},
904 { "midnight", date_midnight
},
908 { "never", date_never
},
913 static const char *number_name
[] = {
914 "zero", "one", "two", "three", "four",
915 "five", "six", "seven", "eight", "nine", "ten",
918 static const struct typelen
{
925 { "days", 24*60*60 },
926 { "weeks", 7*24*60*60 },
930 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, struct tm
*now
, int *num
, int *touched
)
932 const struct typelen
*tl
;
933 const struct special
*s
;
934 const char *end
= date
;
937 while (isalpha(*++end
))
940 for (i
= 0; i
< 12; i
++) {
941 int match
= match_string(date
, month_names
[i
]);
949 for (s
= special
; s
->name
; s
++) {
950 int len
= strlen(s
->name
);
951 if (match_string(date
, s
->name
) == len
) {
959 for (i
= 1; i
< 11; i
++) {
960 int len
= strlen(number_name
[i
]);
961 if (match_string(date
, number_name
[i
]) == len
) {
967 if (match_string(date
, "last") == 4) {
976 int len
= strlen(tl
->type
);
977 if (match_string(date
, tl
->type
) >= len
-1) {
978 update_tm(tm
, now
, tl
->length
* *num
);
986 for (i
= 0; i
< 7; i
++) {
987 int match
= match_string(date
, weekday_names
[i
]);
989 int diff
, n
= *num
-1;
992 diff
= tm
->tm_wday
- i
;
997 update_tm(tm
, now
, diff
* 24 * 60 * 60);
1003 if (match_string(date
, "months") >= 5) {
1005 update_tm(tm
, now
, 0); /* fill in date fields if needed */
1006 n
= tm
->tm_mon
- *num
;
1017 if (match_string(date
, "years") >= 4) {
1018 update_tm(tm
, now
, 0); /* fill in date fields if needed */
1019 tm
->tm_year
-= *num
;
1028 static const char *approxidate_digit(const char *date
, struct tm
*tm
, int *num
,
1032 unsigned long number
= strtoul(date
, &end
, 10);
1039 if (isdigit(end
[1])) {
1040 int match
= match_multi_number(number
, *end
, date
, end
,
1043 return date
+ match
;
1047 /* Accept zero-padding only for small numbers ("Dec 02", never "Dec 0002") */
1048 if (date
[0] != '0' || end
- date
<= 2)
1054 * Do we have a pending number at the end, or when
1055 * we see a new one? Let's assume it's a month day,
1056 * as in "Dec 6, 1992"
1058 static void pending_number(struct tm
*tm
, int *num
)
1064 if (tm
->tm_mday
< 0 && number
< 32)
1065 tm
->tm_mday
= number
;
1066 else if (tm
->tm_mon
< 0 && number
< 13)
1067 tm
->tm_mon
= number
-1;
1068 else if (tm
->tm_year
< 0) {
1069 if (number
> 1969 && number
< 2100)
1070 tm
->tm_year
= number
- 1900;
1071 else if (number
> 69 && number
< 100)
1072 tm
->tm_year
= number
;
1073 else if (number
< 38)
1074 tm
->tm_year
= 100 + number
;
1075 /* We screw up for number = 00 ? */
1080 static unsigned long approxidate_str(const char *date
,
1081 const struct timeval
*tv
,
1089 time_sec
= tv
->tv_sec
;
1090 localtime_r(&time_sec
, &tm
);
1098 unsigned char c
= *date
;
1103 pending_number(&tm
, &number
);
1104 date
= approxidate_digit(date
-1, &tm
, &number
, time_sec
);
1109 date
= approxidate_alpha(date
-1, &tm
, &now
, &number
, &touched
);
1111 pending_number(&tm
, &number
);
1114 return update_tm(&tm
, &now
, 0);
1117 unsigned long approxidate_relative(const char *date
, const struct timeval
*tv
)
1119 unsigned long timestamp
;
1123 if (!parse_date_basic(date
, ×tamp
, &offset
))
1125 return approxidate_str(date
, tv
, &errors
);
1128 unsigned long approxidate_careful(const char *date
, int *error_ret
)
1131 unsigned long timestamp
;
1137 if (!parse_date_basic(date
, ×tamp
, &offset
)) {
1142 gettimeofday(&tv
, NULL
);
1143 return approxidate_str(date
, &tv
, error_ret
);
1146 int date_overflows(unsigned long t
)
1150 /* If we overflowed our unsigned long, that's bad... */
1155 * ...but we also are going to feed the result to system
1156 * functions that expect time_t, which is often "signed long".
1157 * Make sure that we fit into time_t, as well.
1160 return t
!= sys
|| (t
< 1) != (sys
< 1);