Skip to content

Commit

Permalink
Switch zone to utcOffset in some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ichernev committed Dec 24, 2014
1 parent c4b9a71 commit 9f15c47
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 52 deletions.
8 changes: 5 additions & 3 deletions test/moment/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,16 +486,18 @@ exports.create = {
},

'parsing iso' : function (test) {
var offset = moment([2011, 9, 8]).zone(),
var offset = moment([2011, 9, 8]).utcOffset(),
pad = function (input) {
if (input < 10) {
return '0' + input;
}
return '' + input;
},
hourOffset = (offset > 0) ? Math.floor(offset / 60) : Math.ceil(offset / 60),
hourOffset = (offset > 0 ? Math.floor(offset / 60) : Math.ceil(offset / 60)),
minOffset = offset - (hourOffset * 60),
tz = (offset > 0) ? '-' + pad(hourOffset) + ':' + pad(minOffset) : '+' + pad(-hourOffset) + ':' + pad(-minOffset),
tz = (offset > 0 ?
'+' + pad(hourOffset) + ':' + pad(minOffset) :
'-' + pad(-hourOffset) + ':' + pad(-minOffset)),
tz2 = tz.replace(':', ''),
tz3 = tz2.slice(0, 3),
formats = [
Expand Down
10 changes: 5 additions & 5 deletions test/moment/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function dstForYear(year) {
while (current < end) {
last = current.clone();
current.add(24, 'hour');
if (last.zone() !== current.zone()) {
if (last.utcOffset() !== current.utcOffset()) {
end = current.clone();
current = last.clone();
break;
Expand All @@ -23,10 +23,10 @@ function dstForYear(year) {
while (current < end) {
last = current.clone();
current.add(1, 'hour');
if (last.zone() !== current.zone()) {
if (last.utcOffset() !== current.utcOffset()) {
return {
moment : last,
diff : (current.zone() - last.zone()) / 60
diff : -(current.utcOffset() - last.utcOffset()) / 60
};
}
}
Expand Down Expand Up @@ -174,8 +174,8 @@ exports.diff = {
},

'diff between utc and local' : function (test) {
if (moment([2012]).zone() === moment([2011]).zone()) {
// Russia's zone offset on 1st of Jan 2012 vs 2011 is different
if (moment([2012]).utcOffset() === moment([2011]).utcOffset()) {
// Russia's utc offset on 1st of Jan 2012 vs 2011 is different
test.equal(moment([2012]).utc().diff([2011], 'years'), 1, 'year diff');
}
test.equal(moment([2010, 2, 2]).utc().diff([2010, 0, 2], 'months'), 2, 'month diff');
Expand Down
35 changes: 10 additions & 25 deletions test/moment/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,13 @@ exports.format = {
'format timezone' : function (test) {
test.expect(2);

var b = moment(new Date(2010, 1, 14, 15, 25, 50, 125)),
explanation = 'moment().format(\'z\') = ' + b.format('z') + ' It should be something like \'PST\'';
if (moment().zone() === -60) {
explanation += 'For UTC+1 this is a known issue, see https://github.com/timrwood/moment/issues/162';
}
var b = moment(new Date(2010, 1, 14, 15, 25, 50, 125));
test.ok(b.format('Z').match(/^[\+\-]\d\d:\d\d$/), b.format('Z') + ' should be something like \'+07:30\'');
test.ok(b.format('ZZ').match(/^[\+\-]\d{4}$/), b.format('ZZ') + ' should be something like \'+0700\'');
test.done();
},

'format multiple with zone' : function (test) {
'format multiple with utc offset' : function (test) {
test.expect(1);

var b = moment('2012-10-08 -1200', ['YYYY-MM-DD HH:mm ZZ', 'YYYY-MM-DD ZZ', 'YYYY-MM-DD']);
Expand Down Expand Up @@ -140,24 +136,13 @@ exports.format = {
test.done();
},

'zone' : function (test) {
test.expect(3);
'utcOffset sanity checks': function (test) {
test.equal(moment().utcOffset() % 15, 0,
'utc offset should be a multiple of 15 (was ' + moment().utcOffset() + ')');

test.equal(moment().utcOffset(), -(new Date()).getTimezoneOffset(),
'utcOffset should return the opposite of getTimezoneOffset');

if (moment().zone() > 0) {
test.ok(moment().format('ZZ').indexOf('-') > -1, 'When the zone() offset is greater than 0, the ISO offset should be less than zero');
}
if (moment().zone() < 0) {
test.ok(moment().format('ZZ').indexOf('+') > -1, 'When the zone() offset is less than 0, the ISO offset should be greater than zero');
}
if (moment().zone() === 0) {
test.ok(moment().format('ZZ').indexOf('+') > -1, 'When the zone() offset is equal to 0, the ISO offset should be positive zero');
}
if (moment().zone() === 0) {
test.equal(moment().zone(), 0, 'moment.fn.zone should be a multiple of 15 (was ' + moment().zone() + ')');
} else {
test.equal(moment().zone() % 15, 0, 'moment.fn.zone should be a multiple of 15 (was ' + moment().zone() + ')');
}
test.equal(moment().zone(), new Date().getTimezoneOffset(), 'zone should equal getTimezoneOffset');
test.done();
},

Expand Down Expand Up @@ -395,8 +380,8 @@ exports.format = {

for (i = 0; i < zones.length; ++i) {
z = zones[i];
a = moment().zone(z).startOf('day').subtract({m: 1});
test.equal(moment(a).zone(z).calendar(), 'Yesterday at 11:59 PM',
a = moment().utcOffset(z).startOf('day').subtract({m: 1});
test.equal(moment(a).utcOffset(z).calendar(), 'Yesterday at 11:59 PM',
'Yesterday at 11:59 PM, not Today, or the wrong time, tz = ' + z);
}

Expand Down
4 changes: 2 additions & 2 deletions test/moment/is_same.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ exports.isSame = {
test.done();
},

'is same with zone\'d moments' : function (test) {
'is same with utc offset moments' : function (test) {
test.expect(3);
test.ok(moment.parseZone('2013-02-01T-05:00').isSame(moment('2013-02-01'), 'year'), 'zoned vs local moment');
test.ok(moment('2013-02-01').isSame(moment('2013-02-01').zone('-05:00'), 'year'), 'local vs zoned moment');
test.ok(moment('2013-02-01').isSame(moment('2013-02-01').utcOffset('-05:00'), 'year'), 'local vs zoned moment');
test.ok(moment.parseZone('2013-02-01T-05:00').isSame(moment.parseZone('2013-02-01T-06:30'), 'year'),
'zoned vs (differently) zoned moment');
test.done();
Expand Down
12 changes: 6 additions & 6 deletions test/moment/sod_eod.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ exports.endStartOf = {

moment.updateOffset = function (mom, keepTime) {
if (mom.isBefore(dstAt)) {
mom.zone(8, keepTime);
mom.utcOffset(-8, keepTime);
} else {
mom.zone(7, keepTime);
mom.utcOffset(-7, keepTime);
}
};

Expand Down Expand Up @@ -393,9 +393,9 @@ exports.endStartOf = {

moment.updateOffset = function (mom, keepTime) {
if (mom.isBefore(dstAt)) {
mom.zone(7, keepTime);
mom.utcOffset(-7, keepTime);
} else {
mom.zone(8, keepTime);
mom.utcOffset(-8, keepTime);
}
};

Expand All @@ -409,13 +409,13 @@ exports.endStartOf = {
test.equal(m.format(), '2014-11-02T00:00:00-07:00',
'startOf(\'day\') across -1');

// note that zone is -8
// note that utc offset is -8
m = moment('2014-11-02T01:30:00-08:00').parseZone();
m.startOf('h');
test.equal(m.format(), '2014-11-02T01:00:00-08:00',
'startOf(\'hour\') after +1');

// note that zone is -7
// note that utc offset is -7
m = moment('2014-11-02T01:30:00-07:00').parseZone();
m.startOf('h');
test.equal(m.format(), '2014-11-02T01:00:00-07:00',
Expand Down
22 changes: 11 additions & 11 deletions test/moment/utc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ exports.utc = {
'utc and local' : function (test) {
test.expect(7);

var m = moment(Date.UTC(2011, 1, 2, 3, 4, 5, 6)), zone, expected;
var m = moment(Date.UTC(2011, 1, 2, 3, 4, 5, 6)), offset, expected;
m.utc();
// utc
test.equal(m.date(), 2, 'the day should be correct for utc');
Expand All @@ -27,17 +27,17 @@ exports.utc = {

// local
m.local();
if (m.zone() > 180) {
if (m.utcOffset() < -180) {
test.equal(m.date(), 1, 'the date should be correct for local');
test.equal(m.day(), 2, 'the day should be correct for local');
} else {
test.equal(m.date(), 2, 'the date should be correct for local');
test.equal(m.day(), 3, 'the day should be correct for local');
}
zone = Math.ceil(m.zone() / 60);
expected = (24 + 3 - zone) % 24;
offset = Math.ceil(m.utcOffset() / 60);
expected = (24 + 3 + offset) % 24;
test.equal(m.hours(), expected, 'the hours (' + m.hours() + ') should be correct for local');
test.equal(moment().utc().zone(), 0, 'timezone in utc should always be zero');
test.equal(moment().utc().utcOffset(), 0, 'timezone in utc should always be zero');

test.done();
},
Expand Down Expand Up @@ -87,16 +87,16 @@ exports.utc = {
test.done();
},

'cloning with utc' : function (test) {
'cloning with utc offset' : function (test) {
test.expect(4);

var m = moment.utc('2012-01-02T08:20:00');
test.equal(moment.utc(m)._isUTC, true, 'the local zone should be converted to UTC');
test.equal(moment.utc(m.clone().utc())._isUTC, true, 'the local zone should stay in UTC');
test.equal(moment.utc(m)._isUTC, true, 'the local offset should be converted to UTC');
test.equal(moment.utc(m.clone().utc())._isUTC, true, 'the local offset should stay in UTC');

m.zone(120);
test.equal(moment.utc(m)._isUTC, true, 'the explicit zone should stay in UTC');
test.equal(moment.utc(m).zone(), 0, 'the explicit zone should have an offset of 0');
m.utcOffset(120);
test.equal(moment.utc(m)._isUTC, true, 'the explicit utc offset should stay in UTC');
test.equal(moment.utc(m).utcOffset(), 0, 'the explicit utc offset should have an offset of 0');

test.done();
},
Expand Down

0 comments on commit 9f15c47

Please sign in to comment.