From 466c644380a3ac67c4a05d8fc3e46a6dfb0491f6 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Thu, 6 Sep 2012 01:34:38 +0400 Subject: [PATCH 1/2] restore original timezone in case of error --- index.js | 8 +++++++- test/date.js | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index d9f237d..3eb71bb 100644 --- a/index.js +++ b/index.js @@ -249,7 +249,13 @@ function setTimezone (timezone, relative) { , tz = exports._currentZoneinfo; if (!tz || oldTz !== timezone) { debug('current timezone is not "%s", calling tzset()', timezone); - tz = exports.tzset(timezone); + try { + tz = exports.tzset(timezone); + } catch(err) { + // If there was a invalid timezone, restore it back and rethrow + exports.tzset(oldTz); + throw err; + } } // Get the zoneinfo for this Date instance's time value 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() From c83d41b2bfe604c3ce625b67d8b10fff61ab9e71 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Thu, 6 Sep 2012 02:05:06 +0400 Subject: [PATCH 2/2] tzset() should probably restore TZ too, so moved this code deeper --- index.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 3eb71bb..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]; @@ -249,13 +253,7 @@ function setTimezone (timezone, relative) { , tz = exports._currentZoneinfo; if (!tz || oldTz !== timezone) { debug('current timezone is not "%s", calling tzset()', timezone); - try { - tz = exports.tzset(timezone); - } catch(err) { - // If there was a invalid timezone, restore it back and rethrow - exports.tzset(oldTz); - throw err; - } + tz = exports.tzset(timezone); } // Get the zoneinfo for this Date instance's time value