diff --git a/index.js b/index.js index d9f237d..9061056 100644 --- a/index.js +++ b/index.js @@ -147,6 +147,10 @@ function tzset (tz) { debug('set the current timezone to:', usedTz); if (!rtn.tzname[1] && rtn.timezone === 0) { debug('got bad zoneinfo object:', rtn); + if (tz !== exports.currentTimezone) { + // restore original timezone in case of error + tzset(exports.currentTimezone); + } var err = new Error("Unknown Timezone: '" + usedTz + "'"); for (var i in rtn) { err[i] = rtn[i]; diff --git a/test/date.js b/test/date.js index 92be017..585d187 100644 --- a/test/date.js +++ b/test/date.js @@ -51,6 +51,13 @@ describe('Date', function () { initial.should.equal(process.env.TZ) }) + it('should clean up after an error', function () { + var initial = process.env.TZ + , d = new time.Date + d.setTimezone.bind(d, 'Any/Wrong/TZ').should.throw(/^Unknown Timezone/) + initial.should.equal(process.env.TZ) + }) + it('should change the "timezone offset"', function () { var d = new time.Date , offset = d.getTimezoneOffset()