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