Skip to content
Browse files

fix %z for timezones greater than GMT

  • Loading branch information...
1 parent b38a331 commit 403504a494494b9403d74f68508e9101c2a81f33 @samsonjs committed Mar 7, 2013
Showing with 39 additions and 19 deletions.
  1. +2 −1 Makefile
  2. +1 −2 lib/index.js
  3. +36 −16 test/test.js
View
3 Makefile
@@ -1,4 +1,5 @@
test:
- node test/test.js
+ TZ=America/Vancouver node test/test.js
+ TZ=CET node test/test.js
.PHONY: test
View
3 lib/index.js
@@ -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;
}
@@ -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;
View
52 test/test.js
@@ -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')
@@ -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.
Something went wrong with that request. Please try again.