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

Implement timezone interface #3134

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/lib/create/date-from-array.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
export function createDate (y, m, d, h, M, s, ms) {
//can't just apply() to create a date:
//http://stackoverflow.com/questions/181348/instantiating-a-javascript-object-by-calling-prototype-constructor-apply
var date = new Date(y, m, d, h, M, s, ms);

//the date constructor remaps years 0-99 to 1900-1999
if (y < 100 && y >= 0 && isFinite(date.getFullYear())) {
date.setFullYear(y);
}
return date;
}

export function createUTCDate (y) {
var date = new Date(Date.UTC.apply(null, arguments));

Expand Down
46 changes: 25 additions & 21 deletions src/lib/create/from-anything.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ export function prepareConfig (config) {
configFromStringAndArray(config);
} else if (format) {
configFromStringAndFormat(config);
} else if (isDate(input)) {
config._d = input;
} else {
configFromInput(config);
}
Expand All @@ -59,43 +57,49 @@ export function prepareConfig (config) {
}

function configFromInput(config) {
var input = config._i;
var input = config._i,
type = typeof input;
if (input === undefined) {
config._d = new Date(hooks.now());
config._offset = 0;
} else if (isDate(input)) {
config._d = new Date(input.valueOf());
} else if (typeof input === 'string') {
config._offset = 0;
} else if (type === 'string') {
configFromString(config);
} else if (isArray(input)) {
config._a = map(input.slice(0), function (obj) {
return parseInt(obj, 10);
});
configFromArray(config);
} else if (typeof(input) === 'object') {
} else if (type === 'object') {
configFromObject(config);
} else if (typeof(input) === 'number') {
} else if (type === 'number') {
// from milliseconds
config._d = new Date(input);
config._offset = 0;
} else {
hooks.createFromInputFallback(config);
}
}

export function createLocalOrUTC (input, format, locale, strict, isUTC) {
var c = {};
export function createWithTimeZone (timeZone) {
return function (input, format, locale, strict) {
var c = {};

if (typeof(locale) === 'boolean') {
strict = locale;
locale = undefined;
}
// object construction must be done this way.
// https://github.com/moment/moment/issues/1423
c._isAMomentObject = true;
c._useUTC = c._isUTC = isUTC;
c._l = locale;
c._i = input;
c._f = format;
c._strict = strict;
if (typeof(locale) === 'boolean') {
strict = locale;
locale = undefined;
}
// object construction must be done this way.
// https://github.com/moment/moment/issues/1423
c._isAMomentObject = true;
c._z = timeZone;
c._l = locale;
c._i = input;
c._f = format;
c._strict = strict;

return createFromConfig(c);
return createFromConfig(c);
};
}
14 changes: 3 additions & 11 deletions src/lib/create/from-array.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { hooks } from '../utils/hooks';
import { createDate, createUTCDate } from './date-from-array';
import { createUTCDate } from './date-from-array';
import { daysInYear } from '../units/year';
import { weekOfYear, weeksInYear, dayOfYearFromWeeks } from '../units/week-calendar-utils';
import { YEAR, MONTH, DATE, HOUR, MINUTE, SECOND, MILLISECOND } from '../units/constants';
Expand All @@ -10,10 +10,7 @@ import getParsingFlags from './parsing-flags';
function currentDateArray(config) {
// hooks is actually the exported moment object
var nowValue = new Date(hooks.now());
if (config._useUTC) {
return [nowValue.getUTCFullYear(), nowValue.getUTCMonth(), nowValue.getUTCDate()];
}
return [nowValue.getFullYear(), nowValue.getMonth(), nowValue.getDate()];
return [nowValue.getUTCFullYear(), nowValue.getUTCMonth(), nowValue.getUTCDate()];
}

// convert an array to a date.
Expand Down Expand Up @@ -70,12 +67,7 @@ export function configFromArray (config) {
config._a[HOUR] = 0;
}

config._d = (config._useUTC ? createUTCDate : createDate).apply(null, input);
// Apply timezone offset from input. The actual utcOffset can be changed
// with parseZone.
if (config._tzm != null) {
config._d.setUTCMinutes(config._d.getUTCMinutes() - config._tzm);
}
config._d = createUTCDate.apply(null, input);

if (config._nextDay) {
config._a[HOUR] = 24;
Expand Down
3 changes: 0 additions & 3 deletions src/lib/create/from-string-and-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ export function configFromStringAndArray(config) {
for (i = 0; i < config._f.length; i++) {
currentScore = 0;
tempConfig = copyConfig({}, config);
if (config._useUTC != null) {
tempConfig._useUTC = config._useUTC;
}
tempConfig._f = config._f[i];
configFromStringAndFormat(tempConfig);

Expand Down
3 changes: 2 additions & 1 deletion src/lib/create/from-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export function configFromString(config) {

if (matched !== null) {
config._d = new Date(+matched[1]);
config._offset = 0;
return;
}

Expand All @@ -115,6 +116,6 @@ hooks.createFromInputFallback = deprecate(
'release. Please refer to ' +
'https://github.com/moment/moment/issues/1407 for more info.',
function (config) {
config._d = new Date(config._i + (config._useUTC ? ' UTC' : ''));
config._d = new Date(config._i);
}
);
7 changes: 3 additions & 4 deletions src/lib/create/local.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createLocalOrUTC } from './from-anything';
import LocalTimeZone from '../timezone/local';
import { createWithTimeZone } from './from-anything';

export function createLocal (input, format, locale, strict) {
return createLocalOrUTC(input, format, locale, strict, false);
}
export var createLocal = createWithTimeZone(new LocalTimeZone());
9 changes: 6 additions & 3 deletions src/lib/create/utc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { createLocalOrUTC } from './from-anything';
import FixedOffsetTimeZone from '../timezone/fixed-offset';
import { createWithTimeZone } from './from-anything';

export function createUTC (input, format, locale, strict) {
return createLocalOrUTC(input, format, locale, strict, true).utc();
var create = createWithTimeZone(new FixedOffsetTimeZone(0));

export function createUTC () {
return create.apply(null, arguments).utcOffset(0);
}
1 change: 0 additions & 1 deletion src/lib/duration/bubble.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import absFloor from '../utils/abs-floor';
import absCeil from '../utils/abs-ceil';
import { createUTCDate } from '../create/date-from-array';

export function bubble () {
var milliseconds = this._milliseconds;
Expand Down
22 changes: 8 additions & 14 deletions src/lib/moment/add-subtract.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { get, set } from './get-set';
import { setMonth } from '../units/month';
import { createDuration } from '../duration/create';
import { deprecateSimple } from '../utils/deprecate';
import { hooks } from '../utils/hooks';
import updateOffset from '../timezone/update-offset';
import absRound from '../utils/abs-round';


// TODO: remove 'name' arg after deprecation is removed
function createAdder(direction, name) {
return function (val, period) {
Expand All @@ -23,32 +21,28 @@ function createAdder(direction, name) {
};
}

export function addSubtract (mom, duration, isAdding, updateOffset) {
export function addSubtract (mom, duration, isAdding) {
var milliseconds = duration._milliseconds,
days = absRound(duration._days),
months = absRound(duration._months);
months = absRound(duration._months),
date = mom._d;

if (!mom.isValid()) {
// No op
return;
}

updateOffset = updateOffset == null ? true : updateOffset;

if (milliseconds) {
mom._d.setTime(mom._d.valueOf() + milliseconds * isAdding);
date.setTime(date.valueOf() + milliseconds * isAdding);
}
if (days) {
set(mom, 'Date', get(mom, 'Date') + days * isAdding);
date.setUTCDate(date.getUTCDate() + days * isAdding);
}
if (months) {
setMonth(mom, get(mom, 'Month') + months * isAdding);
}
if (updateOffset) {
hooks.updateOffset(mom, days || months);
setMonth(mom, date.getUTCMonth() + months * isAdding);
}
updateOffset(mom, days || months);
}

export var add = createAdder(1, 'add');
export var subtract = createAdder(-1, 'subtract');

33 changes: 3 additions & 30 deletions src/lib/moment/constructor.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { hooks } from '../utils/hooks';
import hasOwnProp from '../utils/has-own-prop';
import isUndefined from '../utils/is-undefined';
import getParsingFlags from '../create/parsing-flags';

// Plugins that add properties should also add the key here (null value),
// so we can properly clone ourselves.
var momentProperties = hooks.momentProperties = [];
import updateOffset from '../timezone/update-offset';

export function copyConfig(to, from) {
var i, prop, val;

if (!isUndefined(from._isAMomentObject)) {
to._isAMomentObject = from._isAMomentObject;
}
Expand All @@ -28,9 +21,6 @@ export function copyConfig(to, from) {
if (!isUndefined(from._tzm)) {
to._tzm = from._tzm;
}
if (!isUndefined(from._isUTC)) {
to._isUTC = from._isUTC;
}
if (!isUndefined(from._offset)) {
to._offset = from._offset;
}
Expand All @@ -40,33 +30,16 @@ export function copyConfig(to, from) {
if (!isUndefined(from._locale)) {
to._locale = from._locale;
}

if (momentProperties.length > 0) {
for (i in momentProperties) {
prop = momentProperties[i];
val = from[prop];
if (!isUndefined(val)) {
to[prop] = val;
}
}
}
to._z = from._z;

return to;
}

var updateInProgress = false;

// Moment prototype object
export function Moment(config) {
copyConfig(this, config);
this._d = new Date(config._d != null ? config._d.getTime() : NaN);
// Prevent infinite loop in case updateOffset creates new moment
// objects.
if (updateInProgress === false) {
updateInProgress = true;
hooks.updateOffset(this);
updateInProgress = false;
}
updateOffset(this, this._offset == null);
}

export function isMoment (obj) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/moment/creation-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export function creationData() {
input: this._i,
format: this._f,
locale: this._locale,
isUTC: this._isUTC,
timeZone: this._z,
strict: this._strict
};
}
9 changes: 4 additions & 5 deletions src/lib/moment/get-set.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { normalizeUnits } from '../units/aliases';
import { hooks } from '../utils/hooks';
import updateOffset from '../timezone/update-offset';
import isFunction from '../utils/is-function';

export function makeGetSet (unit, keepTime) {
return function (value) {
if (value != null) {
set(this, unit, value);
hooks.updateOffset(this, keepTime);
return this;
return updateOffset(this, keepTime);
} else {
return get(this, unit);
}
Expand All @@ -16,12 +15,12 @@ export function makeGetSet (unit, keepTime) {

export function get (mom, unit) {
return mom.isValid() ?
mom._d['get' + (mom._isUTC ? 'UTC' : '') + unit]() : NaN;
mom._d['getUTC' + unit]() : NaN;
}

export function set (mom, unit, value) {
if (mom.isValid()) {
mom._d['set' + (mom._isUTC ? 'UTC' : '') + unit](value);
mom._d['setUTC' + unit](value);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib/moment/moment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createLocal } from '../create/local';
import { createUTC } from '../create/utc';
import { createWithTimeZone } from '../create/from-anything';
import { createInvalid } from '../create/valid';
import { isMoment } from './constructor';
import { min, max } from './min-max';
Expand All @@ -23,6 +24,7 @@ export {
createUnix,
createLocal,
createInZone,
createWithTimeZone,
createInvalid,
momentPrototype
};
15 changes: 15 additions & 0 deletions src/lib/timezone/fixed-offset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default function FixedOffsetTimeZone (offset) {
this._offset = offset;
}

FixedOffsetTimeZone.prototype.parse = function (timestamp) {
return this._offset;
};

FixedOffsetTimeZone.prototype.abbr = function (timestamp) {
return 'UTC';
};

FixedOffsetTimeZone.prototype.name = function (timestamp) {
return 'Coordinated Universal Time';
};
24 changes: 24 additions & 0 deletions src/lib/timezone/local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export default function LocalTimeZone () {}

function getTimezoneOffset (date) {
// On Firefox.24 Date#getTimezoneOffset returns a floating point.
// https://github.com/moment/moment/pull/1871
return -Math.round(date.getTimezoneOffset() / 15) * 15;
}

LocalTimeZone.prototype.parse = function (timestamp) {
var asUtc = new Date(timestamp);
return getTimezoneOffset(new Date(
asUtc.getUTCFullYear(),
asUtc.getUTCMonth(),
asUtc.getUTCDate(),
asUtc.getUTCHours(),
asUtc.getUTCMinutes(),
asUtc.getUTCSeconds(),
asUtc.getUTCMilliseconds()
));
};

LocalTimeZone.prototype.abbr = function (timestamp) {
return '';
};
Loading