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.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")
"Monday, December 27, 1999"
*/
var date = date || new Date(),
sourceWeekday = Ox.formatDate(date, '%u', utc);
date = Ox.makeDate(date);
Ox.print(date, Ox.getDate(date, utc), Ox.formatDate(date, '%u', utc), date)
var sourceWeekday = Ox.getISODay(date, utc),
targetWeekday = Ox.isNumber(weekday) ? weekday :
Ox.map(Ox.WEEKDAYS, function(v, i) {
return v.substr(0, 3) == weekday.substr(0, 3) ? i + 1 : null;
})[0];
date[utc ? 'setUTCDate' : 'setDate'](
date[utc ? 'getUTCDate' : 'getDate']() - sourceWeekday + targetWeekday
);
return date;
Ox.print(date, Ox.getDate(date, utc), sourceWeekday, targetWeekday)
return Ox.setDate(date, Ox.getDate(date, utc) - sourceWeekday + targetWeekday, utc);
}
Ox.getDayOfTheYear = function(date, utc) {
@ -935,15 +932,13 @@ Ox.getDayOfTheYear = function(date, utc) {
>>> Ox.getDayOfTheYear(new Date("12/31/2004"))
366
*/
return function(date) {
date = date || new Date();
var month = date[utc ? 'getUTCMonth' : 'getMonth'](),
year = date[utc ? 'getUTCFullYear' : 'getFullYear']();
return Ox.sum(Ox.map(Ox.range(month), function(i) {
return Ox.getDaysInMonth(year, i + 1);
})) + date.getDate();
};
}();
date = Ox.makeDate(date);
var month = Ox.getMonth(date, utc),
year = Ox.getFullYear(date, utc);
return Ox.sum(Ox.map(Ox.range(month), function(i) {
return Ox.getDaysInMonth(year, i + 1);
})) + Ox.getDate(date, utc);
};
Ox.getDaysInMonth = function(year, month) {
/*
@ -959,35 +954,34 @@ Ox.getDaysInMonth = function(year, month) {
Ox.map(Ox.MONTHS, function(v, i) {
return v.substr(0, 3) == month.substr(0, 3) ? i + 1 : null;
})[0];
return new Date(year, month, 0).getDate()
//return Ox.DAYS[month - 1] + (month == 2 && Ox.isLeapYear(year));
return new Date(year, month, 0).getDate();
}
Ox.getDaysInYear = function(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)
>>> Ox.getFirstDayOfTheYear(new Date("01/01/00"))
>>> Ox.getFirstDayOfTheYear(new Date("01/01/2000"))
6
*/
var date_ = date ? new Date(date.valueOf()) : new Date();
date_.setMonth(0);
date_.setDate(1);
return date_.getDay();
date = Ox.makeDate(date);
date = Ox.setMonth(date, 0, utc);
date = Ox.setDate(date, 1, utc);
return Ox.getDay(date, utc)
};
Ox.getISODate = function(date) {
Ox.getISODate = function(date, utc) {
/*
>>> Ox.getISODate(new Date("01/01/2000"))
"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)
>>> Ox.getISODay(new Date("01/01/2000"))
@ -997,10 +991,10 @@ Ox.getISODay = function(date) {
>>> Ox.getISODay(new Date("01/03/2000"))
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
>>> Ox.getISOWeek(new Date("01/01/2000"))
@ -1010,14 +1004,14 @@ Ox.getISOWeek = function(date) {
>>> Ox.getISOWeek(new Date("01/03/2000"))
1
*/
date = date || new Date();
var date_ = new Date(date.valueOf());
date = Ox.makeDate(date);
// set date to Thursday of the same week
date_.setDate(date.getDate() - Ox.getISODay(date) + 4);
return Math.floor((Ox.getDayOfTheYear(date_) - 1) / 7) + 1;
return Math.floor((Ox.getDayOfTheYear(Ox.setDate(
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
>>> Ox.getISOYear(new Date("01/01/2000"))
@ -1027,14 +1021,15 @@ Ox.getISOYear = function(date) {
>>> Ox.getISOYear(new Date("01/03/2000"))
2000
*/
date = date || new Date();
var date_ = new Date(date.valueOf());
date = Ox.makeDate(date);
// set date to Thursday of the same week
date_.setDate(date.getDate() - Ox.getISODay(date) + 4);
return date_.getFullYear();
return Ox.getFullYear(Ox.setDate(
date, Ox.getDate(date, utc) - Ox.getISODay(date, utc) + 4, utc
));
};
Ox.getTime = function() {
// fixme: needed?
return +new Date();
}
@ -1044,13 +1039,13 @@ Ox.getTimezoneOffsetString = function(date) {
>>> Ox.getTimezoneOffsetString(new Date('01/01/2000')).length
5
*/
var offset = (date || new Date()).getTimezoneOffset();
var offset = (Ox.makeDate(date)).getTimezoneOffset();
return (offset < 0 ? '+' : '-') +
Ox.pad(Math.floor(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)
>>> Ox.getWeek(new Date("01/01/2000"))
@ -1060,9 +1055,9 @@ Ox.getWeek = function(date) {
>>> Ox.getWeek(new Date("01/03/2000"))
1
*/
date = date || new Date();
return Math.floor((Ox.getDayOfTheYear(date) +
Ox.getFirstDayOfTheYear(date) - 1) / 7);
date = Ox.makeDate(date);
return Math.floor((Ox.getDayOfTheYear(date, utc) +
Ox.getFirstDayOfTheYear(date, utc) - 1) / 7);
};
Ox.isLeapYear = function(year) {
@ -1082,6 +1077,24 @@ Ox.makeDate = function(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
@ -1613,17 +1626,9 @@ Ox.formatDate = function(date, str, utc) {
>>> Ox.formatDate(new Date("01/03/2000"), "%W")
"01"
*/
var fn = {}, format;
[
'getFullYear', 'getMonth', 'getDate', 'getDay',
'getHours', 'getMinutes', 'getSeconds'
].forEach(function(v) {
fn[v] = utc ? v.replace('get', 'getUTC') : v;
});
format = [
date = Ox.makeDate(date);
var format = [
['%', function() {return '%{%}';}],
['c', function() {return '%x %X';}],
['X', function() {return '%r';}],
@ -1636,34 +1641,34 @@ Ox.formatDate = function(date, str, utc) {
['T', function() {return '%H:%M:%S';}],
['v', function() {return '%e-%b-%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.SHORT_WEEKDAYS[(d[fn.getDay]() + 6) % 7];}],
['B', function(d) {return Ox.MONTHS[d[fn.getMonth]()];}],
['b', function(d) {return Ox.SHORT_MONTHS[d[fn.getMonth]()];}],
['C', function(d) {return Math.floor(d[fn.getFullYear]() / 100).toString();}],
['d', function(d) {return Ox.pad(d[fn.getDate](), 2);}],
['e', function(d) {return Ox.pad(d[fn.getDate](), 2, ' ');}],
['G', function(d) {return Ox.getISOYear(d);}],
['g', function(d) {return Ox.getISOYear(d).toString().substr(-2);}],
['H', function(d) {return Ox.pad(d[fn.getHours](), 2);}],
['I', function(d) {return Ox.pad((d[fn.getHours]() + 11) % 12 + 1, 2);}],
['j', function(d) {return Ox.pad(Ox.getDayOfTheYear(d), 3);}],
['k', function(d) {return Ox.pad(d[fn.getHours](), 2, ' ');}],
['l', function(d) {return Ox.pad(((d[fn.getHours]() + 11) % 12 + 1), 2, ' ');}],
['M', function(d) {return Ox.pad(d[fn.getMinutes](), 2);}],
['m', function(d) {return Ox.pad((d[fn.getMonth]() + 1), 2);}],
['p', function(d) {return Ox.AMPM[Math.floor(d[fn.getHours]() / 12)];}],
['Q', function(d) {return Math.floor(d[fn.getMonth]() / 4) + 1;}],
['S', function(d) {return Ox.pad(d[fn.getSeconds](), 2);}],
['A', function(d) {return Ox.WEEKDAYS[(Ox.getDay(d, utc) + 6) % 7];}],
['a', function(d) {return Ox.SHORT_WEEKDAYS[(Ox.getDay(d, utc) + 6) % 7];}],
['B', function(d) {return Ox.MONTHS[Ox.getMonth(d, utc)];}],
['b', function(d) {return Ox.SHORT_MONTHS[Ox.getMonth(d, utc)];}],
['C', function(d) {return Math.floor(Ox.getFullYear(d, utc) / 100).toString();}],
['d', function(d) {return Ox.pad(Ox.getDate(d, utc), 2);}],
['e', function(d) {return Ox.pad(Ox.getDate(d, utc), 2, ' ');}],
['G', function(d) {return Ox.getISOYear(d, utc);}],
['g', function(d) {return Ox.getISOYear(d, utc).toString().substr(-2);}],
['H', function(d) {return Ox.pad(Ox.getHours(d, utc), 2);}],
['I', function(d) {return Ox.pad((Ox.getHours(d, utc) + 11) % 12 + 1, 2);}],
['j', function(d) {return Ox.pad(Ox.getDayOfTheYear(d, utc), 3);}],
['k', function(d) {return Ox.pad(Ox.getHours(d, utc), 2, ' ');}],
['l', function(d) {return Ox.pad(((Ox.getHours(d, utc) + 11) % 12 + 1), 2, ' ');}],
['M', function(d) {return Ox.pad(Ox.getMinutes(d, utc), 2);}],
['m', function(d) {return Ox.pad((Ox.getMonth(d, utc) + 1), 2);}],
['p', function(d) {return Ox.AMPM[Math.floor(Ox.getHours(d, utc) / 12)];}],
['Q', function(d) {return Math.floor(Ox.getMonth(d, utc) / 4) + 1;}],
['S', function(d) {return Ox.pad(Ox.getSeconds(d, utc), 2);}],
['s', function(d) {return Math.floor(d.getTime() / 1000);}],
['U', function(d) {return Ox.pad(Ox.getWeek(d), 2);}],
['u', function(d) {return Ox.getISODay(d);}],
['V', function(d) {return Ox.pad(Ox.getISOWeek(d), 2);}],
['W', function(d) {return Ox.pad(Math.floor((Ox.getDayOfTheYear(d) +
(Ox.getFirstDayOfTheYear(d) || 7) - 2) / 7), 2);}],
['w', function(d) {return d[fn.getDay]();}],
['Y', function(d) {return d[fn.getFullYear]();}],
['y', function(d) {return d[fn.getFullYear]().toString().substr(-2);}],
['U', function(d) {return Ox.pad(Ox.getWeek(d, utc), 2);}],
['u', function(d) {return Ox.getISODay(d, utc);}],
['V', function(d) {return Ox.pad(Ox.getISOWeek(d, utc), 2);}],
['W', function(d) {return Ox.pad(Math.floor((Ox.getDayOfTheYear(d, utc) +
(Ox.getFirstDayOfTheYear(d, utc) || 7) - 2) / 7), 2);}],
['w', function(d) {return Ox.getDay(d, utc);}],
['Y', function(d) {return Ox.getFullYear(d, utc);}],
['y', function(d) {return Ox.getFullYear(d, utc).toString().substr(-2);}],
['Z', function(d) {return d.toString().split('(')[1].replace(')', '');}],
['z', function(d) {return Ox.getTimezoneOffsetString(d);}],
['n', function() {return '\n';}],
@ -1671,7 +1676,10 @@ Ox.formatDate = function(date, str, utc) {
['\\{%\\}', function() {return '%';}]
];
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;
};

View file

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