2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
13 static time_t my_mktime(struct tm
*tm
)
15 static const int mdays
[] = {
16 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
18 int year
= tm
->tm_year
- 70;
19 int month
= tm
->tm_mon
;
20 int day
= tm
->tm_mday
;
22 if (year
< 0 || year
> 129) /* algo only works for 1970-2099 */
24 if (month
< 0 || month
> 11) /* array bounds */
26 if (month
< 2 || (year
+ 2) % 4)
28 return (year
* 365 + (year
+ 1) / 4 + mdays
[month
] + day
) * 24*60*60UL +
29 tm
->tm_hour
* 60*60 + tm
->tm_min
* 60 + tm
->tm_sec
;
32 static const char *month_names
[] = {
33 "January", "February", "March", "April", "May", "June",
34 "July", "August", "September", "October", "November", "December"
37 static const char *weekday_names
[] = {
38 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
42 * Check these. And note how it doesn't do the summer-time conversion.
44 * In my world, it's always summer, and things are probably a bit off
51 } timezone_names
[] = {
52 { "IDLW", -12, 0, }, /* International Date Line West */
53 { "NT", -11, 0, }, /* Nome */
54 { "CAT", -10, 0, }, /* Central Alaska */
55 { "HST", -10, 0, }, /* Hawaii Standard */
56 { "HDT", -10, 1, }, /* Hawaii Daylight */
57 { "YST", -9, 0, }, /* Yukon Standard */
58 { "YDT", -9, 1, }, /* Yukon Daylight */
59 { "PST", -8, 0, }, /* Pacific Standard */
60 { "PDT", -8, 1, }, /* Pacific Daylight */
61 { "MST", -7, 0, }, /* Mountain Standard */
62 { "MDT", -7, 1, }, /* Mountain Daylight */
63 { "CST", -6, 0, }, /* Central Standard */
64 { "CDT", -6, 1, }, /* Central Daylight */
65 { "EST", -5, 0, }, /* Eastern Standard */
66 { "EDT", -5, 1, }, /* Eastern Daylight */
67 { "AST", -3, 0, }, /* Atlantic Standard */
68 { "ADT", -3, 1, }, /* Atlantic Daylight */
69 { "WAT", -1, 0, }, /* West Africa */
71 { "GMT", 0, 0, }, /* Greenwich Mean */
72 { "UTC", 0, 0, }, /* Universal (Coordinated) */
74 { "WET", 0, 0, }, /* Western European */
75 { "BST", 0, 1, }, /* British Summer */
76 { "CET", +1, 0, }, /* Central European */
77 { "MET", +1, 0, }, /* Middle European */
78 { "MEWT", +1, 0, }, /* Middle European Winter */
79 { "MEST", +1, 1, }, /* Middle European Summer */
80 { "CEST", +1, 1, }, /* Central European Summer */
81 { "MESZ", +1, 1, }, /* Middle European Summer */
82 { "FWT", +1, 0, }, /* French Winter */
83 { "FST", +1, 1, }, /* French Summer */
84 { "EET", +2, 0, }, /* Eastern Europe, USSR Zone 1 */
85 { "WAST", +7, 0, }, /* West Australian Standard */
86 { "WADT", +7, 1, }, /* West Australian Daylight */
87 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
88 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
89 { "EAST", +10, 0, }, /* Eastern Australian Standard */
90 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
91 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
92 { "NZT", +11, 0, }, /* New Zealand */
93 { "NZST", +11, 0, }, /* New Zealand Standard */
94 { "NZDT", +11, 1, }, /* New Zealand Daylight */
95 { "IDLE", +12, 0, }, /* International Date Line East */
98 #define NR_TZ (sizeof(timezone_names) / sizeof(timezone_names[0]))
100 static int match_string(const char *date
, const char *str
)
104 for (i
= 0; *date
; date
++, str
++, i
++) {
107 if (toupper(*date
) == toupper(*str
))
117 * Parse month, weekday, or timezone name
119 static int match_alpha(const char *date
, struct tm
*tm
, int *offset
)
123 for (i
= 0; i
< 12; i
++) {
124 int match
= match_string(date
, month_names
[i
]);
131 for (i
= 0; i
< 7; i
++) {
132 int match
= match_string(date
, weekday_names
[i
]);
139 for (i
= 0; i
< NR_TZ
; i
++) {
140 int match
= match_string(date
, timezone_names
[i
].name
);
142 int off
= timezone_names
[i
].offset
;
144 /* This is bogus, but we like summer */
145 off
+= timezone_names
[i
].dst
;
156 static int match_digit(char *date
, struct tm
*tm
, int *offset
)
159 unsigned long num
, num2
, num3
;
161 num
= strtoul(date
, &end
, 10);
163 /* Time? num:num[:num] */
164 if (num
< 24 && end
[0] == ':' && isdigit(end
[1])) {
166 num
= strtoul(end
+1, &end
, 10);
169 if (end
[0] == ':' && isdigit(end
[1])) {
170 num
= strtoul(end
+1, &end
, 10);
178 /* Year? Day of month? Numeric date-string?*/
182 if (num
> 0 && num
< 32) {
187 tm
->tm_year
= num
- 1900;
198 if (num
&& num
< 32 && isdigit(end
[1])) {
199 num2
= strtoul(end
+1, &end
, 10);
200 if (!num2
|| num2
> 31)
209 tm
->tm_mon
= num
- 1;
211 if (*end
== c
&& isdigit(end
[1])) {
212 num3
= strtoul(end
+1, &end
, 10);
227 static int match_tz(char *date
, int *offp
)
230 int offset
= strtoul(date
+1, &end
, 10);
236 offset
= hour
*60+min
;
244 /* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
245 (i.e. English) day/month names, and it doesn't work correctly with %z. */
246 void parse_date(char *date
, char *result
, int maxlen
)
252 memset(&tm
, 0, sizeof(tm
));
261 unsigned char c
= *date
;
263 /* Stop at end of string or newline */
268 match
= match_alpha(date
, &tm
, &offset
);
270 match
= match_digit(date
, &tm
, &offset
);
271 else if ((c
== '-' || c
== '+') && isdigit(date
[1]))
272 match
= match_tz(date
, &offset
);
282 /* mktime uses local timezone */
283 then
= my_mktime(&tm
);
285 offset
= (then
- mktime(&tm
)) / 60;
292 snprintf(result
, maxlen
, "%lu %+03d%02d", then
, offset
/60, offset
% 60);
295 void datestamp(char *buf
, int bufsize
)
302 offset
= my_mktime(localtime(&now
)) - now
;
305 snprintf(buf
, bufsize
, "%lu %+05d", now
, offset
/60*100 + offset
%60);