From fe90bb6944f2ff1969ca975954d303b449dfa95b Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 5 Oct 2020 20:33:19 +0800 Subject: [PATCH] fix: Fix objectSupport plugin bug in UTC (#1107) fix #1105 --- src/plugin/objectSupport/index.js | 10 +++++----- test/plugin/objectSupport.test.js | 11 ++++++++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js index 6d161b8de..33b5a62da 100644 --- a/src/plugin/objectSupport/index.js +++ b/src/plugin/objectSupport/index.js @@ -1,4 +1,4 @@ -export default (o, c) => { +export default (o, c, dayjs) => { const proto = c.prototype const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) && obj instanceof Object const prettyUnit = (u) => { @@ -9,13 +9,13 @@ export default (o, c) => { const { date, utc } = cfg const $d = {} if (isObject(date)) { - const now = new Date() + const now = utc ? dayjs.utc() : dayjs() Object.keys(date).forEach((k) => { $d[prettyUnit(k)] = date[k] }) - const d = $d.day || ((!$d.year && !($d.month >= 0)) ? now.getDate() : 1) - const y = $d.year || now.getFullYear() - const M = $d.month >= 0 ? $d.month : ((!$d.year && !$d.day) ? now.getMonth() : 0)// eslint-disable-line no-nested-ternary,max-len + const d = $d.day || ((!$d.year && !($d.month >= 0)) ? now.date() : 1) + const y = $d.year || now.year() + const M = $d.month >= 0 ? $d.month : ((!$d.year && !$d.day) ? now.month() : 0)// eslint-disable-line no-nested-ternary,max-len const h = $d.hour || 0 const m = $d.minute || 0 const s = $d.second || 0 diff --git a/test/plugin/objectSupport.test.js b/test/plugin/objectSupport.test.js index 476b97f4c..392bdbf7c 100755 --- a/test/plugin/objectSupport.test.js +++ b/test/plugin/objectSupport.test.js @@ -21,6 +21,9 @@ const now = new Date() const currentYear = now.getFullYear() const currentMonth = utils.s(now.getMonth() + 1, 2, '0') const currentDate = utils.s(now.getDate(), 2, '0') +const currentUTCYear = now.getUTCFullYear() +const currentUTCMonth = utils.s(now.getUTCMonth() + 1, 2, '0') +const currentUTCDate = utils.s(now.getUTCDate(), 2, '0') const fmt = 'YYYY-MM-DD HH:mm:ss.SSS' const tests = [ [{ year: 2010 }, '2010-01-01 00:00:00.000'], @@ -31,7 +34,8 @@ const tests = [ { hour: 15, minute: 25, second: 50, millisecond: 125 }, - `${currentYear}-${currentMonth}-${currentDate} 15:25:50.125`], + `${currentYear}-${currentMonth}-${currentDate} 15:25:50.125`, + `${currentUTCYear}-${currentUTCMonth}-${currentUTCDate} 15:25:50.125`], [ { year: 2010, month: 1, day: 12, hours: 1 @@ -119,8 +123,9 @@ it('Constructor from Object', () => { it('Constructor from Object UTC', () => { for (let i = 0; i < tests.length; i += 1) { - expect(dayjs.utc(tests[i][0]).format(fmt)).toBe(tests[i][1]) - expect(moment.utc(tests[i][0]).format(fmt)).toBe(tests[i][1]) + const result = tests[i][2] || tests[i][1] + expect(dayjs.utc(tests[i][0]).format(fmt)).toBe(result) + expect(moment.utc(tests[i][0]).format(fmt)).toBe(result) } }) it('Set from Object', () => {