From 88f1076220fd9bb7b514489f86e6c762cd9b9136 Mon Sep 17 00:00:00 2001 From: Rafael Sobral Date: Thu, 10 Nov 2016 20:13:29 -0200 Subject: [PATCH 1/4] Function to get offset changes in a period --- moment-timezone.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/moment-timezone.js b/moment-timezone.js index e53f3256..fabcb79d 100644 --- a/moment-timezone.js +++ b/moment-timezone.js @@ -161,6 +161,15 @@ } }, + offsetChanges: function (from, to) { + var fromIndex = this._index(from), + toIndex = this._index(to), + changes = [], + i; + for (i = fromIndex; i < toIndex; i++) changes.push(untils[i]); + return changes; + }, + parse : function (timestamp) { var target = +timestamp, offsets = this.offsets, From 8bd2526a3880897540c1038ffceece66586f9526 Mon Sep 17 00:00:00 2001 From: Rafael Sobral Date: Thu, 10 Nov 2016 20:26:46 -0200 Subject: [PATCH 2/4] Test implementation for Zone.offsetChanges --- tests/moment-timezone/zone.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/moment-timezone/zone.js b/tests/moment-timezone/zone.js index 8dac8c14..72c9d439 100644 --- a/tests/moment-timezone/zone.js +++ b/tests/moment-timezone/zone.js @@ -38,6 +38,21 @@ exports.zone = { test.done(); }, + offsetChanges: function(test) { + var zone = new tz.Zone(PACKED), + from = 1200 * 60000, + to = 1400 * 60000, + expected = [ 1240 * 60000, 1340 * 60000 ]; + test.deepEqual(zone.offsetChanges(from, to), expected, "The offsetChanges from " + from + "to" + to + " should be " + expected); + from = 0 + expected = [ 1000 * 60000, 1100 * 60000, 1240 * 60000, 1340 * 60000 ]; + test.deepEqual(zone.offsetChanges(from, to), expected, "The offsetChanges from " + from + "to" + to + " should be " + expected); + to = 1500 * 60000; + expected = [ 1000 * 60000, 1100 * 60000, 1240 * 60000, 1340 * 60000, 1480 * 60000 ]; + test.deepEqual(zone.offsetChanges(from, to), expected, "The offsetChanges from " + from + "to" + to + " should be " + expected); + test.done(); + }, + abbr : function (test) { var zone = new tz.Zone(PACKED), tests = [ From 60f74c10c1c0b40ce0f880e0518dae23c783db2a Mon Sep 17 00:00:00 2001 From: Rafael Sobral Date: Thu, 10 Nov 2016 20:33:28 -0200 Subject: [PATCH 3/4] Zone.offsetChanges fix --- moment-timezone.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moment-timezone.js b/moment-timezone.js index fabcb79d..f6e610ea 100644 --- a/moment-timezone.js +++ b/moment-timezone.js @@ -166,7 +166,7 @@ toIndex = this._index(to), changes = [], i; - for (i = fromIndex; i < toIndex; i++) changes.push(untils[i]); + for (i = fromIndex; i < toIndex; i++) changes.push(this.untils[i]); return changes; }, From 5d0f2bd9178b2489dcd11c6de00ec5824f1b6fa9 Mon Sep 17 00:00:00 2001 From: Rafael Sobral Date: Mon, 21 Nov 2016 22:21:32 -0200 Subject: [PATCH 4/4] Implemented offsetNext and offsetPrevious It return the previous and the next offset --- moment-timezone.js | 19 +++++++++++++++++++ tests/moment-timezone/zone.js | 24 +++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/moment-timezone.js b/moment-timezone.js index f6e610ea..25a9b6af 100644 --- a/moment-timezone.js +++ b/moment-timezone.js @@ -170,6 +170,25 @@ return changes; }, + offsetNext: function (timestamp) { + var index = this._index(timestamp); + if (index === undefined) { + return undefined; + } + return this.untils[index]; + }, + + offsetPrevious: function (timestamp) { + var index = this._index(timestamp); + if (index === undefined) { + return undefined; + } + if (index < 1) { + return undefined; + } + return this.untils[index-1]; + }, + parse : function (timestamp) { var target = +timestamp, offsets = this.offsets, diff --git a/tests/moment-timezone/zone.js b/tests/moment-timezone/zone.js index 72c9d439..9d8d32ce 100644 --- a/tests/moment-timezone/zone.js +++ b/tests/moment-timezone/zone.js @@ -44,7 +44,7 @@ exports.zone = { to = 1400 * 60000, expected = [ 1240 * 60000, 1340 * 60000 ]; test.deepEqual(zone.offsetChanges(from, to), expected, "The offsetChanges from " + from + "to" + to + " should be " + expected); - from = 0 + from = 0; expected = [ 1000 * 60000, 1100 * 60000, 1240 * 60000, 1340 * 60000 ]; test.deepEqual(zone.offsetChanges(from, to), expected, "The offsetChanges from " + from + "to" + to + " should be " + expected); to = 1500 * 60000; @@ -53,6 +53,28 @@ exports.zone = { test.done(); }, + offsetNext: function(test) { + var zone = new tz.Zone(PACKED), + timestamp = 1050 * 60000, + expected = 1100 * 60000; + test.equal(zone.offsetNext(timestamp), expected, "The offsetNext of " + timestamp + " should be " + expected); + timestamp = 1150 * 60000; + expected = 1240 * 60000; + test.equal(zone.offsetNext(timestamp), expected, "The offsetNext of " + timestamp + " should be " + expected); + test.done(); + }, + + offsetPrevious: function(test) { + var zone = new tz.Zone(PACKED), + timestamp = 1050 * 60000, + expected = 1000 * 60000; + test.equal(zone.offsetPrevious(timestamp), expected, "The offsetNext of " + timestamp + " should be " + expected); + timestamp = 1150 * 60000; + expected = 1100 * 60000; + test.equal(zone.offsetPrevious(timestamp), expected, "The offsetNext of " + timestamp + " should be " + expected); + test.done(); + }, + abbr : function (test) { var zone = new tz.Zone(PACKED), tests = [