2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
12 static time_t my_mktime(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 return (year
* 365 + (year
+ 1) / 4 + mdays
[month
] + day
) * 24*60*60UL +
28 tm
->tm_hour
* 60*60 + tm
->tm_min
* 60 + tm
->tm_sec
;
31 static const char *month_names
[] = {
32 "January", "February", "March", "April", "May", "June",
33 "July", "August", "September", "October", "November", "December"
36 static const char *weekday_names
[] = {
37 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
41 * The "tz" thing is passed in as this strange "decimal parse of tz"
42 * thing, which means that tz -0100 is passed in as the integer -100,
43 * even though it means "sixty minutes off"
45 const char *show_date(unsigned long time
, int tz
)
49 static char timebuf
[200];
52 minutes
= tz
< 0 ? -tz
: tz
;
53 minutes
= (minutes
/ 100)*60 + (minutes
% 100);
54 minutes
= tz
< 0 ? -minutes
: minutes
;
55 t
= time
+ minutes
* 60;
59 sprintf(timebuf
, "%.3s %.3s %d %02d:%02d:%02d %d %+05d",
60 weekday_names
[tm
->tm_wday
],
61 month_names
[tm
->tm_mon
],
63 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
64 tm
->tm_year
+ 1900, tz
);
69 * Check these. And note how it doesn't do the summer-time conversion.
71 * In my world, it's always summer, and things are probably a bit off
78 } timezone_names
[] = {
79 { "IDLW", -12, 0, }, /* International Date Line West */
80 { "NT", -11, 0, }, /* Nome */
81 { "CAT", -10, 0, }, /* Central Alaska */
82 { "HST", -10, 0, }, /* Hawaii Standard */
83 { "HDT", -10, 1, }, /* Hawaii Daylight */
84 { "YST", -9, 0, }, /* Yukon Standard */
85 { "YDT", -9, 1, }, /* Yukon Daylight */
86 { "PST", -8, 0, }, /* Pacific Standard */
87 { "PDT", -8, 1, }, /* Pacific Daylight */
88 { "MST", -7, 0, }, /* Mountain Standard */
89 { "MDT", -7, 1, }, /* Mountain Daylight */
90 { "CST", -6, 0, }, /* Central Standard */
91 { "CDT", -6, 1, }, /* Central Daylight */
92 { "EST", -5, 0, }, /* Eastern Standard */
93 { "EDT", -5, 1, }, /* Eastern Daylight */
94 { "AST", -3, 0, }, /* Atlantic Standard */
95 { "ADT", -3, 1, }, /* Atlantic Daylight */
96 { "WAT", -1, 0, }, /* West Africa */
98 { "GMT", 0, 0, }, /* Greenwich Mean */
99 { "UTC", 0, 0, }, /* Universal (Coordinated) */
101 { "WET", 0, 0, }, /* Western European */
102 { "BST", 0, 1, }, /* British Summer */
103 { "CET", +1, 0, }, /* Central European */
104 { "MET", +1, 0, }, /* Middle European */
105 { "MEWT", +1, 0, }, /* Middle European Winter */
106 { "MEST", +1, 1, }, /* Middle European Summer */
107 { "CEST", +1, 1, }, /* Central European Summer */
108 { "MESZ", +1, 1, }, /* Middle European Summer */
109 { "FWT", +1, 0, }, /* French Winter */
110 { "FST", +1, 1, }, /* French Summer */
111 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
112 { "EEST", +2, 1, }, /* Eastern European Daylight */
113 { "WAST", +7, 0, }, /* West Australian Standard */
114 { "WADT", +7, 1, }, /* West Australian Daylight */
115 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
116 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
117 { "EAST", +10, 0, }, /* Eastern Australian Standard */
118 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
119 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
120 { "NZT", +11, 0, }, /* New Zealand */
121 { "NZST", +11, 0, }, /* New Zealand Standard */
122 { "NZDT", +11, 1, }, /* New Zealand Daylight */
123 { "IDLE", +12, 0, }, /* International Date Line East */
126 static int match_string(const char *date
, const char *str
)
130 for (i
= 0; *date
; date
++, str
++, i
++) {
133 if (toupper(*date
) == toupper(*str
))
142 static int skip_alpha(const char *date
)
147 } while (isalpha(date
[i
]));
152 * Parse month, weekday, or timezone name
154 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
158 for (i
= 0; i
< 12; i
++) {
159 int match
= match_string(date
, month_names
[i
]);
166 for (i
= 0; i
< 7; i
++) {
167 int match
= match_string(date
, weekday_names
[i
]);
174 for (i
= 0; i
< ARRAY_SIZE(timezone_names
); i
++) {
175 int match
= match_string(date
, timezone_names
[i
].name
);
177 int off
= timezone_names
[i
].offset
;
179 /* This is bogus, but we like summer */
180 off
+= timezone_names
[i
].dst
;
182 /* Only use the tz name offset if we don't have anything better */
190 if (match_string(date
, "PM") == 2) {
191 if (tm
->tm_hour
> 0 && tm
->tm_hour
< 12)
197 return skip_alpha(date
);
200 static int is_date(int year
, int month
, int day
, struct tm
*tm
)
202 if (month
> 0 && month
< 13 && day
> 0 && day
< 32) {
204 tm
->tm_mon
= month
-1;
208 if (year
>= 1970 && year
< 2100) {
210 } else if (year
> 70 && year
< 100) {
212 } else if (year
< 38) {
217 tm
->tm_mon
= month
-1;
225 static int match_multi_number(unsigned long num
, char c
, const char *date
, char *end
, struct tm
*tm
)
229 num2
= strtol(end
+1, &end
, 10);
231 if (*end
== c
&& isdigit(end
[1]))
232 num3
= strtol(end
+1, &end
, 10);
239 if (num
< 25 && num2
>= 0 && num2
< 60 && num3
>= 0 && num3
<= 60) {
251 if (is_date(num
, num2
, num3
, tm
))
254 if (is_date(num
, num3
, num2
, tm
))
258 if (is_date(num3
, num2
, num
, tm
))
261 if (is_date(num3
, num
, num2
, tm
))
269 * We've seen a digit. Time? Year? Date?
271 static int match_digit(const char *date
, struct tm
*tm
, int *offset
, int *tm_gmt
)
277 num
= strtoul(date
, &end
, 10);
280 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
282 if (num
> 946684800) {
284 if (gmtime_r(&time
, tm
)) {
291 * Check for special formats: num[:-/]num[same]num
297 if (isdigit(end
[1])) {
298 int match
= match_multi_number(num
, *end
, date
, end
, tm
);
305 * None of the special formats? Try to guess what
306 * the number meant. We use the number of digits
307 * to make a more educated guess..
312 } while (isdigit(date
[n
]));
314 /* Four-digit year or a timezone? */
316 if (num
<= 1200 && *offset
== -1) {
317 unsigned int minutes
= num
% 100;
318 unsigned int hours
= num
/ 100;
319 *offset
= hours
*60 + minutes
;
320 } else if (num
> 1900 && num
< 2100)
321 tm
->tm_year
= num
- 1900;
326 * NOTE! We will give precedence to day-of-month over month or
327 * year numbers in the 1-12 range. So 05 is always "mday 5",
328 * unless we already have a mday..
330 * IOW, 01 Apr 05 parses as "April 1st, 2005".
332 if (num
> 0 && num
< 32 && tm
->tm_mday
< 0) {
337 /* Two-digit year? */
338 if (n
== 2 && tm
->tm_year
< 0) {
339 if (num
< 10 && tm
->tm_mday
>= 0) {
340 tm
->tm_year
= num
+ 100;
349 if (num
> 0 && num
< 32) {
351 } else if (num
> 1900) {
352 tm
->tm_year
= num
- 1900;
353 } else if (num
> 70) {
355 } else if (num
> 0 && num
< 13) {
362 static int match_tz(const char *date
, int *offp
)
365 int offset
= strtoul(date
+1, &end
, 10);
367 int n
= end
- date
- 1;
373 * Don't accept any random crap.. At least 3 digits, and
374 * a valid minute. We might want to check that the minutes
375 * are divisible by 30 or something too.
377 if (min
< 60 && n
> 2) {
378 offset
= hour
*60+min
;
387 static int date_string(unsigned long date
, int offset
, char *buf
, int len
)
395 return snprintf(buf
, len
, "%lu %c%02d%02d", date
, sign
, offset
/ 60, offset
% 60);
398 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
399 (i.e. English) day/month names, and it doesn't work correctly with %z. */
400 int parse_date(const char *date
, char *result
, int maxlen
)
406 memset(&tm
, 0, sizeof(tm
));
416 unsigned char c
= *date
;
418 /* Stop at end of string or newline */
423 match
= match_alpha(date
, &tm
, &offset
);
425 match
= match_digit(date
, &tm
, &offset
, &tm_gmt
);
426 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
427 match
= match_tz(date
, &offset
);
437 /* mktime uses local timezone */
438 then
= my_mktime(&tm
);
440 offset
= (then
- mktime(&tm
)) / 60;
447 return date_string(then
, offset
, result
, maxlen
);
450 void datestamp(char *buf
, int bufsize
)
457 offset
= my_mktime(localtime(&now
)) - now
;
460 date_string(now
, offset
, buf
, bufsize
);
463 static void update_tm(struct tm
*tm
, unsigned long sec
)
465 time_t n
= mktime(tm
) - sec
;
469 static void date_yesterday(struct tm
*tm
, int *num
)
471 update_tm(tm
, 24*60*60);
474 static void date_time(struct tm
*tm
, int hour
)
476 if (tm
->tm_hour
< hour
)
477 date_yesterday(tm
, NULL
);
483 static void date_midnight(struct tm
*tm
, int *num
)
488 static void date_noon(struct tm
*tm
, int *num
)
493 static void date_tea(struct tm
*tm
, int *num
)
498 static const struct special
{
500 void (*fn
)(struct tm
*, int *);
502 { "yesterday", date_yesterday
},
503 { "noon", date_noon
},
504 { "midnight", date_midnight
},
509 static const char *number_name
[] = {
510 "zero", "one", "two", "three", "four",
511 "five", "six", "seven", "eight", "nine", "ten",
514 static const struct typelen
{
521 { "days", 24*60*60 },
522 { "weeks", 7*24*60*60 },
526 static const char *approxidate_alpha(const char *date
, struct tm
*tm
, int *num
)
528 const struct typelen
*tl
;
529 const struct special
*s
;
530 const char *end
= date
;
533 while (isalpha(*++end
))
536 for (i
= 0; i
< 12; i
++) {
537 int match
= match_string(date
, month_names
[i
]);
544 for (s
= special
; s
->name
; s
++) {
545 int len
= strlen(s
->name
);
546 if (match_string(date
, s
->name
) == len
) {
553 for (i
= 1; i
< 11; i
++) {
554 int len
= strlen(number_name
[i
]);
555 if (match_string(date
, number_name
[i
]) == len
) {
560 if (match_string(date
, "last") == 4)
567 int len
= strlen(tl
->type
);
568 if (match_string(date
, tl
->type
) >= len
-1) {
569 update_tm(tm
, tl
->length
* *num
);
576 for (i
= 0; i
< 7; i
++) {
577 int match
= match_string(date
, weekday_names
[i
]);
579 int diff
, n
= *num
-1;
582 diff
= tm
->tm_wday
- i
;
587 update_tm(tm
, diff
* 24 * 60 * 60);
592 if (match_string(date
, "months") >= 5) {
593 int n
= tm
->tm_mon
- *num
;
603 if (match_string(date
, "years") >= 4) {
612 unsigned long approxidate(const char *date
)
619 if (parse_date(date
, buffer
, sizeof(buffer
)) > 0)
620 return strtoul(buffer
, NULL
, 10);
622 gettimeofday(&tv
, NULL
);
623 localtime_r(&tv
.tv_sec
, &tm
);
626 unsigned char c
= *date
;
632 number
= strtoul(date
-1, &end
, 10);
637 date
= approxidate_alpha(date
-1, &tm
, &number
);
639 if (number
> 0 && number
< 32)
641 if (tm
.tm_mon
> now
.tm_mon
&& tm
.tm_year
== now
.tm_year
)