Skip to content

Commit

Permalink
fix %z for timezones greater than GMT
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonjs committed Mar 7, 2013
1 parent b38a331 commit 403504a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test:
node test/test.js
TZ=America/Vancouver node test/test.js
TZ=CET node test/test.js

.PHONY: test
3 changes: 1 addition & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
}
else {
var off = d.getTimezoneOffset();
return (off < 0 ? '+' : '-') + pad(off / 60) + pad(off % 60);
return (off < 0 ? '+' : '-') + pad(Math.abs(off / 60)) + pad(off % 60);
}
default: return c;
}
Expand All @@ -137,7 +137,6 @@

// Default padding is '0' and default length is 2, both are optional.
function pad(n, padding, length) {

// pad(n, <length>)
if (typeof padding === 'number') {
length = padding;
Expand Down
52 changes: 36 additions & 16 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,26 @@ assert.fn(lib.localizedStrftime)
ok('Exports')

/// time zones
testTimezone('P[DS]T')
ok('Time zones')
if (!process.env.TZ || process.env.TZ == 'America/Vancouver') {
testTimezone('P[DS]T')
assert.format('%C', '01', '01', new Date(100, 0, 1))
ok('Time zones (' + process.env.TZ + ')')
}
else if (process.env.TZ == 'CET') {
testTimezone('CES?T')
assert.format('%C', '01', '00', new Date(100, 0, 1))
ok('Time zones (' + process.env.TZ + ')')
}
else {
console.log('(Current timezone has no tests: ' + process.env.TZ + ')')
}

/// check all formats in GMT, most coverage
assert.format('%A', 'Tuesday')
assert.format('%a', 'Tue')
assert.format('%B', 'June')
assert.format('%b', 'Jun')
assert.format('%C', '20')
assert.format('%C', '01', null, new Date(100, 0, 1))
assert.format('%D', '06/07/11')
assert.format('%d', '07')
assert.format('%e', '7')
Expand Down Expand Up @@ -147,25 +157,35 @@ function testTimezone(regex) {
regex = typeof regex === 'string' ? RegExp('\\((' + regex + ')\\)$') : regex
var match = Time.toString().match(regex)
if (match) {
var hourDiff = Math.floor(Time.getTimezoneOffset() / 60)
, hours = String(18 - hourDiff)
var off = Time.getTimezoneOffset()
, hourOff = off / 60
, hourDiff = Math.floor(hourOff)
, hours = 18 - hourDiff
, padSpace24 = hours < 10 ? ' ' : ''
, padZero24 = hours < 10 ? '0' : ''
, hour24 = String(hours)
, padSpace12 = (hours % 12) < 10 ? ' ' : ''
, padZero12 = (hours % 12) < 10 ? '0' : ''
, hour12 = String(hours % 12)
, sign = hourDiff < 0 ? '+' : '-'
, minDiff = Time.getTimezoneOffset() - (hourDiff * 60)
, mins = String(51 - minDiff)
, R = hours + ':' + mins
, tz = match[1]
assert.format('%H', hours, '18')
assert.format('%I', hours, '06')
assert.format('%k', hours, '18')
assert.format('%l', hours, ' 6')
, ampm = hour12 == hour24 ? 'AM' : 'PM'
, R = hour24 + ':' + mins
, r = padZero12 + hour12 + ':' + mins + ':45 ' + ampm
, T = R + ':45'
assert.format('%H', padZero24 + hour24, '18')
assert.format('%I', padZero12 + hour12, '06')
assert.format('%k', padSpace24 + hour24, '18')
assert.format('%l', padSpace12 + hour12, ' 6')
assert.format('%M', mins)
assert.format('%P', 'am', 'pm')
assert.format('%p', 'AM', 'PM')
assert.format('%P', ampm.toLowerCase(), 'pm')
assert.format('%p', ampm, 'PM')
assert.format('%R', R, '18:51')
assert.format('%r', R + ':45 AM', '06:51:45 PM')
assert.format('%T', R + ':45', '18:51:45')
assert.format('%r', r, '06:51:45 PM')
assert.format('%T', T, '18:51:45')
assert.format('%Z', tz, 'GMT')
assert.format('%z', sign + '0' + hourDiff + '00', '+0000')
ok(tz)
assert.format('%z', sign + '0' + Math.abs(hourDiff) + '00', '+0000')
}
}

0 comments on commit 403504a

Please sign in to comment.