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