]> git.ipfire.org Git - thirdparty/git.git/blame - date.c
[PATCH] Allow removing files in a subdirectory.
[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
89967023
LT
160static int match_digit(char *date, struct tm *tm, int *offset)
161{
162 char *end, c;
163 unsigned long num, num2, num3;
164
165 num = strtoul(date, &end, 10);
166
167 /* Time? num:num[:num] */
168 if (num < 24 && end[0] == ':' && isdigit(end[1])) {
169 tm->tm_hour = num;
170 num = strtoul(end+1, &end, 10);
171 if (num < 60) {
172 tm->tm_min = num;
173 if (end[0] == ':' && isdigit(end[1])) {
174 num = strtoul(end+1, &end, 10);
175 if (num < 61)
176 tm->tm_sec = num;
177 }
178 }
179 return end - date;
180 }
ecee9d9e 181
89967023
LT
182 /* Year? Day of month? Numeric date-string?*/
183 c = *end;
184 switch (c) {
185 default:
186 if (num > 0 && num < 32) {
187 tm->tm_mday = num;
188 break;
189 }
190 if (num > 1900) {
191 tm->tm_year = num - 1900;
192 break;
193 }
194 if (num > 70) {
195 tm->tm_year = num;
196 break;
197 }
198 break;
199
200 case '-':
201 case '/':
202 if (num && num < 32 && isdigit(end[1])) {
203 num2 = strtoul(end+1, &end, 10);
204 if (!num2 || num2 > 31)
205 break;
206 if (num > 12) {
207 if (num2 > 12)
208 break;
209 num3 = num;
210 num = num2;
211 num2 = num3;
212 }
213 tm->tm_mon = num - 1;
214 tm->tm_mday = num2;
215 if (*end == c && isdigit(end[1])) {
a9058882 216 num3 = strtoul(end+1, &end, 10);
89967023
LT
217 if (num3 > 1900)
218 num3 -= 1900;
a9058882
LT
219 else if (num3 < 38)
220 num3 += 100;
89967023
LT
221 tm->tm_year = num3;
222 }
223 break;
224 }
225 }
ecee9d9e 226
89967023
LT
227 return end - date;
228
229}
ecee9d9e 230
89967023
LT
231static int match_tz(char *date, int *offp)
232{
233 char *end;
234 int offset = strtoul(date+1, &end, 10);
235 int min, hour;
ecee9d9e 236
89967023
LT
237 min = offset % 100;
238 hour = offset / 100;
ecee9d9e 239
89967023
LT
240 offset = hour*60+min;
241 if (*date == '-')
242 offset = -offset;
ecee9d9e 243
89967023
LT
244 *offp = offset;
245 return end - date;
246}
ecee9d9e 247
89967023
LT
248/* Gr. strptime is crap for this; it doesn't have a way to require RFC2822
249 (i.e. English) day/month names, and it doesn't work correctly with %z. */
250void parse_date(char *date, char *result, int maxlen)
251{
252 struct tm tm;
7f26664f 253 int offset, sign;
89967023 254 time_t then;
ecee9d9e 255
89967023
LT
256 memset(&tm, 0, sizeof(tm));
257 tm.tm_year = -1;
258 tm.tm_mon = -1;
259 tm.tm_mday = -1;
eaa85129
LT
260 tm.tm_isdst = -1;
261 offset = -1;
89967023
LT
262
263 for (;;) {
264 int match = 0;
265 unsigned char c = *date;
266
267 /* Stop at end of string or newline */
268 if (!c || c == '\n')
269 break;
270
271 if (isalpha(c))
272 match = match_alpha(date, &tm, &offset);
273 else if (isdigit(c))
274 match = match_digit(date, &tm, &offset);
275 else if ((c == '-' || c == '+') && isdigit(date[1]))
276 match = match_tz(date, &offset);
277
278 if (!match) {
279 /* BAD CRAP */
280 match = 1;
281 }
282
283 date += match;
284 }
ecee9d9e 285
eaa85129
LT
286 /* mktime uses local timezone */
287 then = my_mktime(&tm);
288 if (offset == -1)
289 offset = (then - mktime(&tm)) / 60;
290
ecee9d9e
ET
291 if (then == -1)
292 return;
293
89967023 294 then -= offset * 60;
ecee9d9e 295
7f26664f
LT
296 sign = '+';
297 if (offset < 0) {
298 offset = -offset;
299 sign = '-';
300 }
301
302 snprintf(result, maxlen, "%lu %c%02d%02d", then, sign, offset/60, offset % 60);
ecee9d9e
ET
303}
304
305void datestamp(char *buf, int bufsize)
306{
307 time_t now;
308 int offset;
309
310 time(&now);
311
312 offset = my_mktime(localtime(&now)) - now;
313 offset /= 60;
314
315 snprintf(buf, bufsize, "%lu %+05d", now, offset/60*100 + offset%60);
316}