@matheusbsbs / matheus-bagliano / commits / 38035cf4a5
Fork 0

date parsing: be friendlier to our European friends.

This does three things, only applies to cases where the user manually tries to override the author/commit time by environment variables, with non-ISO, non-2822 format date-string: - Refuses to use the interpretation to put the date in the future; recent kernel history has a commit made with 10/03/2006 which is recorded as October 3rd. - Adds '.' as the possible year-month-date separator. We learned from our European friends on the #git channel that dd.mm.yyyy is the norm there. - When the separator is '.', we prefer dd.mm.yyyy over mm.dd.yyyy; otherwise mm/dd/yy[yy] takes precedence over dd/mm/yy[yy]. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 5, 2006 at 15:31 UTC 38035cf4a51c48cccf6c5e3977130261bc0c03a7
1 file changed +56 -21
date.c
+56 -21
@@ -197,26 +197,43 @@ static int match_alpha(const char *date, struct tm *tm, int *offset)
197 return skip_alpha(date);
198 }
199
200 -static int is_date(int year, int month, int day, struct tm *tm)
200 +static int is_date(int year, int month, int day, struct tm *now_tm, time_t now, struct tm *tm)
201 {
202 if (month > 0 && month < 13 && day > 0 && day < 32) {
203 + struct tm check = *tm;
204 + struct tm *r = (now_tm ? &check : tm);
205 + time_t specified;
206 +
207 + r->tm_mon = month - 1;
208 + r->tm_mday = day;
209 if (year == -1) {
204 - tm->tm_mon = month-1;
205 - tm->tm_mday = day;
206 - return 1;
210 + if (!now_tm)
211 + return 1;
212 + r->tm_year = now_tm->tm_year;
213 }
208 - if (year >= 1970 && year < 2100) {
209 - year -= 1900;
210 - } else if (year > 70 && year < 100) {
211 - /* ok */
212 - } else if (year < 38) {
213 - year += 100;
214 - } else
214 + else if (year >= 1970 && year < 2100)
215 + r->tm_year = year - 1900;
216 + else if (year > 70 && year < 100)
217 + r->tm_year = year;
218 + else if (year < 38)
219 + r->tm_year = year + 100;
220 + else
221 return 0;
222 + if (!now_tm)
223 + return 1;
224 +
225 + specified = my_mktime(r);
226
217 - tm->tm_mon = month-1;
218 - tm->tm_mday = day;
219 - tm->tm_year = year;
227 + /* Be it commit time or author time, it does not make
228 + * sense to specify timestamp way into the future. Make
229 + * sure it is not later than ten days from now...
230 + */
231 + if (now + 10*24*3600 < specified)
232 + return 0;
233 + tm->tm_mon = r->tm_mon;
234 + tm->tm_mday = r->tm_mday;
235 + if (year != -1)
236 + tm->tm_year = r->tm_year;
237 return 1;
238 }
239 return 0;
@@ -224,6 +241,9 @@ static int is_date(int year, int month, int day, struct tm *tm)
241
242 static int match_multi_number(unsigned long num, char c, const char *date, char *end, struct tm *tm)
243 {
244 + time_t now;
245 + struct tm now_tm;
246 + struct tm *refuse_future;
247 long num2, num3;
248
249 num2 = strtol(end+1, &end, 10);
@@ -246,19 +266,33 @@ static int match_multi_number(unsigned long num, char c, const char *date, char
266
267 case '-':
268 case '/':
269 + case '.':
270 + now = time(NULL);
271 + refuse_future = NULL;
272 + if (gmtime_r(&now, &now_tm))
273 + refuse_future = &now_tm;
274 +
275 if (num > 70) {
276 /* yyyy-mm-dd? */
251 - if (is_date(num, num2, num3, tm))
277 + if (is_date(num, num2, num3, refuse_future, now, tm))
278 break;
279 /* yyyy-dd-mm? */
254 - if (is_date(num, num3, num2, tm))
280 + if (is_date(num, num3, num2, refuse_future, now, tm))
281 break;
282 }
257 - /* mm/dd/yy ? */
258 - if (is_date(num3, num, num2, tm))
283 + /* Our eastern European friends say dd.mm.yy[yy]
284 + * is the norm there, so giving precedence to
285 + * mm/dd/yy[yy] form only when separator is not '.'
286 + */
287 + if (c != '.' &&
288 + is_date(num3, num, num2, refuse_future, now, tm))
289 + break;
290 + /* European dd.mm.yy[yy] or funny US dd/mm/yy[yy] */
291 + if (is_date(num3, num2, num, refuse_future, now, tm))
292 break;
260 - /* dd/mm/yy ? */
261 - if (is_date(num3, num2, num, tm))
293 + /* Funny European mm.dd.yy */
294 + if (c == '.' &&
295 + is_date(num3, num, num2, refuse_future, now, tm))
296 break;
297 return 0;
298 }
@@ -288,10 +322,11 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
322 }
323
324 /*
291 - * Check for special formats: num[:-/]num[same]num
325 + * Check for special formats: num[-.:/]num[same]num
326 */
327 switch (*end) {
328 case ':':
329 + case '.':
330 case '/':
331 case '-':
332 if (isdigit(end[1])) {