]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
implement support for retrieving current date from alternate clock sources
authorAaron Ogata <atogata@notebowl.com>
Sun, 22 Nov 2015 22:37:47 +0000 (15:37 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Wed, 9 Dec 2015 08:13:12 +0000 (00:13 -0800)
src/lib/create/from-anything.js
src/lib/create/from-array.js
src/lib/moment/moment.js
src/lib/moment/now.js [new file with mode: 0644]
src/lib/moment/prototype.js
src/moment.js
src/test/moment/now.js [new file with mode: 0644]

index b3897027fa6074beaaf989428867aa1c3bed0931..dd99f9d519d244de6cce67d8ec75e5d018cf9fdf 100644 (file)
@@ -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') {
index fdf7804e285472313b855f670b6da49e72ad4c86..c1bb6b56f9e3cccd5316b02465c7bcadf4afd04e 100644 (file)
@@ -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.
index cf73b9f71d0c7731144c4964c6d07a27f1262c06..12eb5f15ab8b8f0047f1bbed599e1d11781ca3c0 100644 (file)
@@ -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 (file)
index 0000000..42631b5
--- /dev/null
@@ -0,0 +1,3 @@
+export var now = Date.now || function () {
+    return +(new Date());
+};
index 39775880bfee7b7b9076233ce3e9c72eb17b105e..17e9f6477dd8b47cd287a8e348531d8a158160d8 100644 (file)
@@ -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;
index 58a4931319b4a0555e0c137d12cc9c670a32c9ef..fd188ba1c45e4f3b30cc48e29de4fb5421d833a6 100644 (file)
@@ -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 (file)
index 0000000..d65a5ca
--- /dev/null
@@ -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;
+    }
+});