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

Default timezone #152

Merged
merged 5 commits into from
Dec 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions moment-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,13 @@

moment.tz = tz;

moment.defaultZone = null;

moment.updateOffset = function (mom, keepTime) {
var offset;
if (mom._z === undefined) {
mom._z = moment.defaultZone;
}
if (mom._z) {
offset = mom._z.offset(mom);
if (Math.abs(offset) < 16) {
Expand Down Expand Up @@ -384,6 +389,11 @@
fn.zoneAbbr = abbrWrap(fn.zoneAbbr);
fn.utc = resetZoneWrap(fn.utc);

moment.tz.setDefault = function(name) {
moment.defaultZone = name ? getZone(name) : null;
return moment;
};

// Cloning a moment should include the _z property.
var momentProperties = moment.momentProperties;
if (Object.prototype.toString.call(momentProperties) === '[object Array]') {
Expand Down
76 changes: 76 additions & 0 deletions tests/moment-timezone/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"use strict";

var moment = require("../../");

exports.default = {
defaultZone : function (t) {
t.equal(
moment.defaultZone,
null,
'initial default zone should be null'
);
t.equal(
moment.tz.setDefault('America/New_York').defaultZone.name,
'America/New_York',
'calling moment.tz.setDefault with a valid timezone should expose it via defaultZone'
);
t.equal(
moment.tz.setDefault().defaultZone,
null,
'calling moment.tz.setDefault with a falsey argument should unset defaultZone'
);
t.done();
},
normal: function (t) {
moment.tz.setDefault('America/New_York');
var m = moment();
t.equal(
m._z.name,
'America/New_York',
'creating moments should set their default timezone if it is set'
);

moment.tz.setDefault();
var m2 = moment();
moment.tz.setDefault('America/New_York');
m2.millisecond(0);
t.equal(
m2._z,
null,
'calling updateOffset on moments created before setting a default timezone should not affect their timezone'
);
moment.tz.setDefault();
t.done();
},
utc : function (t) {
moment.tz.setDefault('America/New_York');
t.equal(
moment.utc().format('ZZ'),
'+0000',
'creating moments in UTC mode should ignore default timezone'
);
t.notEqual(
moment().format('ZZ'),
'+0000',
'using UTC mode should not affect normal moment creation'
);

var utc_moment = moment.utc(),
normal_moment = moment(),
normal_moment_offset = normal_moment.format('ZZ');
moment.tz.setDefault();
utc_moment.millisecond(0);
normal_moment.millisecond(0);
t.equal(
utc_moment.format('ZZ'),
'+0000',
'resetting default timezone should not affect existing moments created in UTC mode'
);
t.equal(
normal_moment.format('ZZ'),
normal_moment_offset,
'resetting default timezone should not affect existing moments'
);
t.done();
}
};