2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 static time_t my_mktime(struct tm
*tm
)
11 static const int mdays
[] = {
12 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
14 int year
= tm
->tm_year
- 70;
15 int month
= tm
->tm_mon
;
16 int day
= tm
->tm_mday
;
18 if (year
< 0 || year
> 129) /* algo only works for 1970-2099 */
20 if (month
< 0 || month
> 11) /* array bounds */
22 if (month
< 2 || (year
+ 2) % 4)
24 return (year
* 365 + (year
+ 1) / 4 + mdays
[month
] + day
) * 24*60*60UL +
25 tm
->tm_hour
* 60*60 + tm
->tm_min
* 60 + tm
->tm_sec
;
28 static const char *month_names
[] = {
29 "January", "February", "March", "April", "May", "June",
30 "July", "August", "September", "October", "November", "December"
33 static const char *weekday_names
[] = {
34 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
37 static time_t gm_time_t(unsigned long time
, int tz
)
41 minutes
= tz
< 0 ? -tz
: tz
;
42 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
43 minutes
= tz
< 0 ? -minutes
: minutes
;
44 return time
+ minutes
* 60;
48 * The "tz" thing is passed in as this strange "decimal parse of tz"
49 * thing, which means that tz -0100 is passed in as the integer -100,
50 * even though it means "sixty minutes off"
52 static struct tm
*time_to_tm(unsigned long time
, int tz
)
54 time_t t
= gm_time_t(time
, tz
);
59 * What value of "tz" was in effect back then at "time" in the
62 static int local_tzoffset(unsigned long time
)
70 t_local
= my_mktime(&tm
);
79 offset
/= 60; /* in minutes */
80 offset
= (offset
% 60) + ((offset
/ 60) * 100);
81 return offset
* eastwest
;
84 const char *show_date(unsigned long time
, int tz
, enum date_mode mode
)
87 static char timebuf
[200];
89 if (mode
== DATE_RELATIVE
) {
92 gettimeofday(&now
, NULL
);
93 if (now
.tv_sec
< time
)
94 return "in the future";
95 diff
= now
.tv_sec
- time
;
97 snprintf(timebuf
, sizeof(timebuf
), "%lu seconds ago", diff
);
100 /* Turn it into minutes */
101 diff
= (diff
+ 30) / 60;
103 snprintf(timebuf
, sizeof(timebuf
), "%lu minutes ago", diff
);
106 /* Turn it into hours */
107 diff
= (diff
+ 30) / 60;
109 snprintf(timebuf
, sizeof(timebuf
), "%lu hours ago", diff
);
112 /* We deal with number of days from here on */
113 diff
= (diff
+ 12) / 24;
115 snprintf(timebuf
, sizeof(timebuf
), "%lu days ago", diff
);
118 /* Say weeks for the past 10 weeks or so */
120 snprintf(timebuf
, sizeof(timebuf
), "%lu weeks ago", (diff
+ 3) / 7);
123 /* Say months for the past 12 months or so */
125 snprintf(timebuf
, sizeof(timebuf
), "%lu months ago", (diff
+ 15) / 30);
128 /* Else fall back on absolute format.. */
131 if (mode
== DATE_LOCAL
)
132 tz
= local_tzoffset(time
);
134 tm
= time_to_tm(time
, tz
);
137 if (mode
== DATE_SHORT
)
138 sprintf(timebuf
, "%04d-%02d-%02d", tm
->tm_year
+ 1900,
139 tm
->tm_mon
+ 1, tm
->tm_mday
);
140 else if (mode
== DATE_ISO8601
)
141 sprintf(timebuf
, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
145 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
148 sprintf(timebuf
, "%.3s %.3s %d %02d:%02d:%02d %d%c%+05d",
149 weekday_names
[tm
->tm_wday
],
150 month_names
[tm
->tm_mon
],
152 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
154 (mode
== DATE_LOCAL
) ? 0 : ' ',
159 const char *show_rfc2822_date(unsigned long time
, int tz
)
162 static char timebuf
[200];
164 tm
= time_to_tm(time
, tz
);
167 sprintf(timebuf
, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
168 weekday_names
[tm
->tm_wday
], tm
->tm_mday
,
169 month_names
[tm
->tm_mon
], tm
->tm_year
+ 1900,
170 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, tz
);
175 * Check these. And note how it doesn't do the summer-time conversion.
177 * In my world, it's always summer, and things are probably a bit off
180 static const struct {
184 } timezone_names
[] = {
185 { "IDLW", -12, 0, }, /* International Date Line West */
186 { "NT", -11, 0, }, /* Nome */
187 { "CAT", -10, 0, }, /* Central Alaska */
188 { "HST", -10, 0, }, /* Hawaii Standard */
189 { "HDT", -10, 1, }, /* Hawaii Daylight */
190 { "YST", -9, 0, }, /* Yukon Standard */
191 { "YDT", -9, 1, }, /* Yukon Daylight */
192 { "PST", -8, 0, }, /* Pacific Standard */
193 { "PDT", -8, 1, }, /* Pacific Daylight */
194 { "MST", -7, 0, }, /* Mountain Standard */
195 { "MDT", -7, 1, }, /* Mountain Daylight */
196 { "CST", -6, 0, }, /* Central Standard */
197 { "CDT", -6, 1, }, /* Central Daylight */
198 { "EST", -5, 0, }, /* Eastern Standard */
199 { "EDT", -5, 1, }, /* Eastern Daylight */
200 { "AST", -3, 0, }, /* Atlantic Standard */
201 { "ADT", -3, 1, }, /* Atlantic Daylight */
202 { "WAT", -1, 0, }, /* West Africa */
204 { "GMT", 0, 0, }, /* Greenwich Mean */
205 { "UTC", 0, 0, }, /* Universal (Coordinated) */
207 { "WET", 0, 0, }, /* Western European */
208 { "BST", 0, 1, }, /* British Summer */
209 { "CET", +1, 0, }, /* Central European */
210 { "MET", +1, 0, }, /* Middle European */
211 { "MEWT", +1, 0, }, /* Middle European Winter */
212 { "MEST", +1, 1, }, /* Middle European Summer */
213 { "CEST", +1, 1, }, /* Central European Summer */
214 { "MESZ", +1, 1, }, /* Middle European Summer */
215 { "FWT", +1, 0, }, /* French Winter */
216 { "FST", +1, 1, }, /* French Summer */
217 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
218 { "EEST", +2, 1, }, /* Eastern European Daylight */
219 { "WAST", +7, 0, }, /* West Australian Standard */
220 { "WADT", +7, 1, }, /* West Australian Daylight */
221 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
222 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
223 { "EAST", +10, 0, }, /* Eastern Australian Standard */
224 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
225 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
226 { "NZT", +11, 0, }, /* New Zealand */
227 { "NZST", +11, 0, }, /* New Zealand Standard */
228 { "NZDT", +11, 1, }, /* New Zealand Daylight */
229 { "IDLE", +12, 0, }, /* International Date Line East */
232 static int match_string(const char *date
, const char *str
)
236 for (i
= 0; *date
; date
++, str
++, i
++) {
239 if (toupper(*date
) == toupper(*str
))
248 static int skip_alpha(const char *date
)
253 } while (isalpha(date
[i
]));
258 * Parse month, weekday, or timezone name
260 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
264 for (i
= 0; i
< 12; i
++) {
265 int match
= match_string(date
, month_names
[i
]);
272 for (i
= 0; i
< 7; i
++) {
273 int match
= match_string(date
, weekday_names
[i
]);
280 for (i
= 0; i
< ARRAY_SIZE(timezone_names
); i
++) {
281 int match
= match_string(date
, timezone_names
[i
].name
);
283 int off
= timezone_names
[i
].offset
;
285 /* This is bogus, but we like summer */
286 off
+= timezone_names
[i
].dst
;
288 /* Only use the tz name offset if we don't have anything better */
296 if (match_string(date
, "PM") == 2) {
297 tm
->tm_hour
= (tm
->tm_hour
% 12) + 12;
301 if (match_string(date
, "AM") == 2) {
302 tm
->tm_hour
= (tm
->tm_hour
% 12) + 0;
307 return skip_alpha(date
);
310 static int is_date(int year
, int month
, int day
, struct tm
*now_tm
, time_t now
, struct tm
*tm
)
312 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
313 struct tm check
= *tm
;
314 struct tm
*r
= (now_tm
? &check
: tm
);
317 r
->tm_mon
= month
- 1;
322 r
->tm_year
= now_tm
->tm_year
;
324 else if (year
>= 1970 && year
< 2100)
325 r
->tm_year
= year
- 1900;
326 else if (year
> 70 && year
< 100)
329 r
->tm_year
= year
+ 100;
335 specified
= my_mktime(r
);
337 /* Be it commit time or author time, it does not make
338 * sense to specify timestamp way into the future. Make
339 * sure it is not later than ten days from now...
341 if (now
+ 10*24*3600 < specified
)
343 tm
->tm_mon
= r
->tm_mon
;
344 tm
->tm_mday
= r
->tm_mday
;
346 tm
->tm_year
= r
->tm_year
;
352 static int match_multi_number(unsigned long num
, char c
, const char *date
, char *end
, struct tm
*tm
)
356 struct tm
*refuse_future
;
359 num2
= strtol(end
+1, &end
, 10);
361 if (*end
== c
&& isdigit(end
[1]))
362 num3
= strtol(end
+1, &end
, 10);
369 if (num
< 25 && num2
>= 0 && num2
< 60 && num3
>= 0 && num3
<= 60) {
381 refuse_future
= NULL
;
382 if (gmtime_r(&now
, &now_tm
))
383 refuse_future
= &now_tm
;
387 if (is_date(num
, num2
, num3
, refuse_future
, now
, tm
))
390 if (is_date(num
, num3
, num2
, refuse_future
, now
, tm
))
393 /* Our eastern European friends say dd.mm.yy[yy]
394 * is the norm there, so giving precedence to
395 * mm/dd/yy[yy] form only when separator is not '.'
398 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
400 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
401 if (is_date(num3
, num2
, num
, refuse_future
, now
, tm
))
403 /* Funny European mm.dd.yy */
405 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
413 * We've seen a digit. Time? Year? Date?
415 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
421 num
= strtoul(date
, &end
, 10);
424 * Seconds since 1970? We trigger on that for any numbers with
425 * more than 8 digits. This is because we don't want to rule out
426 * numbers like 20070606 as a YYYYMMDD date.
428 if (num
>= 100000000) {
430 if (gmtime_r(&time
, tm
)) {
437 * Check for special formats: num[-.:/]num[same]num
444 if (isdigit(end
[1])) {
445 int match
= match_multi_number(num
, *end
, date
, end
, tm
);
452 * None of the special formats? Try to guess what
453 * the number meant. We use the number of digits
454 * to make a more educated guess..
459 } while (isdigit(date
[n
]));
461 /* Four-digit year or a timezone? */
463 if (num
<= 1400 && *offset
== -1) {
464 unsigned int minutes
= num
% 100;
465 unsigned int hours
= num
/ 100;
466 *offset
= hours
*60 + minutes
;
467 } else if (num
> 1900 && num
< 2100)
468 tm
->tm_year
= num
- 1900;
473 * NOTE! We will give precedence to day-of-month over month or
474 * year numbers in the 1-12 range. So 05 is always "mday 5",
475 * unless we already have a mday..
477 * IOW, 01 Apr 05 parses as "April 1st, 2005".
479 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
484 /* Two-digit year? */
485 if (n
== 2 && tm
->tm_year
< 0) {
486 if (num
< 10 && tm
->tm_mday
>= 0) {
487 tm
->tm_year
= num
+ 100;
496 if (num
> 0 && num
< 32) {
498 } else if (num
> 1900) {
499 tm
->tm_year
= num
- 1900;
500 } else if (num
> 70) {
502 } else if (num
> 0 && num
< 13) {
509 static int match_tz(const char *date
, int *offp
)
512 int offset
= strtoul(date
+1, &end
, 10);
514 int n
= end
- date
- 1;
520 * Don't accept any random crap.. At least 3 digits, and
521 * a valid minute. We might want to check that the minutes
522 * are divisible by 30 or something too.
524 if (min
< 60 && n
> 2) {
525 offset
= hour
*60+min
;
534 static int date_string(unsigned long date
, int offset
, char *buf
, int len
)
542 return snprintf(buf
, len
, "%lu %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
545 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
546 (i.e. English) day/month names, and it doesn't work correctly with %z. */
547 int parse_date(const char *date
, char *result
, int maxlen
)
553 memset(&tm
, 0, sizeof(tm
));
563 unsigned char c
= *date
;
565 /* Stop at end of string or newline */
570 match
= match_alpha(date
, &tm
, &offset
);
572 match
= match_digit(date
, &tm
, &offset
, &tm_gmt
);
573 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
574 match
= match_tz(date
, &offset
);
584 /* mktime uses local timezone */
585 then
= my_mktime(&tm
);
587 offset
= (then
- mktime(&tm
)) / 60;
594 return date_string(then
, offset
, result
, maxlen
);
597 void datestamp(char *buf
, int bufsize
)
604 offset
= my_mktime(localtime(&now
)) - now
;
607 date_string(now
, offset
, buf
, bufsize
);
610 static void update_tm(struct tm
*tm
, unsigned long sec
)
612 time_t n
= mktime(tm
) - sec
;
616 static void date_yesterday(struct tm
*tm
, int *num
)
618 update_tm(tm
, 24*60*60);
621 static void date_time(struct tm
*tm
, int hour
)
623 if (tm
->tm_hour
< hour
)
624 date_yesterday(tm
, NULL
);
630 static void date_midnight(struct tm
*tm
, int *num
)
635 static void date_noon(struct tm
*tm
, int *num
)
640 static void date_tea(struct tm
*tm
, int *num
)
645 static void date_pm(struct tm
*tm
, int *num
)
656 tm
->tm_hour
= (hour
% 12) + 12;
659 static void date_am(struct tm
*tm
, int *num
)
670 tm
->tm_hour
= (hour
% 12);
673 static const struct special
{
675 void (*fn
)(struct tm
*, int *);
677 { "yesterday", date_yesterday
},
678 { "noon", date_noon
},
679 { "midnight", date_midnight
},
686 static const char *number_name
[] = {
687 "zero", "one", "two", "three", "four",
688 "five", "six", "seven", "eight", "nine", "ten",
691 static const struct typelen
{
698 { "days", 24*60*60 },
699 { "weeks", 7*24*60*60 },
703 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, int *num
)
705 const struct typelen
*tl
;
706 const struct special
*s
;
707 const char *end
= date
;
710 while (isalpha(*++end
));
713 for (i
= 0; i
< 12; i
++) {
714 int match
= match_string(date
, month_names
[i
]);
721 for (s
= special
; s
->name
; s
++) {
722 int len
= strlen(s
->name
);
723 if (match_string(date
, s
->name
) == len
) {
730 for (i
= 1; i
< 11; i
++) {
731 int len
= strlen(number_name
[i
]);
732 if (match_string(date
, number_name
[i
]) == len
) {
737 if (match_string(date
, "last") == 4)
744 int len
= strlen(tl
->type
);
745 if (match_string(date
, tl
->type
) >= len
-1) {
746 update_tm(tm
, tl
->length
* *num
);
753 for (i
= 0; i
< 7; i
++) {
754 int match
= match_string(date
, weekday_names
[i
]);
756 int diff
, n
= *num
-1;
759 diff
= tm
->tm_wday
- i
;
764 update_tm(tm
, diff
* 24 * 60 * 60);
769 if (match_string(date
, "months") >= 5) {
770 int n
= tm
->tm_mon
- *num
;
780 if (match_string(date
, "years") >= 4) {
789 static const char *approxidate_digit(const char *date
, struct tm
*tm
, int *num
)
792 unsigned long number
= strtoul(date
, &end
, 10);
799 if (isdigit(end
[1])) {
800 int match
= match_multi_number(number
, *end
, date
, end
, tm
);
810 unsigned long approxidate(const char *date
)
817 if (parse_date(date
, buffer
, sizeof(buffer
)) > 0)
818 return strtoul(buffer
, NULL
, 10);
820 gettimeofday(&tv
, NULL
);
821 localtime_r(&tv
.tv_sec
, &tm
);
824 unsigned char c
= *date
;
829 date
= approxidate_digit(date
-1, &tm
, &number
);
833 date
= approxidate_alpha(date
-1, &tm
, &number
);
835 if (number
> 0 && number
< 32)
837 if (tm
.tm_mon
> now
.tm_mon
&& tm
.tm_year
== now
.tm_year
)