Ox.formatDate: better tests for '%s' and '%+', fix '%s', '%Z' and '%z'

This commit is contained in:
rolux 2012-05-27 15:01:22 +02:00
parent 707899b61c
commit 4b8c1a7d6b

View file

@ -39,6 +39,7 @@ Ox.formatDate <f> Formats a date according to a format string
utc <b> date is utc
<script>
Ox.test.date = new Date('2005/01/02 00:03:04');
Ox.test.epoch = new Date('1970/01/01 00:00:00');
</script>
> Ox.formatDate(Ox.test.date, '%A') // Full weekday
'Sunday'
@ -92,8 +93,8 @@ Ox.formatDate <f> Formats a date according to a format string
'12:03:04 AM'
> Ox.formatDate(Ox.test.date, '%S') // Zero-padded second
'04'
> Ox.formatDate(Ox.test.date, '%s', true) // Number of seconds since the Epoch
'1104620584'
> Ox.formatDate(Ox.test.epoch, '%s', true) // Number of seconds since the Epoch
'0'
> Ox.formatDate(Ox.test.date, '%T') // Time
'00:03:04'
> Ox.formatDate(Ox.test.date, '%t') // Tab
@ -122,8 +123,8 @@ Ox.formatDate <f> Formats a date according to a format string
'UTC'
> Ox.formatDate(Ox.test.date, '%z', true) // Time zone offset
'+0000'
> Ox.formatDate(Ox.test.date, '%+', true) // Formatted date and time
'Sun Jan 2 00:03:04 CET 2005'
> Ox.formatDate(Ox.test.date, '%+').replace(/ [A-Z]+ /, ' XYZ ') // Formatted date and time
'Sun Jan 2 00:03:04 XYZ 2005'
> Ox.formatDate(Ox.test.date, '%%')
'%'
@*/
@ -219,8 +220,9 @@ Ox.formatDate <f> Formats a date according to a format string
return Ox.pad(Ox.getSeconds(date, utc), 2);
}],
['s', function(date, utc) {
return Math.floor(date.getTime() / 1000)
- (utc ? Ox.getTimezoneOffset() / 1000 : 0);
return Math.floor((+date - (
utc ? Ox.getTimezoneOffset(date) : 0
)) / 1000);
}],
['U', function(date, utc) {
return Ox.pad(Ox.getWeek(date, utc), 2);
@ -252,11 +254,12 @@ Ox.formatDate <f> Formats a date according to a format string
['y', function(date, utc) {
return Ox.getFullYear(date, utc).toString().slice(-2);
}],
['Z', function(date) {
return (date.toString().split('(')[1] || '').replace(')', '');
['Z', function(date, utc) {
return utc ? 'UTC'
: (date.toString().split('(')[1] || '').replace(')', '');
}],
['z', function(date) {
return Ox.getTimezoneOffsetString(date);
['z', function(date, utc) {
return utc ? '+0000' : Ox.getTimezoneOffsetString(date);
}],
['n', function() {
return '\n';