From: Aaron Ogata Date: Sun, 22 Nov 2015 22:37:47 +0000 (-0700) Subject: implement support for retrieving current date from alternate clock sources X-Git-Tag: 2.11.0~18^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f54a32f4c62c12a710335211a73cb002a7249646;p=thirdparty%2Fmoment.git implement support for retrieving current date from alternate clock sources --- diff --git a/src/lib/create/from-anything.js b/src/lib/create/from-anything.js index b3897027f..dd99f9d51 100644 --- a/src/lib/create/from-anything.js +++ b/src/lib/create/from-anything.js @@ -61,7 +61,7 @@ export function prepareConfig (config) { function configFromInput(config) { var input = config._i; if (input === undefined) { - config._d = new Date(); + config._d = new Date(Moment.prototype.now()); } else if (isDate(input)) { config._d = new Date(+input); } else if (typeof input === 'string') { diff --git a/src/lib/create/from-array.js b/src/lib/create/from-array.js index fdf7804e2..c1bb6b56f 100644 --- a/src/lib/create/from-array.js +++ b/src/lib/create/from-array.js @@ -3,15 +3,16 @@ 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'; import { createLocal } from './local'; +import { Moment } from '../moment/constructor'; import defaults from '../utils/defaults'; import getParsingFlags from './parsing-flags'; function currentDateArray(config) { - var now = new Date(); + var nowValue = new Date(Moment.prototype.now()); if (config._useUTC) { - return [now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()]; + return [nowValue.getUTCFullYear(), nowValue.getUTCMonth(), nowValue.getUTCDate()]; } - return [now.getFullYear(), now.getMonth(), now.getDate()]; + return [nowValue.getFullYear(), nowValue.getMonth(), nowValue.getDate()]; } // convert an array to a date. diff --git a/src/lib/moment/moment.js b/src/lib/moment/moment.js index cf73b9f71..12eb5f15a 100644 --- a/src/lib/moment/moment.js +++ b/src/lib/moment/moment.js @@ -3,6 +3,7 @@ import { createUTC } from '../create/utc'; import { createInvalid } from '../create/valid'; import { isMoment } from './constructor'; import { min, max } from './min-max'; +import { now } from './now'; import momentPrototype from './prototype'; function createUnix (input) { @@ -14,6 +15,7 @@ function createInZone () { } export { + now, min, max, isMoment, diff --git a/src/lib/moment/now.js b/src/lib/moment/now.js new file mode 100644 index 000000000..42631b597 --- /dev/null +++ b/src/lib/moment/now.js @@ -0,0 +1,3 @@ +export var now = Date.now || function () { + return +(new Date()); +}; diff --git a/src/lib/moment/prototype.js b/src/lib/moment/prototype.js index 39775880b..17e9f6477 100644 --- a/src/lib/moment/prototype.js +++ b/src/lib/moment/prototype.js @@ -13,6 +13,7 @@ import { to, toNow } from './to'; import { getSet } from './get-set'; import { locale, localeData, lang } from './locale'; import { prototypeMin, prototypeMax } from './min-max'; +import { now } from './now'; import { startOf, endOf } from './start-end-of'; import { valueOf, toDate, toArray, toObject, toJSON, unix } from './to-type'; import { isValid, parsingFlags, invalidAt } from './valid'; @@ -42,6 +43,7 @@ proto.locale = locale; proto.localeData = localeData; proto.max = prototypeMax; proto.min = prototypeMin; +proto.now = now; proto.parsingFlags = parsingFlags; proto.set = getSet; proto.startOf = startOf; diff --git a/src/moment.js b/src/moment.js index 58a493131..fd188ba1c 100644 --- a/src/moment.js +++ b/src/moment.js @@ -11,6 +11,7 @@ moment.version = '2.10.6'; import { min, max, + now, isMoment, momentPrototype as fn, createUTC as utc, @@ -46,6 +47,7 @@ setHookCallback(local); moment.fn = fn; moment.min = min; moment.max = max; +moment.now = now; moment.utc = utc; moment.unix = unix; moment.months = months; diff --git a/src/test/moment/now.js b/src/test/moment/now.js new file mode 100644 index 000000000..d65a5ca17 --- /dev/null +++ b/src/test/moment/now.js @@ -0,0 +1,31 @@ +import { module, test } from '../qunit'; +import moment from '../../moment'; + +module('now'); + +test('now', function (assert) { + var startOfTest = new Date().valueOf(), + momentNowTime = moment.now(), + afterMomentCreationTime = new Date().valueOf(); + + assert.ok(startOfTest <= momentNowTime, 'moment now() time should be now, not in the past'); + assert.ok(momentNowTime <= afterMomentCreationTime, 'moment now() time should be now, not in the future'); +}); + +test('now - custom value', function (assert) { + var CUSTOM_DATE = '2015-01-01T01:30:00.000Z'; + + var oldFn = moment.fn.now; + + moment.fn.now = function () { + return new Date(CUSTOM_DATE).valueOf(); + }; + + try { + assert.ok(moment().toISOString() === CUSTOM_DATE, 'moment() constructor should use the function defined by moment.now, but it did not'); + assert.ok(moment([]).toISOString() === '2014-12-31T07:00:00.000Z', 'moment() constructor should fall back to the date defined by moment.now when an empty array is given, but it did not'); + assert.ok(moment.utc([]).toISOString() === '2015-01-01T00:00:00.000Z', 'moment() constructor should fall back to the date defined by moment.now when an empty array is given, but it did not'); + } finally { + moment.fn.now = oldFn; + } +});