fixing date

This commit is contained in:
rolux 2011-04-24 00:27:31 +02:00
parent 4d3c7143a5
commit 3672d7ee9c
2 changed files with 94 additions and 86 deletions

View file

@ -903,8 +903,6 @@ Date functions
================================================================================ ================================================================================
*/ */
// fixme: support UTC, only halfway done
Ox.getDateInWeek = function(date, weekday, utc) { Ox.getDateInWeek = function(date, weekday, utc) {
/* /*
>>> Ox.formatDate(Ox.getDateInWeek(new Date("January 1 2000"), "Sunday"), "%A, %B %e, %Y") >>> Ox.formatDate(Ox.getDateInWeek(new Date("January 1 2000"), "Sunday"), "%A, %B %e, %Y")
@ -914,16 +912,15 @@ Ox.getDateInWeek = function(date, weekday, utc) {
>>> Ox.formatDate(Ox.getDateInWeek(new Date("1/1/2000"), 1), "%A, %B %e, %Y") >>> Ox.formatDate(Ox.getDateInWeek(new Date("1/1/2000"), 1), "%A, %B %e, %Y")
"Monday, December 27, 1999" "Monday, December 27, 1999"
*/ */
var date = date || new Date(), date = Ox.makeDate(date);
sourceWeekday = Ox.formatDate(date, '%u', utc); Ox.print(date, Ox.getDate(date, utc), Ox.formatDate(date, '%u', utc), date)
var sourceWeekday = Ox.getISODay(date, utc),
targetWeekday = Ox.isNumber(weekday) ? weekday : targetWeekday = Ox.isNumber(weekday) ? weekday :
Ox.map(Ox.WEEKDAYS, function(v, i) { Ox.map(Ox.WEEKDAYS, function(v, i) {
return v.substr(0, 3) == weekday.substr(0, 3) ? i + 1 : null; return v.substr(0, 3) == weekday.substr(0, 3) ? i + 1 : null;
})[0]; })[0];
date[utc ? 'setUTCDate' : 'setDate']( Ox.print(date, Ox.getDate(date, utc), sourceWeekday, targetWeekday)
date[utc ? 'getUTCDate' : 'getDate']() - sourceWeekday + targetWeekday return Ox.setDate(date, Ox.getDate(date, utc) - sourceWeekday + targetWeekday, utc);
);
return date;
} }
Ox.getDayOfTheYear = function(date, utc) { Ox.getDayOfTheYear = function(date, utc) {
@ -935,15 +932,13 @@ Ox.getDayOfTheYear = function(date, utc) {
>>> Ox.getDayOfTheYear(new Date("12/31/2004")) >>> Ox.getDayOfTheYear(new Date("12/31/2004"))
366 366
*/ */
return function(date) { date = Ox.makeDate(date);
date = date || new Date(); var month = Ox.getMonth(date, utc),
var month = date[utc ? 'getUTCMonth' : 'getMonth'](), year = Ox.getFullYear(date, utc);
year = date[utc ? 'getUTCFullYear' : 'getFullYear']();
return Ox.sum(Ox.map(Ox.range(month), function(i) { return Ox.sum(Ox.map(Ox.range(month), function(i) {
return Ox.getDaysInMonth(year, i + 1); return Ox.getDaysInMonth(year, i + 1);
})) + date.getDate(); })) + Ox.getDate(date, utc);
}; };
}();
Ox.getDaysInMonth = function(year, month) { Ox.getDaysInMonth = function(year, month) {
/* /*
@ -959,35 +954,34 @@ Ox.getDaysInMonth = function(year, month) {
Ox.map(Ox.MONTHS, function(v, i) { Ox.map(Ox.MONTHS, function(v, i) {
return v.substr(0, 3) == month.substr(0, 3) ? i + 1 : null; return v.substr(0, 3) == month.substr(0, 3) ? i + 1 : null;
})[0]; })[0];
return new Date(year, month, 0).getDate() return new Date(year, month, 0).getDate();
//return Ox.DAYS[month - 1] + (month == 2 && Ox.isLeapYear(year));
} }
Ox.getDaysInYear = function(year) { Ox.getDaysInYear = function(year) {
return 365 + Ox.isLeapYear(year); return 365 + Ox.isLeapYear(year);
}; };
Ox.getFirstDayOfTheYear = function(date) { Ox.getFirstDayOfTheYear = function(date, utc) {
/* /*
Decimal weekday of January 1 (0-6, Sunday as first day) Decimal weekday of January 1 (0-6, Sunday as first day)
>>> Ox.getFirstDayOfTheYear(new Date("01/01/00")) >>> Ox.getFirstDayOfTheYear(new Date("01/01/2000"))
6 6
*/ */
var date_ = date ? new Date(date.valueOf()) : new Date(); date = Ox.makeDate(date);
date_.setMonth(0); date = Ox.setMonth(date, 0, utc);
date_.setDate(1); date = Ox.setDate(date, 1, utc);
return date_.getDay(); return Ox.getDay(date, utc)
}; };
Ox.getISODate = function(date) { Ox.getISODate = function(date, utc) {
/* /*
>>> Ox.getISODate(new Date("01/01/2000")) >>> Ox.getISODate(new Date("01/01/2000"))
"2000-01-01T00:00:00Z" "2000-01-01T00:00:00Z"
*/ */
return Ox.formatDate(date || new Date(), '%FT%TZ'); return Ox.formatDate(Ox.makeDate(date), '%FT%TZ', utc);
}; };
Ox.getISODay = function(date) { Ox.getISODay = function(date, utc) {
/* /*
Decimal weekday (1-7, Monday as first day) Decimal weekday (1-7, Monday as first day)
>>> Ox.getISODay(new Date("01/01/2000")) >>> Ox.getISODay(new Date("01/01/2000"))
@ -997,10 +991,10 @@ Ox.getISODay = function(date) {
>>> Ox.getISODay(new Date("01/03/2000")) >>> Ox.getISODay(new Date("01/03/2000"))
1 1
*/ */
return (date || new Date()).getDay() || 7; return Ox.getDay(Ox.makeDate(date), utc) || 7;
}; };
Ox.getISOWeek = function(date) { Ox.getISOWeek = function(date, utc) {
/* /*
see http://en.wikipedia.org/wiki/ISO_8601 see http://en.wikipedia.org/wiki/ISO_8601
>>> Ox.getISOWeek(new Date("01/01/2000")) >>> Ox.getISOWeek(new Date("01/01/2000"))
@ -1010,14 +1004,14 @@ Ox.getISOWeek = function(date) {
>>> Ox.getISOWeek(new Date("01/03/2000")) >>> Ox.getISOWeek(new Date("01/03/2000"))
1 1
*/ */
date = date || new Date(); date = Ox.makeDate(date);
var date_ = new Date(date.valueOf());
// set date to Thursday of the same week // set date to Thursday of the same week
date_.setDate(date.getDate() - Ox.getISODay(date) + 4); return Math.floor((Ox.getDayOfTheYear(Ox.setDate(
return Math.floor((Ox.getDayOfTheYear(date_) - 1) / 7) + 1; date, Ox.getDate(date, utc) - Ox.getISODay(date, utc) + 4, utc
), utc) - 1) / 7) + 1;
}; };
Ox.getISOYear = function(date) { Ox.getISOYear = function(date, utc) {
/* /*
see http://en.wikipedia.org/wiki/ISO_8601 see http://en.wikipedia.org/wiki/ISO_8601
>>> Ox.getISOYear(new Date("01/01/2000")) >>> Ox.getISOYear(new Date("01/01/2000"))
@ -1027,14 +1021,15 @@ Ox.getISOYear = function(date) {
>>> Ox.getISOYear(new Date("01/03/2000")) >>> Ox.getISOYear(new Date("01/03/2000"))
2000 2000
*/ */
date = date || new Date(); date = Ox.makeDate(date);
var date_ = new Date(date.valueOf());
// set date to Thursday of the same week // set date to Thursday of the same week
date_.setDate(date.getDate() - Ox.getISODay(date) + 4); return Ox.getFullYear(Ox.setDate(
return date_.getFullYear(); date, Ox.getDate(date, utc) - Ox.getISODay(date, utc) + 4, utc
));
}; };
Ox.getTime = function() { Ox.getTime = function() {
// fixme: needed?
return +new Date(); return +new Date();
} }
@ -1044,13 +1039,13 @@ Ox.getTimezoneOffsetString = function(date) {
>>> Ox.getTimezoneOffsetString(new Date('01/01/2000')).length >>> Ox.getTimezoneOffsetString(new Date('01/01/2000')).length
5 5
*/ */
var offset = (date || new Date()).getTimezoneOffset(); var offset = (Ox.makeDate(date)).getTimezoneOffset();
return (offset < 0 ? '+' : '-') + return (offset < 0 ? '+' : '-') +
Ox.pad(Math.floor(Math.abs(offset) / 60), 2) + Ox.pad(Math.floor(Math.abs(offset) / 60), 2) +
Ox.pad(Math.abs(offset) % 60, 2); Ox.pad(Math.abs(offset) % 60, 2);
}; };
Ox.getWeek = function(date) { Ox.getWeek = function(date, utc) {
/* /*
Week of the year (0-53, Sunday as first day) Week of the year (0-53, Sunday as first day)
>>> Ox.getWeek(new Date("01/01/2000")) >>> Ox.getWeek(new Date("01/01/2000"))
@ -1060,9 +1055,9 @@ Ox.getWeek = function(date) {
>>> Ox.getWeek(new Date("01/03/2000")) >>> Ox.getWeek(new Date("01/03/2000"))
1 1
*/ */
date = date || new Date(); date = Ox.makeDate(date);
return Math.floor((Ox.getDayOfTheYear(date) + return Math.floor((Ox.getDayOfTheYear(date, utc) +
Ox.getFirstDayOfTheYear(date) - 1) / 7); Ox.getFirstDayOfTheYear(date, utc) - 1) / 7);
}; };
Ox.isLeapYear = function(year) { Ox.isLeapYear = function(year) {
@ -1082,6 +1077,24 @@ Ox.makeDate = function(date) {
Ox.isUndefined(date) ? new Date() : new Date(date); Ox.isUndefined(date) ? new Date() : new Date(date);
}; };
['get', 'set'].forEach(function(verb) {
[
'FullYear', 'Month', 'Date', 'Day', 'Hours', 'Minutes', 'Seconds'
].forEach(function(noun) {
Ox['get' + noun] = function(date, 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);
}
});
});
/* /*
================================================================================ ================================================================================
DOM functions DOM functions
@ -1614,16 +1627,8 @@ Ox.formatDate = function(date, str, utc) {
"01" "01"
*/ */
var fn = {}, format; date = Ox.makeDate(date);
var format = [
[
'getFullYear', 'getMonth', 'getDate', 'getDay',
'getHours', 'getMinutes', 'getSeconds'
].forEach(function(v) {
fn[v] = utc ? v.replace('get', 'getUTC') : v;
});
format = [
['%', function() {return '%{%}';}], ['%', function() {return '%{%}';}],
['c', function() {return '%x %X';}], ['c', function() {return '%x %X';}],
['X', function() {return '%r';}], ['X', function() {return '%r';}],
@ -1636,34 +1641,34 @@ Ox.formatDate = function(date, str, utc) {
['T', function() {return '%H:%M:%S';}], ['T', function() {return '%H:%M:%S';}],
['v', function() {return '%e-%b-%Y';}], ['v', function() {return '%e-%b-%Y';}],
['\\+', function() {return '%a %b %e %H:%M:%S %Z %Y';}], ['\\+', function() {return '%a %b %e %H:%M:%S %Z %Y';}],
['A', function(d) {return Ox.WEEKDAYS[(d[fn.getDay]() + 6) % 7];}], ['A', function(d) {return Ox.WEEKDAYS[(Ox.getDay(d, utc) + 6) % 7];}],
['a', function(d) {return Ox.SHORT_WEEKDAYS[(d[fn.getDay]() + 6) % 7];}], ['a', function(d) {return Ox.SHORT_WEEKDAYS[(Ox.getDay(d, utc) + 6) % 7];}],
['B', function(d) {return Ox.MONTHS[d[fn.getMonth]()];}], ['B', function(d) {return Ox.MONTHS[Ox.getMonth(d, utc)];}],
['b', function(d) {return Ox.SHORT_MONTHS[d[fn.getMonth]()];}], ['b', function(d) {return Ox.SHORT_MONTHS[Ox.getMonth(d, utc)];}],
['C', function(d) {return Math.floor(d[fn.getFullYear]() / 100).toString();}], ['C', function(d) {return Math.floor(Ox.getFullYear(d, utc) / 100).toString();}],
['d', function(d) {return Ox.pad(d[fn.getDate](), 2);}], ['d', function(d) {return Ox.pad(Ox.getDate(d, utc), 2);}],
['e', function(d) {return Ox.pad(d[fn.getDate](), 2, ' ');}], ['e', function(d) {return Ox.pad(Ox.getDate(d, utc), 2, ' ');}],
['G', function(d) {return Ox.getISOYear(d);}], ['G', function(d) {return Ox.getISOYear(d, utc);}],
['g', function(d) {return Ox.getISOYear(d).toString().substr(-2);}], ['g', function(d) {return Ox.getISOYear(d, utc).toString().substr(-2);}],
['H', function(d) {return Ox.pad(d[fn.getHours](), 2);}], ['H', function(d) {return Ox.pad(Ox.getHours(d, utc), 2);}],
['I', function(d) {return Ox.pad((d[fn.getHours]() + 11) % 12 + 1, 2);}], ['I', function(d) {return Ox.pad((Ox.getHours(d, utc) + 11) % 12 + 1, 2);}],
['j', function(d) {return Ox.pad(Ox.getDayOfTheYear(d), 3);}], ['j', function(d) {return Ox.pad(Ox.getDayOfTheYear(d, utc), 3);}],
['k', function(d) {return Ox.pad(d[fn.getHours](), 2, ' ');}], ['k', function(d) {return Ox.pad(Ox.getHours(d, utc), 2, ' ');}],
['l', function(d) {return Ox.pad(((d[fn.getHours]() + 11) % 12 + 1), 2, ' ');}], ['l', function(d) {return Ox.pad(((Ox.getHours(d, utc) + 11) % 12 + 1), 2, ' ');}],
['M', function(d) {return Ox.pad(d[fn.getMinutes](), 2);}], ['M', function(d) {return Ox.pad(Ox.getMinutes(d, utc), 2);}],
['m', function(d) {return Ox.pad((d[fn.getMonth]() + 1), 2);}], ['m', function(d) {return Ox.pad((Ox.getMonth(d, utc) + 1), 2);}],
['p', function(d) {return Ox.AMPM[Math.floor(d[fn.getHours]() / 12)];}], ['p', function(d) {return Ox.AMPM[Math.floor(Ox.getHours(d, utc) / 12)];}],
['Q', function(d) {return Math.floor(d[fn.getMonth]() / 4) + 1;}], ['Q', function(d) {return Math.floor(Ox.getMonth(d, utc) / 4) + 1;}],
['S', function(d) {return Ox.pad(d[fn.getSeconds](), 2);}], ['S', function(d) {return Ox.pad(Ox.getSeconds(d, utc), 2);}],
['s', function(d) {return Math.floor(d.getTime() / 1000);}], ['s', function(d) {return Math.floor(d.getTime() / 1000);}],
['U', function(d) {return Ox.pad(Ox.getWeek(d), 2);}], ['U', function(d) {return Ox.pad(Ox.getWeek(d, utc), 2);}],
['u', function(d) {return Ox.getISODay(d);}], ['u', function(d) {return Ox.getISODay(d, utc);}],
['V', function(d) {return Ox.pad(Ox.getISOWeek(d), 2);}], ['V', function(d) {return Ox.pad(Ox.getISOWeek(d, utc), 2);}],
['W', function(d) {return Ox.pad(Math.floor((Ox.getDayOfTheYear(d) + ['W', function(d) {return Ox.pad(Math.floor((Ox.getDayOfTheYear(d, utc) +
(Ox.getFirstDayOfTheYear(d) || 7) - 2) / 7), 2);}], (Ox.getFirstDayOfTheYear(d, utc) || 7) - 2) / 7), 2);}],
['w', function(d) {return d[fn.getDay]();}], ['w', function(d) {return Ox.getDay(d, utc);}],
['Y', function(d) {return d[fn.getFullYear]();}], ['Y', function(d) {return Ox.getFullYear(d, utc);}],
['y', function(d) {return d[fn.getFullYear]().toString().substr(-2);}], ['y', function(d) {return Ox.getFullYear(d, utc).toString().substr(-2);}],
['Z', function(d) {return d.toString().split('(')[1].replace(')', '');}], ['Z', function(d) {return d.toString().split('(')[1].replace(')', '');}],
['z', function(d) {return Ox.getTimezoneOffsetString(d);}], ['z', function(d) {return Ox.getTimezoneOffsetString(d);}],
['n', function() {return '\n';}], ['n', function() {return '\n';}],
@ -1671,7 +1676,10 @@ Ox.formatDate = function(date, str, utc) {
['\\{%\\}', function() {return '%';}] ['\\{%\\}', function() {return '%';}]
]; ];
format.forEach(function(v) { format.forEach(function(v) {
str = str.replace(new RegExp('%' + v[0], 'g'), v[1](date)); var regexp = new RegExp('%' + v[0], 'g');
if (regexp.test(str)) {
str = str.replace(regexp, v[1](date));
}
}); });
return str; return str;
}; };

View file

@ -288,10 +288,10 @@ Provides function Ox.UI([options], callback) that fires when
function stop() { function stop() {
var counter = 0, var counter = 0,
message = 'Browser not supported, use ' + userAgents.map(function(userAgent, i) { message = 'Browser not supported, use ' + browsers.map(function(browser, i) {
return userAgent.name + ( return browser.name + (
i == userAgents.length - 1 ? '.' : i == browsers.length - 1 ? '.' :
i == userAgents.length - 2 ? ' or' : ',' i == browsers.length - 2 ? ' or' : ','
); );
}).join(' '); }).join(' ');
if (oxUIOptions.display == 'none') { if (oxUIOptions.display == 'none') {