},
'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 = [
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;
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
};
}
}
},
'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');
'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']);
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();
},
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);
}
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();
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);
}
};
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);
}
};
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',
'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');
// 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();
},
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();
},