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

[bugfix] parseZone should handle UTC #3523

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 16 additions & 6 deletions src/lib/units/offset.js
Expand Up @@ -48,12 +48,19 @@ addParseToken(['Z', 'ZZ'], function (input, array, config) {
var chunkOffset = /([\+\-]|\d\d)/gi;

function offsetFromString(matcher, string) {
var matches = ((string || '').match(matcher) || []);
var matches = (string || '').match(matcher);

if (matches === null) {
return null;
}

var chunk = matches[matches.length - 1] || [];
var parts = (chunk + '').match(chunkOffset) || ['-', 0, 0];
var minutes = +(parts[1] * 60) + toInt(parts[2]);

return parts[0] === '+' ? minutes : -minutes;
return minutes === 0 ?
0 :
parts[0] === '+' ? minutes : -minutes;
}

// Return a moment from input, that is local/utc/zone equivalent to model.
Expand Down Expand Up @@ -104,6 +111,9 @@ export function getSetOffset (input, keepLocalTime) {
if (input != null) {
if (typeof input === 'string') {
input = offsetFromString(matchShortOffset, input);
if (input === null) {
return this;
}
} else if (Math.abs(input) < 16) {
input = input * 60;
}
Expand Down Expand Up @@ -165,11 +175,11 @@ export function setOffsetToParsedOffset () {
this.utcOffset(this._tzm);
} else if (typeof this._i === 'string') {
var tZone = offsetFromString(matchOffset, this._i);

if (tZone === 0) {
if (tZone != null) {
this.utcOffset(tZone);
}
else {
this.utcOffset(0, true);
} else {
this.utcOffset(offsetFromString(matchOffset, this._i));
}
}
return this;
Expand Down
6 changes: 6 additions & 0 deletions src/test/moment/utc_offset.js
Expand Up @@ -439,6 +439,12 @@ test('parse zone', function (assert) {
assert.equal(m.hours(), 0);
});

test('parse UTC zone', function (assert) {
var m = moment('2013-01-01T05:00:00+00:00').parseZone();
assert.equal(m.utcOffset(), 0);
assert.equal(m.hours(), 5);
});

test('parse zone static', function (assert) {
var m = moment.parseZone('2013-01-01T00:00:00-13:00');
assert.equal(m.utcOffset(), -13 * 60);
Expand Down