allow for passing milliseconds to Ox.parseDate

This commit is contained in:
rolux 2012-06-04 12:02:29 +02:00
parent 8581a3da3a
commit f0ba793a85

View file

@ -265,29 +265,30 @@ Ox.makeYear = function(date, utc) {
}; };
/*@ /*@
Ox.parseDate <f> Takes a string ('YYYY-MM-DD HH:MM:SS') and returns a date Ox.parseDate <f> Takes a string ('YYYY-MM-DD HH:MM:SS.MMM') and returns a date
str <s> string string <s> String
utc <b|false> If true, Date is UTC utc <b|false> If true, date is UTC
> +Ox.parseDate('1970-01-01 01:01:01', true) > +Ox.parseDate('1970-01-01 01:01:01.01', true)
3661000 3661010
> +Ox.parseDate('1970', true) > +Ox.parseDate('1970', true)
0 0
> Ox.parseDate('50', true).getUTCFullYear() > Ox.parseDate('50', true).getUTCFullYear()
50 50
@*/ @*/
// FIXME: Why no milliseconds? Ox.parseDate = function(string, utc) {
Ox.parseDate = function(str, utc) { var date,
var date = new Date(0), defaults = [, 1, 1, 0, 0, 0, 0],
defaults = [, 1, 1, 0, 0, 0], values = /(-?\d+)-?(\d+)?-?(\d+)? ?(\d+)?:?(\d+)?:?(\d+)?\.?(\d+)?/
values = /(-?\d+)-?(\d+)?-?(\d+)? ?(\d+)?:?(\d+)?:?(\d+)?/.exec(str); .exec(string).slice(1);
if (values) { if (values) {
values.shift(); date = new Date();
values = values.map(function(v, i) { values = values.map(function(v, i) {
return v || defaults[i]; return v ? (i == 6 ? Ox.pad(v, 3, '0') : v) : defaults[i];
}); });
values[1]--; values[1]--;
[ [
'FullYear', 'Month', 'Date', 'Hours', 'Minutes', 'Seconds' 'FullYear', 'Month', 'Date',
'Hours', 'Minutes', 'Seconds', 'Milliseconds'
].forEach(function(part, i) { ].forEach(function(part, i) {
Ox['set' + part](date, values[i], utc); Ox['set' + part](date, values[i], utc);
}); });