Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get offset change instants within a period of time #417

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions moment-timezone.js
Expand Up @@ -161,6 +161,34 @@
}
},

offsetChanges: function (from, to) {
var fromIndex = this._index(from),
toIndex = this._index(to),
changes = [],
i;
for (i = fromIndex; i < toIndex; i++) changes.push(this.untils[i]);
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,
Expand Down
37 changes: 37 additions & 0 deletions tests/moment-timezone/zone.js
Expand Up @@ -38,6 +38,43 @@ 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();
},

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 = [
Expand Down