fix Ox.parseDate()

This commit is contained in:
rolux 2011-05-26 09:18:59 +02:00
parent ab6a07d137
commit 13669aec63
3 changed files with 22 additions and 15 deletions

View file

@ -116,6 +116,7 @@ Ox.load('UI', {debug: true, hideScreen: true, showScreen: true, theme: 'modern'}
{name: 'Guy Debord', start: '1931-12-28', end: '1994-11-30', type: 'person'},
{name: 'John Lennon', start: '1940-10-09', end: '1980-12-08', type: 'person'},
{name: 'Jimi Hendrix', start: '1942-11-27', end: '1970-09-18', type: 'person'},
{name: 'Edie Sedgwick', start: '1943-04-20', end: '1971-11-16', type: 'person'},
{name: 'Jim Morrison', start: '1943-12-08', end: '1971-06-03', type: 'person'},
{name: 'Osama bin Laden', start: '1957-03-10', end: '2011-05-02', type: 'person'},
{name: 'Michael Jackson', start: '1958-08-29', end: '2009-06-25', type: 'person'},
@ -147,6 +148,7 @@ Ox.load('UI', {debug: true, hideScreen: true, showScreen: true, theme: 'modern'}
{name: 'American Civil War', start: '1861-04-12', end: '1865-04-09', type: 'other'},
{name: 'Franco-Prussian War', start: '1870-07-19', end: '1871-05-10', type: 'other'},
{name: 'Paris Commune', start: '1871-03-18', end: '1871-05-28', type: 'other'},
{name: 'Haymarket', start: '1886-05-04', end: '1886-05-05', type: 'other'},
{name: 'Titanic', start: '1912-04-15', end: '1912-04-16', type: 'other'},
{name: 'World War One', start: '1914-07-28', end: '1918-11-11', type: 'other'},
{name: 'Battle of Verdun', start: '1916-02-21', end: '1916-12-18', type: 'other'},

View file

@ -161,7 +161,7 @@ Ox.Calendar = function(options, self) {
var date = new Date();
date.setUTCFullYear(Math.floor(i / 12) + 1970);
date.setUTCMonth(Ox.mod(i, 12));
date.setUTCDate(0);
date.setUTCDate(0); // fixme: WTF??
return date;
//*/
//return new Date(Date.UTC(Math.floor(i / 12) + 1970, Ox.mod(i, 12), 1));

View file

@ -1450,20 +1450,28 @@ 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')
3661000
> +Ox.parseDate('1970', true)
0
> Ox.parseDate('50', true).getUTCFullYear()
50
@*/
Ox.parseDate = function(str, utc) {
var def = [, 1, 1, 0, 0, 0];
val = /(\d+)-?(\d+)?-?(\d+)? ?(\d+)?:?(\d+)?:?(\d+)?/(str);
val.shift();
val = val.map(function(v, i) {
return v || def[i];
var date = new Date(),
defaults = [, 1, 1, 0, 0, 0],
values = /(\d+)-?(\d+)?-?(\d+)? ?(\d+)?:?(\d+)?:?(\d+)?/(str);
values.shift();
values = values.map(function(v, i) {
return v || defaults[i];
});
val[1]--;
return utc
? new Date(Date.UTC(val[0], val[1], val[2], val[3], val[4], val[5]))
: new Date(val[0], val[1], val[2], val[3], val[4], val[5])
values[1]--;
[
'FullYear', 'Month', 'Date', 'Hours', 'Minutes', 'Seconds'
].forEach(function(part, i) {
date = Ox['set' + part](date, values[i], utc);
});
return date;
};
//@ Ox.setDate <f> Set the day of a date, optionally UTC
@ -1490,12 +1498,9 @@ Ox.parseDate = function(str, utc) {
return Ox.makeDate(date)['get' + (utc ? 'UTC' : '') + noun]()
}
Ox['set' + noun] = function(date, num, utc) {
// new Date(date) makes a clone, so that
// setSomething() doesn't have side effects
return new Date(
Ox.makeDate(date)
)['set' + (utc ? 'UTC' : '') + noun](num);
// fixme: maybe we _want_ set to have side effects?
Ox.makeDate(date)['set' + (utc ? 'UTC' : '') + noun](num)
);
}
});