Only use timezones when timezone info is specified.

(ie. when neither zulu time or an offset is included, use local time).

Add tests.
This commit is contained in:
Olly Smith 2012-04-04 21:39:59 +01:00
parent 870e0a649a
commit e6e666469d
4 changed files with 81 additions and 43 deletions

View File

@ -382,8 +382,8 @@ Morris.parseDate = (date) ->
n = date.match /^(\d+)-(\d+)$/
o = date.match /^(\d+)-(\d+)-(\d+)$/
p = date.match /^(\d+) W(\d+)$/
q = date.match /^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+)(Z|([+-])(\d+):(\d+))?$/
r = date.match /^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+):(\d+(\.\d+)?)(Z|([+-])(\d+):(\d+))?$/
q = date.match /^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+)(Z|([+-])(\d\d):?(\d\d))?$/
r = date.match /^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+):(\d+(\.\d+)?)(Z|([+-])(\d\d):?(\d\d))?$/
if m
new Date(
parseInt(m[1], 10),
@ -408,32 +408,54 @@ Morris.parseDate = (date) ->
# add weeks
ret.getTime() + parseInt(p[2], 10) * 604800000
else if q
offsetmins = 0
if q[6] != 'Z'
offsetmins = parseInt(q[8], 10) * 60 + parseInt(q[9], 10)
offsetmins = 0 - offsetmins if q[7] == '+'
Date.UTC(
parseInt(q[1], 10),
parseInt(q[2], 10) - 1,
parseInt(q[3], 10),
parseInt(q[4], 10),
parseInt(q[5], 10) + offsetmins)
if q[6] is undefined
# no timezone info, use local
new Date(
parseInt(q[1], 10),
parseInt(q[2], 10) - 1,
parseInt(q[3], 10),
parseInt(q[4], 10),
parseInt(q[5], 10)).getTime()
else
# timezone info supplied, use UTC
offsetmins = 0
if q[6] != 'Z'
offsetmins = parseInt(q[8], 10) * 60 + parseInt(q[9], 10)
offsetmins = 0 - offsetmins if q[7] == '+'
Date.UTC(
parseInt(q[1], 10),
parseInt(q[2], 10) - 1,
parseInt(q[3], 10),
parseInt(q[4], 10),
parseInt(q[5], 10) + offsetmins)
else if r
offsetmins = 0
if r[8] != 'Z'
offsetmins = parseInt(r[10], 10) * 60 + parseInt(r[11], 10)
offsetmins = 0 - offsetmins if r[9] == '+'
secs = parseFloat(r[6])
isecs = Math.floor(secs)
msecs = Math.floor((secs - isecs) * 1000)
Date.UTC(
parseInt(r[1], 10),
parseInt(r[2], 10) - 1,
parseInt(r[3], 10),
parseInt(r[4], 10),
parseInt(r[5], 10) + offsetmins,
isecs,
msecs)
msecs = Math.round((secs - isecs) * 1000)
if r[8] is undefined
# no timezone info, use local
new Date(
parseInt(r[1], 10),
parseInt(r[2], 10) - 1,
parseInt(r[3], 10),
parseInt(r[4], 10),
parseInt(r[5], 10),
isecs,
msecs).getTime()
else
# timezone info supplied, use UTC
offsetmins = 0
if r[8] != 'Z'
offsetmins = parseInt(r[10], 10) * 60 + parseInt(r[11], 10)
offsetmins = 0 - offsetmins if r[9] == '+'
Date.UTC(
parseInt(r[1], 10),
parseInt(r[2], 10) - 1,
parseInt(r[3], 10),
parseInt(r[4], 10),
parseInt(r[5], 10) + offsetmins,
isecs,
msecs)
else
new Date(parseInt(date, 10), 0, 1).getTime()

View File

@ -456,8 +456,8 @@
n = date.match(/^(\d+)-(\d+)$/);
o = date.match(/^(\d+)-(\d+)-(\d+)$/);
p = date.match(/^(\d+) W(\d+)$/);
q = date.match(/^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+)(Z|([+-])(\d+):(\d+))?$/);
r = date.match(/^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+):(\d+(\.\d+)?)(Z|([+-])(\d+):(\d+))?$/);
q = date.match(/^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+)(Z|([+-])(\d\d):?(\d\d))?$/);
r = date.match(/^(\d+)-(\d+)-(\d+)[ T](\d+):(\d+):(\d+(\.\d+)?)(Z|([+-])(\d\d):?(\d\d))?$/);
if (m) {
return new Date(parseInt(m[1], 10), parseInt(m[2], 10) * 3 - 1, 1).getTime();
} else if (n) {
@ -469,22 +469,30 @@
if (ret.getDay() !== 4) ret.setMonth(0, 1 + ((4 - ret.getDay()) + 7) % 7);
return ret.getTime() + parseInt(p[2], 10) * 604800000;
} else if (q) {
offsetmins = 0;
if (q[6] !== 'Z') {
offsetmins = parseInt(q[8], 10) * 60 + parseInt(q[9], 10);
if (q[7] === '+') offsetmins = 0 - offsetmins;
if (q[6] === void 0) {
return new Date(parseInt(q[1], 10), parseInt(q[2], 10) - 1, parseInt(q[3], 10), parseInt(q[4], 10), parseInt(q[5], 10)).getTime();
} else {
offsetmins = 0;
if (q[6] !== 'Z') {
offsetmins = parseInt(q[8], 10) * 60 + parseInt(q[9], 10);
if (q[7] === '+') offsetmins = 0 - offsetmins;
}
return Date.UTC(parseInt(q[1], 10), parseInt(q[2], 10) - 1, parseInt(q[3], 10), parseInt(q[4], 10), parseInt(q[5], 10) + offsetmins);
}
return Date.UTC(parseInt(q[1], 10), parseInt(q[2], 10) - 1, parseInt(q[3], 10), parseInt(q[4], 10), parseInt(q[5], 10) + offsetmins);
} else if (r) {
offsetmins = 0;
if (r[8] !== 'Z') {
offsetmins = parseInt(r[10], 10) * 60 + parseInt(r[11], 10);
if (r[9] === '+') offsetmins = 0 - offsetmins;
}
secs = parseFloat(r[6]);
isecs = Math.floor(secs);
msecs = Math.floor((secs - isecs) * 1000);
return Date.UTC(parseInt(r[1], 10), parseInt(r[2], 10) - 1, parseInt(r[3], 10), parseInt(r[4], 10), parseInt(r[5], 10) + offsetmins, isecs, msecs);
msecs = Math.round((secs - isecs) * 1000);
if (r[8] === void 0) {
return new Date(parseInt(r[1], 10), parseInt(r[2], 10) - 1, parseInt(r[3], 10), parseInt(r[4], 10), parseInt(r[5], 10), isecs, msecs).getTime();
} else {
offsetmins = 0;
if (r[8] !== 'Z') {
offsetmins = parseInt(r[10], 10) * 60 + parseInt(r[11], 10);
if (r[9] === '+') offsetmins = 0 - offsetmins;
}
return Date.UTC(parseInt(r[1], 10), parseInt(r[2], 10) - 1, parseInt(r[3], 10), parseInt(r[4], 10), parseInt(r[5], 10) + offsetmins, isecs, msecs);
}
} else {
return new Date(parseInt(date, 10), 0, 1).getTime();
}

2
morris.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -187,13 +187,21 @@
equal(Morris.parseDate("2012-10-15"), new Date(2012, 9, 15).getTime());
equal(Morris.parseDate("2012-10-15 12:34"), new Date(2012, 9, 15, 12, 34).getTime());
equal(Morris.parseDate("2012-10-15T12:34"), new Date(2012, 9, 15, 12, 34).getTime());
equal(Morris.parseDate("2012-10-15T12:34Z"), new Date(2012, 9, 15, 12, 34).getTime());
equal(Morris.parseDate("2012-10-15T12:34Z"), Date.UTC(2012, 9, 15, 12, 34));
equal(Morris.parseDate("2012-10-15T12:34+0100"), Date.UTC(2012, 9, 15, 11, 34));
equal(Morris.parseDate("2012-10-15T12:34+02:00"), Date.UTC(2012, 9, 15, 10, 34));
equal(Morris.parseDate("2012-10-15T12:34-0100"), Date.UTC(2012, 9, 15, 13, 34));
equal(Morris.parseDate("2012-10-15T12:34-02:00"), Date.UTC(2012, 9, 15, 14, 34));
equal(Morris.parseDate("2012-10-15 12:34:55"), new Date(2012, 9, 15, 12, 34, 55).getTime());
equal(Morris.parseDate("2012-10-15T12:34:55"), new Date(2012, 9, 15, 12, 34, 55).getTime());
equal(Morris.parseDate("2012-10-15T12:34:55Z"), new Date(2012, 9, 15, 12, 34, 55).getTime());
equal(Morris.parseDate("2012-10-15T12:34:55Z"), Date.UTC(2012, 9, 15, 12, 34, 55));
equal(Morris.parseDate("2012-10-15T12:34:55+0600"), Date.UTC(2012, 9, 15, 6, 34, 55));
equal(Morris.parseDate("2012-10-15T12:34:55+04:00"), Date.UTC(2012, 9, 15, 8, 34, 55));
equal(Morris.parseDate("2012-10-15T12:34:55-0600"), Date.UTC(2012, 9, 15, 18, 34, 55));
equal(Morris.parseDate("2012-10-15T12:34:55-04:00"), Date.UTC(2012, 9, 15, 16, 34, 55));
equal(Morris.parseDate("2012-10-15 12:34:55.123"), new Date(2012, 9, 15, 12, 34, 55, 123).getTime());
equal(Morris.parseDate("2012-10-15T12:34:55.123"), new Date(2012, 9, 15, 12, 34, 55, 123).getTime());
equal(Morris.parseDate("2012-10-15T12:34:55.123Z"), new Date(2012, 9, 15, 12, 34, 55, 123).getTime());
equal(Morris.parseDate("2012-10-15T12:34:55.123Z"), Date.UTC(2012, 9, 15, 12, 34, 55, 123));
equal(Morris.parseDate(new Date(2012, 9, 15, 12, 34, 55, 123).getTime()),
new Date(2012, 9, 15, 12, 34, 55, 123).getTime());
});