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>
This commit is contained in:
parent
f696543dad
commit
ee646eb48f
34
date.c
34
date.c
@ -552,23 +552,35 @@ static int match_digit(const char *date, struct tm *tm, int *offset, int *tm_gmt
|
|||||||
static int match_tz(const char *date, int *offp)
|
static int match_tz(const char *date, int *offp)
|
||||||
{
|
{
|
||||||
char *end;
|
char *end;
|
||||||
int offset = strtoul(date+1, &end, 10);
|
int hour = strtoul(date + 1, &end, 10);
|
||||||
int min, hour;
|
int n = end - (date + 1);
|
||||||
int n = end - date - 1;
|
int min = 0;
|
||||||
|
|
||||||
min = offset % 100;
|
if (n == 4) {
|
||||||
hour = offset / 100;
|
/* hhmm */
|
||||||
|
min = hour % 100;
|
||||||
|
hour = hour / 100;
|
||||||
|
} else if (n != 2) {
|
||||||
|
min = 99; /* random crap */
|
||||||
|
} else if (*end == ':') {
|
||||||
|
/* hh:mm? */
|
||||||
|
min = strtoul(end + 1, &end, 10);
|
||||||
|
if (end - (date + 1) != 5)
|
||||||
|
min = 99; /* random crap */
|
||||||
|
} /* otherwise we parsed "hh" */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Don't accept any random crap.. At least 3 digits, and
|
* Don't accept any random crap. Even though some places have
|
||||||
* a valid minute. We might want to check that the minutes
|
* offset larger than 12 hours (e.g. Pacific/Kiritimati is at
|
||||||
* are divisible by 30 or something too.
|
* UTC+14), there is something wrong if hour part is much
|
||||||
|
* larger than that. We might also want to check that the
|
||||||
|
* minutes are divisible by 15 or something too. (Offset of
|
||||||
|
* Kathmandu, Nepal is UTC+5:45)
|
||||||
*/
|
*/
|
||||||
if (min < 60 && n > 2) {
|
if (min < 60 && hour < 24) {
|
||||||
offset = hour*60+min;
|
int offset = hour * 60 + min;
|
||||||
if (*date == '-')
|
if (*date == '-')
|
||||||
offset = -offset;
|
offset = -offset;
|
||||||
|
|
||||||
*offp = offset;
|
*offp = offset;
|
||||||
}
|
}
|
||||||
return end - date;
|
return end - date;
|
||||||
|
@ -40,6 +40,12 @@ check_parse 2008-02 bad
|
|||||||
check_parse 2008-02-14 bad
|
check_parse 2008-02-14 bad
|
||||||
check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 +0000'
|
check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 +0000'
|
||||||
check_parse '2008-02-14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
|
check_parse '2008-02-14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
|
||||||
|
check_parse '2008-02-14 20:30:45 -0015' '2008-02-14 20:30:45 -0015'
|
||||||
|
check_parse '2008-02-14 20:30:45 -5' '2008-02-14 20:30:45 +0000'
|
||||||
|
check_parse '2008-02-14 20:30:45 -5:' '2008-02-14 20:30:45 +0000'
|
||||||
|
check_parse '2008-02-14 20:30:45 -05' '2008-02-14 20:30:45 -0500'
|
||||||
|
check_parse '2008-02-14 20:30:45 -:30' '2008-02-14 20:30:45 +0000'
|
||||||
|
check_parse '2008-02-14 20:30:45 -05:00' '2008-02-14 20:30:45 -0500'
|
||||||
check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 -0500' EST5
|
check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 -0500' EST5
|
||||||
|
|
||||||
check_approxidate() {
|
check_approxidate() {
|
||||||
|
Loading…
Reference in New Issue
Block a user