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
);
58 const char *show_date(unsigned long time
, int tz
, int relative
)
61 static char timebuf
[200];
65 time_t t
= gm_time_t(time
, tz
);
67 gettimeofday(&now
, NULL
);
69 return "in the future";
70 diff
= now
.tv_sec
- t
;
72 snprintf(timebuf
, sizeof(timebuf
), "%lu seconds ago", diff
);
75 /* Turn it into minutes */
76 diff
= (diff
+ 30) / 60;
78 snprintf(timebuf
, sizeof(timebuf
), "%lu minutes ago", diff
);
81 /* Turn it into hours */
82 diff
= (diff
+ 30) / 60;
84 snprintf(timebuf
, sizeof(timebuf
), "%lu hours ago", diff
);
87 /* We deal with number of days from here on */
88 diff
= (diff
+ 12) / 24;
90 snprintf(timebuf
, sizeof(timebuf
), "%lu days ago", diff
);
93 /* Say weeks for the past 10 weeks or so */
95 snprintf(timebuf
, sizeof(timebuf
), "%lu weeks ago", (diff
+ 3) / 7);
98 /* Say months for the past 12 months or so */
100 snprintf(timebuf
, sizeof(timebuf
), "%lu months ago", (diff
+ 15) / 30);
103 /* Else fall back on absolute format.. */
106 tm
= time_to_tm(time
, tz
);
109 sprintf(timebuf
, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
110 weekday_names
[tm
->tm_wday
],
111 month_names
[tm
->tm_mon
],
113 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
114 tm
->tm_year
+ 1900, tz
);
118 const char *show_rfc2822_date(unsigned long time
, int tz
)
121 static char timebuf
[200];
123 tm
= time_to_tm(time
, tz
);
126 sprintf(timebuf
, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
127 weekday_names
[tm
->tm_wday
], tm
->tm_mday
,
128 month_names
[tm
->tm_mon
], tm
->tm_year
+ 1900,
129 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
, tz
);
134 * Check these. And note how it doesn't do the summer-time conversion.
136 * In my world, it's always summer, and things are probably a bit off
139 static const struct {
143 } timezone_names
[] = {
144 { "IDLW", -12, 0, }, /* International Date Line West */
145 { "NT", -11, 0, }, /* Nome */
146 { "CAT", -10, 0, }, /* Central Alaska */
147 { "HST", -10, 0, }, /* Hawaii Standard */
148 { "HDT", -10, 1, }, /* Hawaii Daylight */
149 { "YST", -9, 0, }, /* Yukon Standard */
150 { "YDT", -9, 1, }, /* Yukon Daylight */
151 { "PST", -8, 0, }, /* Pacific Standard */
152 { "PDT", -8, 1, }, /* Pacific Daylight */
153 { "MST", -7, 0, }, /* Mountain Standard */
154 { "MDT", -7, 1, }, /* Mountain Daylight */
155 { "CST", -6, 0, }, /* Central Standard */
156 { "CDT", -6, 1, }, /* Central Daylight */
157 { "EST", -5, 0, }, /* Eastern Standard */
158 { "EDT", -5, 1, }, /* Eastern Daylight */
159 { "AST", -3, 0, }, /* Atlantic Standard */
160 { "ADT", -3, 1, }, /* Atlantic Daylight */
161 { "WAT", -1, 0, }, /* West Africa */
163 { "GMT", 0, 0, }, /* Greenwich Mean */
164 { "UTC", 0, 0, }, /* Universal (Coordinated) */
166 { "WET", 0, 0, }, /* Western European */
167 { "BST", 0, 1, }, /* British Summer */
168 { "CET", +1, 0, }, /* Central European */
169 { "MET", +1, 0, }, /* Middle European */
170 { "MEWT", +1, 0, }, /* Middle European Winter */
171 { "MEST", +1, 1, }, /* Middle European Summer */
172 { "CEST", +1, 1, }, /* Central European Summer */
173 { "MESZ", +1, 1, }, /* Middle European Summer */
174 { "FWT", +1, 0, }, /* French Winter */
175 { "FST", +1, 1, }, /* French Summer */
176 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
177 { "EEST", +2, 1, }, /* Eastern European Daylight */
178 { "WAST", +7, 0, }, /* West Australian Standard */
179 { "WADT", +7, 1, }, /* West Australian Daylight */
180 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
181 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
182 { "EAST", +10, 0, }, /* Eastern Australian Standard */
183 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
184 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
185 { "NZT", +11, 0, }, /* New Zealand */
186 { "NZST", +11, 0, }, /* New Zealand Standard */
187 { "NZDT", +11, 1, }, /* New Zealand Daylight */
188 { "IDLE", +12, 0, }, /* International Date Line East */
191 static int match_string(const char *date
, const char *str
)
195 for (i
= 0; *date
; date
++, str
++, i
++) {
198 if (toupper(*date
) == toupper(*str
))
207 static int skip_alpha(const char *date
)
212 } while (isalpha(date
[i
]));
217 * Parse month, weekday, or timezone name
219 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
223 for (i
= 0; i
< 12; i
++) {
224 int match
= match_string(date
, month_names
[i
]);
231 for (i
= 0; i
< 7; i
++) {
232 int match
= match_string(date
, weekday_names
[i
]);
239 for (i
= 0; i
< ARRAY_SIZE(timezone_names
); i
++) {
240 int match
= match_string(date
, timezone_names
[i
].name
);
242 int off
= timezone_names
[i
].offset
;
244 /* This is bogus, but we like summer */
245 off
+= timezone_names
[i
].dst
;
247 /* Only use the tz name offset if we don't have anything better */
255 if (match_string(date
, "PM") == 2) {
256 tm
->tm_hour
= (tm
->tm_hour
% 12) + 12;
260 if (match_string(date
, "AM") == 2) {
261 tm
->tm_hour
= (tm
->tm_hour
% 12) + 0;
266 return skip_alpha(date
);
269 static int is_date(int year
, int month
, int day
, struct tm
*now_tm
, time_t now
, struct tm
*tm
)
271 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
272 struct tm check
= *tm
;
273 struct tm
*r
= (now_tm
? &check
: tm
);
276 r
->tm_mon
= month
- 1;
281 r
->tm_year
= now_tm
->tm_year
;
283 else if (year
>= 1970 && year
< 2100)
284 r
->tm_year
= year
- 1900;
285 else if (year
> 70 && year
< 100)
288 r
->tm_year
= year
+ 100;
294 specified
= my_mktime(r
);
296 /* Be it commit time or author time, it does not make
297 * sense to specify timestamp way into the future. Make
298 * sure it is not later than ten days from now...
300 if (now
+ 10*24*3600 < specified
)
302 tm
->tm_mon
= r
->tm_mon
;
303 tm
->tm_mday
= r
->tm_mday
;
305 tm
->tm_year
= r
->tm_year
;
311 static int match_multi_number(unsigned long num
, char c
, const char *date
, char *end
, struct tm
*tm
)
315 struct tm
*refuse_future
;
318 num2
= strtol(end
+1, &end
, 10);
320 if (*end
== c
&& isdigit(end
[1]))
321 num3
= strtol(end
+1, &end
, 10);
328 if (num
< 25 && num2
>= 0 && num2
< 60 && num3
>= 0 && num3
<= 60) {
340 refuse_future
= NULL
;
341 if (gmtime_r(&now
, &now_tm
))
342 refuse_future
= &now_tm
;
346 if (is_date(num
, num2
, num3
, refuse_future
, now
, tm
))
349 if (is_date(num
, num3
, num2
, refuse_future
, now
, tm
))
352 /* Our eastern European friends say dd.mm.yy[yy]
353 * is the norm there, so giving precedence to
354 * mm/dd/yy[yy] form only when separator is not '.'
357 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
359 /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
360 if (is_date(num3
, num2
, num
, refuse_future
, now
, tm
))
362 /* Funny European mm.dd.yy */
364 is_date(num3
, num
, num2
, refuse_future
, now
, tm
))
372 * We've seen a digit. Time? Year? Date?
374 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
380 num
= strtoul(date
, &end
, 10);
383 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
385 if (num
> 946684800) {
387 if (gmtime_r(&time
, tm
)) {
394 * Check for special formats: num[-.:/]num[same]num
401 if (isdigit(end
[1])) {
402 int match
= match_multi_number(num
, *end
, date
, end
, tm
);
409 * None of the special formats? Try to guess what
410 * the number meant. We use the number of digits
411 * to make a more educated guess..
416 } while (isdigit(date
[n
]));
418 /* Four-digit year or a timezone? */
420 if (num
<= 1400 && *offset
== -1) {
421 unsigned int minutes
= num
% 100;
422 unsigned int hours
= num
/ 100;
423 *offset
= hours
*60 + minutes
;
424 } else if (num
> 1900 && num
< 2100)
425 tm
->tm_year
= num
- 1900;
430 * NOTE! We will give precedence to day-of-month over month or
431 * year numbers in the 1-12 range. So 05 is always "mday 5",
432 * unless we already have a mday..
434 * IOW, 01 Apr 05 parses as "April 1st, 2005".
436 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
441 /* Two-digit year? */
442 if (n
== 2 && tm
->tm_year
< 0) {
443 if (num
< 10 && tm
->tm_mday
>= 0) {
444 tm
->tm_year
= num
+ 100;
453 if (num
> 0 && num
< 32) {
455 } else if (num
> 1900) {
456 tm
->tm_year
= num
- 1900;
457 } else if (num
> 70) {
459 } else if (num
> 0 && num
< 13) {
466 static int match_tz(const char *date
, int *offp
)
469 int offset
= strtoul(date
+1, &end
, 10);
471 int n
= end
- date
- 1;
477 * Don't accept any random crap.. At least 3 digits, and
478 * a valid minute. We might want to check that the minutes
479 * are divisible by 30 or something too.
481 if (min
< 60 && n
> 2) {
482 offset
= hour
*60+min
;
491 static int date_string(unsigned long date
, int offset
, char *buf
, int len
)
499 return snprintf(buf
, len
, "%lu %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
502 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
503 (i.e. English) day/month names, and it doesn't work correctly with %z. */
504 int parse_date(const char *date
, char *result
, int maxlen
)
510 memset(&tm
, 0, sizeof(tm
));
520 unsigned char c
= *date
;
522 /* Stop at end of string or newline */
527 match
= match_alpha(date
, &tm
, &offset
);
529 match
= match_digit(date
, &tm
, &offset
, &tm_gmt
);
530 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
531 match
= match_tz(date
, &offset
);
541 /* mktime uses local timezone */
542 then
= my_mktime(&tm
);
544 offset
= (then
- mktime(&tm
)) / 60;
551 return date_string(then
, offset
, result
, maxlen
);
554 void datestamp(char *buf
, int bufsize
)
561 offset
= my_mktime(localtime(&now
)) - now
;
564 date_string(now
, offset
, buf
, bufsize
);
567 static void update_tm(struct tm
*tm
, unsigned long sec
)
569 time_t n
= mktime(tm
) - sec
;
573 static void date_yesterday(struct tm
*tm
, int *num
)
575 update_tm(tm
, 24*60*60);
578 static void date_time(struct tm
*tm
, int hour
)
580 if (tm
->tm_hour
< hour
)
581 date_yesterday(tm
, NULL
);
587 static void date_midnight(struct tm
*tm
, int *num
)
592 static void date_noon(struct tm
*tm
, int *num
)
597 static void date_tea(struct tm
*tm
, int *num
)
602 static void date_pm(struct tm
*tm
, int *num
)
613 tm
->tm_hour
= (hour
% 12) + 12;
616 static void date_am(struct tm
*tm
, int *num
)
627 tm
->tm_hour
= (hour
% 12);
630 static const struct special
{
632 void (*fn
)(struct tm
*, int *);
634 { "yesterday", date_yesterday
},
635 { "noon", date_noon
},
636 { "midnight", date_midnight
},
643 static const char *number_name
[] = {
644 "zero", "one", "two", "three", "four",
645 "five", "six", "seven", "eight", "nine", "ten",
648 static const struct typelen
{
655 { "days", 24*60*60 },
656 { "weeks", 7*24*60*60 },
660 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, int *num
)
662 const struct typelen
*tl
;
663 const struct special
*s
;
664 const char *end
= date
;
667 while (isalpha(*++end
));
670 for (i
= 0; i
< 12; i
++) {
671 int match
= match_string(date
, month_names
[i
]);
678 for (s
= special
; s
->name
; s
++) {
679 int len
= strlen(s
->name
);
680 if (match_string(date
, s
->name
) == len
) {
687 for (i
= 1; i
< 11; i
++) {
688 int len
= strlen(number_name
[i
]);
689 if (match_string(date
, number_name
[i
]) == len
) {
694 if (match_string(date
, "last") == 4)
701 int len
= strlen(tl
->type
);
702 if (match_string(date
, tl
->type
) >= len
-1) {
703 update_tm(tm
, tl
->length
* *num
);
710 for (i
= 0; i
< 7; i
++) {
711 int match
= match_string(date
, weekday_names
[i
]);
713 int diff
, n
= *num
-1;
716 diff
= tm
->tm_wday
- i
;
721 update_tm(tm
, diff
* 24 * 60 * 60);
726 if (match_string(date
, "months") >= 5) {
727 int n
= tm
->tm_mon
- *num
;
737 if (match_string(date
, "years") >= 4) {
746 static const char *approxidate_digit(const char *date
, struct tm
*tm
, int *num
)
749 unsigned long number
= strtoul(date
, &end
, 10);
756 if (isdigit(end
[1])) {
757 int match
= match_multi_number(number
, *end
, date
, end
, tm
);
767 unsigned long approxidate(const char *date
)
774 if (parse_date(date
, buffer
, sizeof(buffer
)) > 0)
775 return strtoul(buffer
, NULL
, 10);
777 gettimeofday(&tv
, NULL
);
778 localtime_r(&tv
.tv_sec
, &tm
);
781 unsigned char c
= *date
;
786 date
= approxidate_digit(date
-1, &tm
, &number
);
790 date
= approxidate_alpha(date
-1, &tm
, &number
);
792 if (number
> 0 && number
< 32)
794 if (tm
.tm_mon
> now
.tm_mon
&& tm
.tm_year
== now
.tm_year
)