]> git.ipfire.org Git - thirdparty/git.git/blame - date.c
[PATCH] Document --stdin, -m, -s, and -v flags to git-diff-tree
[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
68849b54
LT
117static int skip_alpha(const char *date)
118{
119 int i = 0;
120 do {
121 i++;
122 } while (isalpha(date[i]));
123 return i;
124}
125
89967023
LT
126/*
127* Parse month, weekday, or timezone name
128*/
129static int match_alpha(const char *date, struct tm *tm, int *offset)
ecee9d9e 130{
89967023 131 int i;
ecee9d9e 132
89967023
LT
133 for (i = 0; i < 12; i++) {
134 int match = match_string(date, month_names[i]);
135 if (match >= 3) {
136 tm->tm_mon = i;
137 return match;
ecee9d9e 138 }
89967023 139 }
ecee9d9e 140
89967023
LT
141 for (i = 0; i < 7; i++) {
142 int match = match_string(date, weekday_names[i]);
143 if (match >= 3) {
144 tm->tm_wday = i;
145 return match;
146 }
147 }
ecee9d9e 148
89967023
LT
149 for (i = 0; i < NR_TZ; i++) {
150 int match = match_string(date, timezone_names[i].name);
151 if (match >= 3) {
5e2a78a4
LT
152 int off = timezone_names[i].offset;
153
154 /* This is bogus, but we like summer */
155 off += timezone_names[i].dst;
156
92e2311b
LT
157 /* Only use the tz name offset if we don't have anything better */
158 if (*offset == -1)
159 *offset = 60*off;
160
89967023 161 return match;
ecee9d9e
ET
162 }
163 }
ecee9d9e 164
68849b54
LT
165 if (match_string(date, "PM") == 2) {
166 if (tm->tm_hour > 0 && tm->tm_hour < 12)
167 tm->tm_hour += 12;
168 return 2;
169 }
170
89967023 171 /* BAD CRAP */
68849b54 172 return skip_alpha(date);
89967023 173}
ecee9d9e 174
198b0fb6 175static int is_date(int year, int month, int day, struct tm *tm)
89967023 176{
198b0fb6
LT
177 if (month > 0 && month < 13 && day > 0 && day < 32) {
178 if (year == -1) {
179 tm->tm_mon = month-1;
180 tm->tm_mday = day;
181 return 1;
89967023 182 }
198b0fb6
LT
183 if (year >= 1970 && year < 2100) {
184 year -= 1900;
185 } else if (year > 70 && year < 100) {
186 /* ok */
187 } else if (year < 38) {
188 year += 100;
189 } else
190 return 0;
191
192 tm->tm_mon = month-1;
193 tm->tm_mday = day;
194 tm->tm_year = year;
195 return 1;
89967023 196 }
198b0fb6
LT
197 return 0;
198}
ecee9d9e 199
198b0fb6
LT
200static int match_multi_number(unsigned long num, char c, char *date, char *end, struct tm *tm)
201{
202 long num2, num3;
203
204 num2 = strtol(end+1, &end, 10);
205 num3 = -1;
206 if (*end == c && isdigit(end[1]))
207 num3 = strtol(end+1, &end, 10);
208
209 /* Time? Date? */
89967023 210 switch (c) {
198b0fb6
LT
211 case ':':
212 if (num3 < 0)
213 num3 = 0;
214 if (num < 25 && num2 >= 0 && num2 < 60 && num3 >= 0 && num3 <= 60) {
215 tm->tm_hour = num;
216 tm->tm_min = num2;
217 tm->tm_sec = num3;
89967023
LT
218 break;
219 }
198b0fb6 220 return 0;
89967023
LT
221
222 case '-':
223 case '/':
198b0fb6
LT
224 if (num > 70) {
225 /* yyyy-mm-dd? */
226 if (is_date(num, num2, num3, tm))
227 break;
228 /* yyyy-dd-mm? */
229 if (is_date(num, num3, num2, tm))
89967023 230 break;
198b0fb6
LT
231 }
232 /* mm/dd/yy ? */
233 if (is_date(num3, num2, num, tm))
89967023 234 break;
198b0fb6
LT
235 /* dd/mm/yy ? */
236 if (is_date(num3, num, num2, tm))
237 break;
238 return 0;
239 }
240 return end - date;
241}
242
243/*
244 * We've seen a digit. Time? Year? Date?
245 */
246static int match_digit(char *date, struct tm *tm, int *offset)
247{
248 int n;
249 char *end;
250 unsigned long num;
251
252 num = strtoul(date, &end, 10);
253
254 /*
255 * Seconds since 1970? We trigger on that for anything after Jan 1, 2000
256 */
257 if (num > 946684800) {
258 time_t time = num;
259 if (gmtime_r(&time, tm))
260 return end - date;
261 }
262
263 /*
264 * Check for special formats: num[:-/]num[same]num
265 */
266 switch (*end) {
267 case ':':
268 case '/':
269 case '-':
270 if (isdigit(end[1])) {
271 int match = match_multi_number(num, *end, date, end, tm);
272 if (match)
273 return match;
89967023
LT
274 }
275 }
198b0fb6
LT
276
277 /*
278 * None of the special formats? Try to guess what
279 * the number meant. We use the number of digits
280 * to make a more educated guess..
281 */
282 n = 0;
283 do {
284 n++;
285 } while (isdigit(date[n]));
286
287 /* Four-digit year or a timezone? */
288 if (n == 4) {
289 if (num <= 1200 && *offset == -1) {
290 unsigned int minutes = num % 100;
291 unsigned int hours = num / 100;
292 *offset = hours*60 + minutes;
293 } else if (num > 1900 && num < 2100)
294 tm->tm_year = num - 1900;
295 return n;
296 }
297
298 /*
299 * NOTE! We will give precedence to day-of-month over month or
300 * year numebers in the 1-12 range. So 05 is always "mday 5",
301 * unless we already have a mday..
302 *
303 * IOW, 01 Apr 05 parses as "April 1st, 2005".
304 */
305 if (num > 0 && num < 32 && tm->tm_mday < 0) {
306 tm->tm_mday = num;
307 return n;
308 }
309
310 /* Two-digit year? */
311 if (n == 2 && tm->tm_year < 0) {
312 if (num < 10 && tm->tm_mday >= 0) {
313 tm->tm_year = num + 100;
314 return n;
315 }
316 if (num >= 70) {
317 tm->tm_year = num;
318 return n;
319 }
320 }
321
322 if (num > 0 && num < 32) {
323 tm->tm_mday = num;
324 } else if (num > 1900) {
325 tm->tm_year = num - 1900;
326 } else if (num > 70) {
327 tm->tm_year = num;
328 } else if (num > 0 && num < 13) {
329 tm->tm_mon = num-1;
330 }
ecee9d9e 331
198b0fb6 332 return n;
89967023 333}
ecee9d9e 334
89967023
LT
335static int match_tz(char *date, int *offp)
336{
337 char *end;
338 int offset = strtoul(date+1, &end, 10);
339 int min, hour;
198b0fb6 340 int n = end - date - 1;
ecee9d9e 341
89967023
LT
342 min = offset % 100;
343 hour = offset / 100;
ecee9d9e 344
198b0fb6
LT
345 /*
346 * Don't accept any random crap.. At least 3 digits, and
347 * a valid minute. We might want to check that the minutes
348 * are divisible by 30 or something too.
349 */
68849b54
LT
350 if (min < 60 && n > 2) {
351 offset = hour*60+min;
352 if (*date == '-')
353 offset = -offset;
ecee9d9e 354
68849b54
LT
355 *offp = offset;
356 }
89967023
LT
357 return end - date;
358}
ecee9d9e 359
89967023
LT
360/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
361 (i.e. English) day/month names, and it doesn't work correctly with %z. */
362void parse_date(char *date, char *result, int maxlen)
363{
364 struct tm tm;
7f26664f 365 int offset, sign;
89967023 366 time_t then;
ecee9d9e 367
89967023
LT
368 memset(&tm, 0, sizeof(tm));
369 tm.tm_year = -1;
370 tm.tm_mon = -1;
371 tm.tm_mday = -1;
eaa85129
LT
372 tm.tm_isdst = -1;
373 offset = -1;
89967023
LT
374
375 for (;;) {
376 int match = 0;
377 unsigned char c = *date;
378
379 /* Stop at end of string or newline */
380 if (!c || c == '\n')
381 break;
382
383 if (isalpha(c))
384 match = match_alpha(date, &tm, &offset);
385 else if (isdigit(c))
386 match = match_digit(date, &tm, &offset);
387 else if ((c == '-' || c == '+') && isdigit(date[1]))
388 match = match_tz(date, &offset);
389
390 if (!match) {
391 /* BAD CRAP */
392 match = 1;
393 }
394
395 date += match;
396 }
ecee9d9e 397
eaa85129
LT
398 /* mktime uses local timezone */
399 then = my_mktime(&tm);
400 if (offset == -1)
401 offset = (then - mktime(&tm)) / 60;
402
ecee9d9e
ET
403 if (then == -1)
404 return;
405
89967023 406 then -= offset * 60;
ecee9d9e 407
7f26664f
LT
408 sign = '+';
409 if (offset < 0) {
410 offset = -offset;
411 sign = '-';
412 }
413
414 snprintf(result, maxlen, "%lu %c%02d%02d", then, sign, offset/60, offset % 60);
ecee9d9e
ET
415}
416
417void datestamp(char *buf, int bufsize)
418{
419 time_t now;
420 int offset;
421
422 time(&now);
423
424 offset = my_mktime(localtime(&now)) - now;
425 offset /= 60;
426
427 snprintf(buf, bufsize, "%lu %+05d", now, offset/60*100 + offset%60);
428}