date.c: Support iso8601 timezone formats
Timezone designators in the following formats are all valid according to ISO8601:2004, section 4.3.2: [+-]hh, [+-]hhmm, [+-]hh:mm but we have ignored the ones with colon so far. Signed-off-by: Haitao Li <lihaitao@gmail.com> Helped-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Haitao Li committed
Sep 9, 2011 at 18:10 UTC
ee646eb48f9a7fc6c225facf2b7449a8a65ef8f2
2 files changed
+29
-11
date.c
+23
-11
@@ -552,23 +552,35 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
552
static int match_tz(const char *date, int *offp)
553
{
554
char *end;
555
- int offset = strtoul(date+1, &end, 10);
556
- int min, hour;
557
- int n = end - date - 1;
555
+ int hour = strtoul(date + 1, &end, 10);
556
+ int n = end - (date + 1);
557
+ int min = 0;
558
559
- min = offset % 100;
560
- hour = offset / 100;
559
+ if (n == 4) {
560
+ /* hhmm */
561
+ min = hour % 100;
562
+ hour = hour / 100;
563
+ } else if (n != 2) {
564
+ min = 99; /* random crap */
565
+ } else if (*end == ':') {
566
+ /* hh:mm? */
567
+ min = strtoul(end + 1, &end, 10);
568
+ if (end - (date + 1) != 5)
569
+ min = 99; /* random crap */
570
+ } /* otherwise we parsed "hh" */
571
572
/*
563
- * Don't accept any random crap.. At least 3 digits, and
564
- * a valid minute. We might want to check that the minutes
565
- * are divisible by 30 or something too.
573
+ * Don't accept any random crap. Even though some places have
574
+ * offset larger than 12 hours (e.g. Pacific/Kiritimati is at
575
+ * UTC+14), there is something wrong if hour part is much
576
+ * larger than that. We might also want to check that the
577
+ * minutes are divisible by 15 or something too. (Offset of
578
+ * Kathmandu, Nepal is UTC+5:45)
579
*/
567
- if (min < 60 && n > 2) {
568
- offset = hour*60+min;
580
+ if (min < 60 && hour < 24) {
581
+ int offset = hour * 60 + min;
582
if (*date == '-')
583
offset = -offset;
571
-
584
*offp = offset;
585
}
586
return end - date;
t/t0006-date.sh
+6
@@ -40,6 +40,12 @@ check_parse 2008-02 bad
40
check_parse 2008-02-14 bad
41
check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 +0000'
42
check_parse '2008-02-14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
43
+check_parse '2008-02-14 20:30:45 -0015' '2008-02-14 20:30:45 -0015'
44
+check_parse '2008-02-14 20:30:45 -5' '2008-02-14 20:30:45 +0000'
45
+check_parse '2008-02-14 20:30:45 -5:' '2008-02-14 20:30:45 +0000'
46
+check_parse '2008-02-14 20:30:45 -05' '2008-02-14 20:30:45 -0500'
47
+check_parse '2008-02-14 20:30:45 -:30' '2008-02-14 20:30:45 +0000'
48
+check_parse '2008-02-14 20:30:45 -05:00' '2008-02-14 20:30:45 -0500'
49
check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 -0500' EST5
50
51
check_approxidate() {