]> git.ipfire.org Git - thirdparty/git.git/blame - date.c
date.c: allow even more varied time formats
[thirdparty/git.git] / date.c
CommitLineData
ecee9d9e
ET
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <ctype.h>
11#include <time.h>
12
13static time_t my_mktime(struct tm *tm)
14{
15 static const int mdays[] = {
16 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
17 };
18 int year = tm->tm_year - 70;
19 int month = tm->tm_mon;
20 int day = tm->tm_mday;
21
22 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
23 return -1;
24 if (month < 0 || month > 11) /* array bounds */
25 return -1;
26 if (month < 2 || (year + 2) % 4)
27 day--;
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;
30}
31
32static const char *month_names[] = {
89967023
LT
33 "January", "February", "March", "April", "May", "June",
34 "July", "August", "September", "October", "November", "December"
ecee9d9e
ET
35};
36
37static const char *weekday_names[] = {
89967023 38 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
ecee9d9e
ET
39};
40
89967023
LT
41/*
42 * Check these. And note how it doesn't do the summer-time conversion.
43 *
44 * In my world, it's always summer, and things are probably a bit off
45 * in other ways too.
46 */
47static const struct {
48 const char *name;
49 int offset;
5e2a78a4 50 int dst;
89967023 51} timezone_names[] = {
5e2a78a4
LT
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 */
70
71 { "GMT", 0, 0, }, /* Greenwich Mean */
72 { "UTC", 0, 0, }, /* Universal (Coordinated) */
73
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 */
92e2311b 85 { "EEST", +2, 1, }, /* Eastern European Daylight */
5e2a78a4
LT
86 { "WAST", +7, 0, }, /* West Australian Standard */
87 { "WADT", +7, 1, }, /* West Australian Daylight */
88 { "CCT", +8, 0, }, /* China Coast, USSR Zone 7 */
89 { "JST", +9, 0, }, /* Japan Standard, USSR Zone 8 */
90 { "EAST", +10, 0, }, /* Eastern Australian Standard */
91 { "EADT", +10, 1, }, /* Eastern Australian Daylight */
92 { "GST", +10, 0, }, /* Guam Standard, USSR Zone 9 */
93 { "NZT", +11, 0, }, /* New Zealand */
94 { "NZST", +11, 0, }, /* New Zealand Standard */
95 { "NZDT", +11, 1, }, /* New Zealand Daylight */
96 { "IDLE", +12, 0, }, /* International Date Line East */
89967023 97};
ecee9d9e 98
89967023
LT
99#define NR_TZ (sizeof(timezone_names) / sizeof(timezone_names[0]))
100
101static int match_string(const char *date, const char *str)
ecee9d9e 102{
89967023
LT
103 int i = 0;
104
105 for (i = 0; *date; date++, str++, i++) {
106 if (*date == *str)
107 continue;
108 if (toupper(*date) == toupper(*str))
109 continue;
110 if (!isalnum(*date))
111 break;
112 return 0;
113 }
114 return i;
ecee9d9e
ET
115}
116
89967023
LT
117/*
118* Parse month, weekday, or timezone name
119*/
120static int match_alpha(const char *date, struct tm *tm, int *offset)
ecee9d9e 121{
89967023 122 int i;
ecee9d9e 123
89967023
LT
124 for (i = 0; i < 12; i++) {
125 int match = match_string(date, month_names[i]);
126 if (match >= 3) {
127 tm->tm_mon = i;
128 return match;
ecee9d9e 129 }
89967023 130 }
ecee9d9e 131
89967023
LT
132 for (i = 0; i < 7; i++) {
133 int match = match_string(date, weekday_names[i]);
134 if (match >= 3) {
135 tm->tm_wday = i;
136 return match;
137 }
138 }
ecee9d9e 139
89967023
LT
140 for (i = 0; i < NR_TZ; i++) {
141 int match = match_string(date, timezone_names[i].name);
142 if (match >= 3) {
5e2a78a4
LT
143 int off = timezone_names[i].offset;
144
145 /* This is bogus, but we like summer */
146 off += timezone_names[i].dst;
147
92e2311b
LT
148 /* Only use the tz name offset if we don't have anything better */
149 if (*offset == -1)
150 *offset = 60*off;
151
89967023 152 return match;
ecee9d9e
ET
153 }
154 }
ecee9d9e 155
89967023
LT
156 /* BAD CRAP */
157 return 0;
158}
ecee9d9e 159
198b0fb6 160static int is_date(int year, int month, int day, struct tm *tm)
89967023 161{
198b0fb6
LT
162 if (month > 0 && month < 13 && day > 0 && day < 32) {
163 if (year == -1) {
164 tm->tm_mon = month-1;
165 tm->tm_mday = day;
166 return 1;
89967023 167 }
198b0fb6
LT
168 if (year >= 1970 && year < 2100) {
169 year -= 1900;
170 } else if (year > 70 && year < 100) {
171 /* ok */
172 } else if (year < 38) {
173 year += 100;
174 } else
175 return 0;
176
177 tm->tm_mon = month-1;
178 tm->tm_mday = day;
179 tm->tm_year = year;
180 return 1;
89967023 181 }
198b0fb6
LT
182 return 0;
183}
ecee9d9e 184
198b0fb6
LT
185static int match_multi_number(unsigned long num, char c, char *date, char *end, struct tm *tm)
186{
187 long num2, num3;
188
189 num2 = strtol(end+1, &end, 10);
190 num3 = -1;
191 if (*end == c && isdigit(end[1]))
192 num3 = strtol(end+1, &end, 10);
193
194 /* Time? Date? */
89967023 195 switch (c) {
198b0fb6
LT
196 case ':':
197 if (num3 < 0)
198 num3 = 0;
199 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
200 tm->tm_hour = num;
201 tm->tm_min = num2;
202 tm->tm_sec = num3;
89967023
LT
203 break;
204 }
198b0fb6 205 return 0;
89967023
LT
206
207 case '-':
208 case '/':
198b0fb6
LT
209 if (num > 70) {
210 /* yyyy-mm-dd? */
211 if (is_date(num, num2, num3, tm))
212 break;
213 /* yyyy-dd-mm? */
214 if (is_date(num, num3, num2, tm))
89967023 215 break;
198b0fb6
LT
216 }
217 /* mm/dd/yy ? */
218 if (is_date(num3, num2, num, tm))
89967023 219 break;
198b0fb6
LT
220 /* dd/mm/yy ? */
221 if (is_date(num3, num, num2, tm))
222 break;
223 return 0;
224 }
225 return end - date;
226}
227
228/*
229 * We've seen a digit. Time? Year? Date?
230 */
231static int match_digit(char *date, struct tm *tm, int *offset)
232{
233 int n;
234 char *end;
235 unsigned long num;
236
237 num = strtoul(date, &end, 10);
238
239 /*
240 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
241 */
242 if (num > 946684800) {
243 time_t time = num;
244 if (gmtime_r(&time, tm))
245 return end - date;
246 }
247
248 /*
249 * Check for special formats: num[:-/]num[same]num
250 */
251 switch (*end) {
252 case ':':
253 case '/':
254 case '-':
255 if (isdigit(end[1])) {
256 int match = match_multi_number(num, *end, date, end, tm);
257 if (match)
258 return match;
89967023
LT
259 }
260 }
198b0fb6
LT
261
262 /*
263 * None of the special formats? Try to guess what
264 * the number meant. We use the number of digits
265 * to make a more educated guess..
266 */
267 n = 0;
268 do {
269 n++;
270 } while (isdigit(date[n]));
271
272 /* Four-digit year or a timezone? */
273 if (n == 4) {
274 if (num <= 1200 && *offset == -1) {
275 unsigned int minutes = num % 100;
276 unsigned int hours = num / 100;
277 *offset = hours*60 + minutes;
278 } else if (num > 1900 && num < 2100)
279 tm->tm_year = num - 1900;
280 return n;
281 }
282
283 /*
284 * NOTE! We will give precedence to day-of-month over month or
285 * year numebers in the 1-12 range. So 05 is always "mday 5",
286 * unless we already have a mday..
287 *
288 * IOW, 01 Apr 05 parses as "April 1st, 2005".
289 */
290 if (num > 0 && num < 32 && tm->tm_mday < 0) {
291 tm->tm_mday = num;
292 return n;
293 }
294
295 /* Two-digit year? */
296 if (n == 2 && tm->tm_year < 0) {
297 if (num < 10 && tm->tm_mday >= 0) {
298 tm->tm_year = num + 100;
299 return n;
300 }
301 if (num >= 70) {
302 tm->tm_year = num;
303 return n;
304 }
305 }
306
307 if (num > 0 && num < 32) {
308 tm->tm_mday = num;
309 } else if (num > 1900) {
310 tm->tm_year = num - 1900;
311 } else if (num > 70) {
312 tm->tm_year = num;
313 } else if (num > 0 && num < 13) {
314 tm->tm_mon = num-1;
315 }
ecee9d9e 316
198b0fb6 317 return n;
89967023 318}
ecee9d9e 319
89967023
LT
320static int match_tz(char *date, int *offp)
321{
322 char *end;
323 int offset = strtoul(date+1, &end, 10);
324 int min, hour;
198b0fb6 325 int n = end - date - 1;
ecee9d9e 326
89967023
LT
327 min = offset % 100;
328 hour = offset / 100;
ecee9d9e 329
198b0fb6
LT
330 /*
331 * Don't accept any random crap.. At least 3 digits, and
332 * a valid minute. We might want to check that the minutes
333 * are divisible by 30 or something too.
334 */
335 if (min >= 60 || n < 3)
336 return 0;
337
89967023
LT
338 offset = hour*60+min;
339 if (*date == '-')
340 offset = -offset;
ecee9d9e 341
89967023
LT
342 *offp = offset;
343 return end - date;
344}
ecee9d9e 345
89967023
LT
346/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
347 (i.e. English) day/month names, and it doesn't work correctly with %z. */
348void parse_date(char *date, char *result, int maxlen)
349{
350 struct tm tm;
7f26664f 351 int offset, sign;
89967023 352 time_t then;
ecee9d9e 353
89967023
LT
354 memset(&tm, 0, sizeof(tm));
355 tm.tm_year = -1;
356 tm.tm_mon = -1;
357 tm.tm_mday = -1;
eaa85129
LT
358 tm.tm_isdst = -1;
359 offset = -1;
89967023
LT
360
361 for (;;) {
362 int match = 0;
363 unsigned char c = *date;
364
365 /* Stop at end of string or newline */
366 if (!c || c == '\n')
367 break;
368
369 if (isalpha(c))
370 match = match_alpha(date, &tm, &offset);
371 else if (isdigit(c))
372 match = match_digit(date, &tm, &offset);
373 else if ((c == '-' || c == '+') && isdigit(date[1]))
374 match = match_tz(date, &offset);
375
376 if (!match) {
377 /* BAD CRAP */
378 match = 1;
379 }
380
381 date += match;
382 }
ecee9d9e 383
eaa85129
LT
384 /* mktime uses local timezone */
385 then = my_mktime(&tm);
386 if (offset == -1)
387 offset = (then - mktime(&tm)) / 60;
388
ecee9d9e
ET
389 if (then == -1)
390 return;
391
89967023 392 then -= offset * 60;
ecee9d9e 393
7f26664f
LT
394 sign = '+';
395 if (offset < 0) {
396 offset = -offset;
397 sign = '-';
398 }
399
400 snprintf(result, maxlen, "%lu %c%02d%02d", then, sign, offset/60, offset % 60);
ecee9d9e
ET
401}
402
403void datestamp(char *buf, int bufsize)
404{
405 time_t now;
406 int offset;
407
408 time(&now);
409
410 offset = my_mktime(localtime(&now)) - now;
411 offset /= 60;
412
413 snprintf(buf, bufsize, "%lu %+05d", now, offset/60*100 + offset%60);
414}