Added support for more precise timestamps using Date.parse

This commit is contained in:
Paul Rosenzweig 2012-03-09 01:16:49 -06:00
parent 5ee41e16e2
commit befa798b88
3 changed files with 14 additions and 31 deletions

View File

@ -308,8 +308,6 @@ class Morris.Line
parseYear: (date) ->
s = date.toString()
m = s.match /^(\d+) Q(\d)$/
n = s.match /^(\d+)-(\d+)$/
o = s.match /^(\d+)-(\d+)-(\d+)$/
p = s.match /^(\d+) W(\d+)$/
if m
parseInt(m[1], 10) + (parseInt(m[2], 10) * 3 - 1) / 12
@ -327,21 +325,13 @@ class Morris.Line
# Number of weeks between thursdays
weeks = Math.ceil((y2 - y1) / 604800000);
parseInt(p[1], 10) + (parseInt(p[2], 10) - 1) / weeks;
else if n
parseInt(n[1], 10) + (parseInt(n[2], 10) - 1) / 12
else if o
# parse to a timestamp
year = parseInt(o[1], 10);
month = parseInt(o[2], 10);
day = parseInt(o[3], 10);
timestamp = new Date(year, month - 1, day).getTime();
# get timestamps for the beginning and end of the year
y1 = new Date(year, 0, 1).getTime();
y2 = new Date(year+1, 0, 1).getTime();
# calculate a decimal-year value
year + (timestamp - y1) / (y2 - y1);
else
parseInt(date, 10)
d = Date.parse(s)
year = new Date(d).getFullYear()
y1 = Date.parse(year)
y2 = Date.parse(year+1)
portionOfThisYear = (d - y1) / (y2 - y1)
year + portionOfThisYear
# make long numbers prettier by inserting commas
# eg: commas(1234567) -> '1,234,567'

View File

@ -343,11 +343,9 @@
};
Line.prototype.parseYear = function(date) {
var day, m, month, n, o, p, s, timestamp, weeks, y1, y2, year;
var d, m, p, portionOfThisYear, s, weeks, y1, y2, year;
s = date.toString();
m = s.match(/^(\d+) Q(\d)$/);
n = s.match(/^(\d+)-(\d+)$/);
o = s.match(/^(\d+)-(\d+)-(\d+)$/);
p = s.match(/^(\d+) W(\d+)$/);
if (m) {
return parseInt(m[1], 10) + (parseInt(m[2], 10) * 3 - 1) / 12;
@ -359,18 +357,13 @@
if (y2.getDay() !== 4) y2.setMonth(0, 1 + ((4 - y2.getDay()) + 7) % 7);
weeks = Math.ceil((y2 - y1) / 604800000);
return parseInt(p[1], 10) + (parseInt(p[2], 10) - 1) / weeks;
} else if (n) {
return parseInt(n[1], 10) + (parseInt(n[2], 10) - 1) / 12;
} else if (o) {
year = parseInt(o[1], 10);
month = parseInt(o[2], 10);
day = parseInt(o[3], 10);
timestamp = new Date(year, month - 1, day).getTime();
y1 = new Date(year, 0, 1).getTime();
y2 = new Date(year + 1, 0, 1).getTime();
return year + (timestamp - y1) / (y2 - y1);
} else {
return parseInt(date, 10);
d = Date.parse(s);
year = new Date(d).getFullYear();
y1 = Date.parse(year);
y2 = Date.parse(year + 1);
portionOfThisYear = (d - y1) / (y2 - y1);
return year + portionOfThisYear;
}
};

2
morris.min.js vendored

File diff suppressed because one or more lines are too long