From 13669aec6399dccc55d532f75d0824ca54dffd0b Mon Sep 17 00:00:00 2001 From: rolux Date: Thu, 26 May 2011 09:18:59 +0200 Subject: [PATCH] fix Ox.parseDate() --- demos/calendar/js/calendar.js | 2 ++ source/Ox.UI/js/Calendar/Ox.Calendar.js | 2 +- source/Ox.js | 33 ++++++++++++++----------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/demos/calendar/js/calendar.js b/demos/calendar/js/calendar.js index 27bf0c3a..b76dab61 100644 --- a/demos/calendar/js/calendar.js +++ b/demos/calendar/js/calendar.js @@ -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'}, diff --git a/source/Ox.UI/js/Calendar/Ox.Calendar.js b/source/Ox.UI/js/Calendar/Ox.Calendar.js index 855d647f..ed4fce99 100644 --- a/source/Ox.UI/js/Calendar/Ox.Calendar.js +++ b/source/Ox.UI/js/Calendar/Ox.Calendar.js @@ -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)); diff --git a/source/Ox.js b/source/Ox.js index 326cfdf7..01b7af30 100644 --- a/source/Ox.js +++ b/source/Ox.js @@ -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 string utc 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 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) + ); } });