From 018d293891e38044ed21ed154605caa717fcab45 Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 7 Mar 2019 17:26:07 +0800 Subject: [PATCH 01/17] init setUTCOffset plugin --- src/index.js | 3 ++- src/plugin/setUTCOffset/index.js | 29 +++++++++++++++++++++++++++++ test/plugin/setUTCOffset.test.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/plugin/setUTCOffset/index.js create mode 100644 test/plugin/setUTCOffset.test.js diff --git a/src/index.js b/src/index.js index 434f60362..c70291f2f 100644 --- a/src/index.js +++ b/src/index.js @@ -38,7 +38,8 @@ const dayjs = (date, c, pl) => { return new Dayjs(cfg) // eslint-disable-line no-use-before-define } -const wrapper = (date, instance) => dayjs(date, { locale: instance.$L, utc: instance.$u }) +const wrapper = (date, instance) => + dayjs(date, { locale: instance.$L, utc: instance.$u, $offset: instance.$offset }) const Utils = U // for plugin use Utils.l = parseLocale diff --git a/src/plugin/setUTCOffset/index.js b/src/plugin/setUTCOffset/index.js new file mode 100644 index 000000000..6c368888a --- /dev/null +++ b/src/plugin/setUTCOffset/index.js @@ -0,0 +1,29 @@ +export default (option, Dayjs) => { + const proto = Dayjs.prototype + const oldParse = proto.parse + proto.parse = function (cfg) { + if (!this.$utils().u(cfg.$offset)) { + this.$offset = cfg.$offset + } + oldParse.call(this, cfg) + } + + const oldUtcOffset = proto.utcOffset + proto.utcOffset = function (input) { + const { u } = this.$utils() + if (u(input)) { + if (this.$u) { + return 0 + } + if (!u(this.$offset)) { + return this.$offset + } + return oldUtcOffset.call(this) + } + const offset = Math.abs(input) <= 16 ? input * 60 : input + this.$offset = offset + return this.add(offset + this.$d.getTimezoneOffset(), 'minute') + } + + // todo toString valueOf toDate shoud minus back to original date +} diff --git a/test/plugin/setUTCOffset.test.js b/test/plugin/setUTCOffset.test.js new file mode 100644 index 000000000..8006ec5be --- /dev/null +++ b/test/plugin/setUTCOffset.test.js @@ -0,0 +1,28 @@ +import MockDate from 'mockdate' +import moment from 'moment' +import dayjs from '../../src' +import setUTCOffset from '../../src/plugin/setUTCOffset' + +dayjs.extend(setUTCOffset) + +beforeEach(() => { + MockDate.set(new Date()) +}) + +afterEach(() => { + MockDate.reset() +}) + +it('UTC Year', () => { + expect(dayjs().utcOffset(540).utcOffset()).toBe(moment().utcOffset(540).utcOffset()) + expect(dayjs().utcOffset(540).format()).toBe(moment().utcOffset(540).format()) + expect(dayjs().utcOffset(60).format()).toBe(moment().utcOffset(60).format()) + expect(dayjs().utcOffset(8).format()).toBe(moment().utcOffset(8).format()) + + expect(dayjs().utcOffset(-540).utcOffset()).toBe(moment().utcOffset(-540).utcOffset()) + expect(dayjs().utcOffset(-540).format()).toBe(moment().utcOffset(-540).format()) + + expect(dayjs().utcOffset(-60).format()).toBe(moment().utcOffset(-60).format()) + expect(dayjs().utcOffset(-8).format()).toBe(moment().utcOffset(-8).format()) + // expect(dayjs().utcOffset(0).format()).toBe(moment().utcOffset(0).format()) +}) From 079bec30cec48d17fdd2dae9e9efe8167b678a6c Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 16:01:54 +0800 Subject: [PATCH 02/17] update --- test/plugin/setUTCOffset.test.js | 2 +- types/plugin/setUtcOffset.d.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 types/plugin/setUtcOffset.d.ts diff --git a/test/plugin/setUTCOffset.test.js b/test/plugin/setUTCOffset.test.js index 8006ec5be..5ab4b2d04 100644 --- a/test/plugin/setUTCOffset.test.js +++ b/test/plugin/setUTCOffset.test.js @@ -13,7 +13,7 @@ afterEach(() => { MockDate.reset() }) -it('UTC Year', () => { +it('Set utcOffset -> Get utcOffset', () => { expect(dayjs().utcOffset(540).utcOffset()).toBe(moment().utcOffset(540).utcOffset()) expect(dayjs().utcOffset(540).format()).toBe(moment().utcOffset(540).format()) expect(dayjs().utcOffset(60).format()).toBe(moment().utcOffset(60).format()) diff --git a/types/plugin/setUtcOffset.d.ts b/types/plugin/setUtcOffset.d.ts new file mode 100644 index 000000000..30ec75e5d --- /dev/null +++ b/types/plugin/setUtcOffset.d.ts @@ -0,0 +1,4 @@ +import { PluginFunc } from 'dayjs' + +declare const plugin: PluginFunc +export = plugin From 4b3c987977c756d2fad63f151965d16a959d6fb7 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 17:10:06 +0800 Subject: [PATCH 03/17] update valueOf --- src/index.js | 4 ++-- src/plugin/setUTCOffset/index.js | 7 ++++++- test/plugin/setUTCOffset.test.js | 4 ++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index c70291f2f..70c8ecb33 100644 --- a/src/index.js +++ b/src/index.js @@ -264,7 +264,7 @@ class Dayjs { [C.S]: C.MILLISECONDS_A_SECOND }[unit] || 1 // ms - const nextTimeStamp = this.valueOf() + (number * step) + const nextTimeStamp = this.$d.getTime() + (number * step) return Utils.w(nextTimeStamp, this) } @@ -371,7 +371,7 @@ class Dayjs { } toDate() { - return new Date(this.$d) + return new Date(this.valueOf()) } toJSON() { diff --git a/src/plugin/setUTCOffset/index.js b/src/plugin/setUTCOffset/index.js index 6c368888a..19ec9a6c7 100644 --- a/src/plugin/setUTCOffset/index.js +++ b/src/plugin/setUTCOffset/index.js @@ -1,6 +1,7 @@ export default (option, Dayjs) => { const proto = Dayjs.prototype const oldParse = proto.parse + const localOffset = (new Date()).getTimezoneOffset() proto.parse = function (cfg) { if (!this.$utils().u(cfg.$offset)) { this.$offset = cfg.$offset @@ -8,6 +9,10 @@ export default (option, Dayjs) => { oldParse.call(this, cfg) } + proto.valueOf = function () { + return this.$d.valueOf() - (((this.$offset || 0) + localOffset) * 60000) + } + const oldUtcOffset = proto.utcOffset proto.utcOffset = function (input) { const { u } = this.$utils() @@ -22,7 +27,7 @@ export default (option, Dayjs) => { } const offset = Math.abs(input) <= 16 ? input * 60 : input this.$offset = offset - return this.add(offset + this.$d.getTimezoneOffset(), 'minute') + return this.add(offset + localOffset, 'minute') } // todo toString valueOf toDate shoud minus back to original date diff --git a/test/plugin/setUTCOffset.test.js b/test/plugin/setUTCOffset.test.js index 5ab4b2d04..26d1ba814 100644 --- a/test/plugin/setUTCOffset.test.js +++ b/test/plugin/setUTCOffset.test.js @@ -26,3 +26,7 @@ it('Set utcOffset -> Get utcOffset', () => { expect(dayjs().utcOffset(-8).format()).toBe(moment().utcOffset(-8).format()) // expect(dayjs().utcOffset(0).format()).toBe(moment().utcOffset(0).format()) }) + +it('Set utcOffset -> Get utcOffset', () => { + expect(dayjs().utcOffset(9).valueOf()).toBe(moment().utcOffset(9).valueOf()) +}) From c853382046c9a0adeb25bd1681a472a3a1f9aa87 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 17:18:34 +0800 Subject: [PATCH 04/17] update valueOf --- src/plugin/setUTCOffset/index.js | 3 ++- test/plugin/setUTCOffset.test.js | 10 ++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/plugin/setUTCOffset/index.js b/src/plugin/setUTCOffset/index.js index 19ec9a6c7..9cd015cb0 100644 --- a/src/plugin/setUTCOffset/index.js +++ b/src/plugin/setUTCOffset/index.js @@ -10,7 +10,8 @@ export default (option, Dayjs) => { } proto.valueOf = function () { - return this.$d.valueOf() - (((this.$offset || 0) + localOffset) * 60000) + const addedOffset = !this.$utils().u(this.$offset) ? (this.$offset || 0) + localOffset : 0 + return this.$d.valueOf() - (addedOffset * 60000) } const oldUtcOffset = proto.utcOffset diff --git a/test/plugin/setUTCOffset.test.js b/test/plugin/setUTCOffset.test.js index 26d1ba814..77d6219bc 100644 --- a/test/plugin/setUTCOffset.test.js +++ b/test/plugin/setUTCOffset.test.js @@ -27,6 +27,12 @@ it('Set utcOffset -> Get utcOffset', () => { // expect(dayjs().utcOffset(0).format()).toBe(moment().utcOffset(0).format()) }) -it('Set utcOffset -> Get utcOffset', () => { - expect(dayjs().utcOffset(9).valueOf()).toBe(moment().utcOffset(9).valueOf()) +it('valueOf should be the same as original', () => { + const d = dayjs() + const du = dayjs().utcOffset(9) + const m = moment() + const mu = moment().utcOffset(9) + expect(d.valueOf()).toBe(m.valueOf()) + expect(d.valueOf()).toBe(du.valueOf()) + expect(du.valueOf()).toBe(mu.valueOf()) }) From b5ce204cc0d497af09b398cf43bed7b6eb71e576 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 17:20:41 +0800 Subject: [PATCH 05/17] update todate --- test/plugin/setUTCOffset.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/plugin/setUTCOffset.test.js b/test/plugin/setUTCOffset.test.js index 77d6219bc..5172f2cca 100644 --- a/test/plugin/setUTCOffset.test.js +++ b/test/plugin/setUTCOffset.test.js @@ -27,12 +27,12 @@ it('Set utcOffset -> Get utcOffset', () => { // expect(dayjs().utcOffset(0).format()).toBe(moment().utcOffset(0).format()) }) -it('valueOf should be the same as original', () => { +it('valueOf, toDate should be the same as original', () => { const d = dayjs() const du = dayjs().utcOffset(9) - const m = moment() const mu = moment().utcOffset(9) - expect(d.valueOf()).toBe(m.valueOf()) expect(d.valueOf()).toBe(du.valueOf()) expect(du.valueOf()).toBe(mu.valueOf()) + expect(d.toDate()).toEqual(du.toDate()) + expect(du.toDate()).toEqual(mu.toDate()) }) From 6f3a6c8e5925e143adcffc04c3177e9ea62f1f09 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 17:25:46 +0800 Subject: [PATCH 06/17] use constant --- src/plugin/setUTCOffset/index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/plugin/setUTCOffset/index.js b/src/plugin/setUTCOffset/index.js index 9cd015cb0..ac5254ec3 100644 --- a/src/plugin/setUTCOffset/index.js +++ b/src/plugin/setUTCOffset/index.js @@ -1,3 +1,5 @@ +import { MILLISECONDS_A_MINUTE, MIN } from '../../constant' + export default (option, Dayjs) => { const proto = Dayjs.prototype const oldParse = proto.parse @@ -11,7 +13,7 @@ export default (option, Dayjs) => { proto.valueOf = function () { const addedOffset = !this.$utils().u(this.$offset) ? (this.$offset || 0) + localOffset : 0 - return this.$d.valueOf() - (addedOffset * 60000) + return this.$d.valueOf() - (addedOffset * MILLISECONDS_A_MINUTE) } const oldUtcOffset = proto.utcOffset @@ -28,7 +30,7 @@ export default (option, Dayjs) => { } const offset = Math.abs(input) <= 16 ? input * 60 : input this.$offset = offset - return this.add(offset + localOffset, 'minute') + return this.add(offset + localOffset, MIN) } // todo toString valueOf toDate shoud minus back to original date From 55a7c861a67f3bc6691b48004ece24b7b0171f45 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 17:49:37 +0800 Subject: [PATCH 07/17] use constant --- src/index.js | 2 +- src/plugin/setUTCOffset/index.js | 5 +++-- test/plugin/setUTCOffset.test.js | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 70c8ecb33..71b76b8a3 100644 --- a/src/index.js +++ b/src/index.js @@ -367,7 +367,7 @@ class Dayjs { } clone() { - return Utils.w(this.toDate(), this) + return Utils.w(this.$d, this) } toDate() { diff --git a/src/plugin/setUTCOffset/index.js b/src/plugin/setUTCOffset/index.js index ac5254ec3..8f5da2f88 100644 --- a/src/plugin/setUTCOffset/index.js +++ b/src/plugin/setUTCOffset/index.js @@ -29,8 +29,9 @@ export default (option, Dayjs) => { return oldUtcOffset.call(this) } const offset = Math.abs(input) <= 16 ? input * 60 : input - this.$offset = offset - return this.add(offset + localOffset, MIN) + const newD = this.add(offset + localOffset, MIN) + newD.$offset = offset + return newD } // todo toString valueOf toDate shoud minus back to original date diff --git a/test/plugin/setUTCOffset.test.js b/test/plugin/setUTCOffset.test.js index 5172f2cca..aeb16ed6a 100644 --- a/test/plugin/setUTCOffset.test.js +++ b/test/plugin/setUTCOffset.test.js @@ -36,3 +36,17 @@ it('valueOf, toDate should be the same as original', () => { expect(d.toDate()).toEqual(du.toDate()) expect(du.toDate()).toEqual(mu.toDate()) }) + +it('clone', () => { + const du = dayjs().utcOffset(9) + const du2 = du.clone() + expect(du.valueOf()).toBe(du2.valueOf()) + expect(du.format()).toBe(du2.format()) +}) + +it('immutable', () => { + const d = dayjs() + const du = d.utcOffset(9) + expect(d.utcOffset()).not.toBe(du.utcOffset()) + expect(d.format()).not.toBe(du.format()) +}) From e7e62450990c3b82952648762280caddd8282635 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 17:56:22 +0800 Subject: [PATCH 08/17] update tostring --- src/plugin/setUTCOffset/index.js | 8 +++++++- test/plugin/setUTCOffset.test.js | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/plugin/setUTCOffset/index.js b/src/plugin/setUTCOffset/index.js index 8f5da2f88..e10cce6fc 100644 --- a/src/plugin/setUTCOffset/index.js +++ b/src/plugin/setUTCOffset/index.js @@ -34,5 +34,11 @@ export default (option, Dayjs) => { return newD } - // todo toString valueOf toDate shoud minus back to original date + proto.toISOString = function () { + return this.toDate().toISOString() + } + + proto.toString = function () { + return this.toDate().toUTCString() + } } diff --git a/test/plugin/setUTCOffset.test.js b/test/plugin/setUTCOffset.test.js index aeb16ed6a..2a939c7db 100644 --- a/test/plugin/setUTCOffset.test.js +++ b/test/plugin/setUTCOffset.test.js @@ -27,7 +27,7 @@ it('Set utcOffset -> Get utcOffset', () => { // expect(dayjs().utcOffset(0).format()).toBe(moment().utcOffset(0).format()) }) -it('valueOf, toDate should be the same as original', () => { +it('valueOf, toDate, toString, toISOString should be the same as original', () => { const d = dayjs() const du = dayjs().utcOffset(9) const mu = moment().utcOffset(9) @@ -35,6 +35,8 @@ it('valueOf, toDate should be the same as original', () => { expect(du.valueOf()).toBe(mu.valueOf()) expect(d.toDate()).toEqual(du.toDate()) expect(du.toDate()).toEqual(mu.toDate()) + expect(du.toISOString()).toEqual(mu.toISOString()) + expect(d.toString()).toEqual(d.toString()) }) it('clone', () => { From 4a22ee8b42d3290c1c64df1398016e4f331820e0 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 18:39:05 +0800 Subject: [PATCH 09/17] combine plugin --- src/plugin/setUTCOffset/index.js | 44 ------------------- src/plugin/utc/index.js | 37 ++++++++++++++-- ...TCOffset.test.js => utc-utcOffset.test.js} | 4 +- types/plugin/setUtcOffset.d.ts | 4 -- 4 files changed, 35 insertions(+), 54 deletions(-) delete mode 100644 src/plugin/setUTCOffset/index.js rename test/plugin/{setUTCOffset.test.js => utc-utcOffset.test.js} (95%) delete mode 100644 types/plugin/setUtcOffset.d.ts diff --git a/src/plugin/setUTCOffset/index.js b/src/plugin/setUTCOffset/index.js deleted file mode 100644 index e10cce6fc..000000000 --- a/src/plugin/setUTCOffset/index.js +++ /dev/null @@ -1,44 +0,0 @@ -import { MILLISECONDS_A_MINUTE, MIN } from '../../constant' - -export default (option, Dayjs) => { - const proto = Dayjs.prototype - const oldParse = proto.parse - const localOffset = (new Date()).getTimezoneOffset() - proto.parse = function (cfg) { - if (!this.$utils().u(cfg.$offset)) { - this.$offset = cfg.$offset - } - oldParse.call(this, cfg) - } - - proto.valueOf = function () { - const addedOffset = !this.$utils().u(this.$offset) ? (this.$offset || 0) + localOffset : 0 - return this.$d.valueOf() - (addedOffset * MILLISECONDS_A_MINUTE) - } - - const oldUtcOffset = proto.utcOffset - proto.utcOffset = function (input) { - const { u } = this.$utils() - if (u(input)) { - if (this.$u) { - return 0 - } - if (!u(this.$offset)) { - return this.$offset - } - return oldUtcOffset.call(this) - } - const offset = Math.abs(input) <= 16 ? input * 60 : input - const newD = this.add(offset + localOffset, MIN) - newD.$offset = offset - return newD - } - - proto.toISOString = function () { - return this.toDate().toISOString() - } - - proto.toString = function () { - return this.toDate().toUTCString() - } -} diff --git a/src/plugin/utc/index.js b/src/plugin/utc/index.js index d367c97be..47540bd80 100644 --- a/src/plugin/utc/index.js +++ b/src/plugin/utc/index.js @@ -1,4 +1,7 @@ +import { MILLISECONDS_A_MINUTE, MIN } from '../../constant' + export default (option, Dayjs, dayjs) => { + const localOffset = (new Date()).getTimezoneOffset() const proto = Dayjs.prototype dayjs.utc = function (date, format) { const cfg = { date, utc: true, format } @@ -18,6 +21,9 @@ export default (option, Dayjs, dayjs) => { if (cfg.utc) { this.$u = true } + if (!this.$utils().u(cfg.$offset)) { + this.$offset = cfg.$offset + } oldParse.call(this, cfg) } @@ -39,11 +45,21 @@ export default (option, Dayjs, dayjs) => { } const oldUtcOffset = proto.utcOffset - proto.utcOffset = function () { - if (this.$u) { - return 0 + proto.utcOffset = function (input) { + const { u } = this.$utils() + if (u(input)) { + if (this.$u) { + return 0 + } + if (!u(this.$offset)) { + return this.$offset + } + return oldUtcOffset.call(this) } - return oldUtcOffset.call(this) + const offset = Math.abs(input) <= 16 ? input * 60 : input + const newD = this.add(offset + localOffset, MIN) + newD.$offset = offset + return newD } const oldFormat = proto.format @@ -53,7 +69,20 @@ export default (option, Dayjs, dayjs) => { return oldFormat.call(this, str) } + proto.valueOf = function () { + const addedOffset = !this.$utils().u(this.$offset) ? (this.$offset || 0) + localOffset : 0 + return this.$d.valueOf() - (addedOffset * MILLISECONDS_A_MINUTE) + } + proto.isUTC = function () { return !!this.$u } + + proto.toISOString = function () { + return this.toDate().toISOString() + } + + proto.toString = function () { + return this.toDate().toUTCString() + } } diff --git a/test/plugin/setUTCOffset.test.js b/test/plugin/utc-utcOffset.test.js similarity index 95% rename from test/plugin/setUTCOffset.test.js rename to test/plugin/utc-utcOffset.test.js index 2a939c7db..f6566434a 100644 --- a/test/plugin/setUTCOffset.test.js +++ b/test/plugin/utc-utcOffset.test.js @@ -1,9 +1,9 @@ import MockDate from 'mockdate' import moment from 'moment' import dayjs from '../../src' -import setUTCOffset from '../../src/plugin/setUTCOffset' +import utc from '../../src/plugin/utc' -dayjs.extend(setUTCOffset) +dayjs.extend(utc) beforeEach(() => { MockDate.set(new Date()) diff --git a/types/plugin/setUtcOffset.d.ts b/types/plugin/setUtcOffset.d.ts deleted file mode 100644 index 30ec75e5d..000000000 --- a/types/plugin/setUtcOffset.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { PluginFunc } from 'dayjs' - -declare const plugin: PluginFunc -export = plugin From 814d2ae17a5900d98c20f9ee2ab31301c55d96f1 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 18:40:01 +0800 Subject: [PATCH 10/17] update test --- src/plugin/utc/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugin/utc/index.js b/src/plugin/utc/index.js index 47540bd80..35e5c8c22 100644 --- a/src/plugin/utc/index.js +++ b/src/plugin/utc/index.js @@ -70,7 +70,7 @@ export default (option, Dayjs, dayjs) => { } proto.valueOf = function () { - const addedOffset = !this.$utils().u(this.$offset) ? (this.$offset || 0) + localOffset : 0 + const addedOffset = !this.$utils().u(this.$offset) ? this.$offset + localOffset : 0 return this.$d.valueOf() - (addedOffset * MILLISECONDS_A_MINUTE) } From 1a8408408daa9bca88a1bf18e2511186125b0e0a Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 18:42:14 +0800 Subject: [PATCH 11/17] update test --- src/plugin/utc/index.js | 3 +++ test/plugin/utc-utcOffset.test.js | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/plugin/utc/index.js b/src/plugin/utc/index.js index 35e5c8c22..15cc254f4 100644 --- a/src/plugin/utc/index.js +++ b/src/plugin/utc/index.js @@ -59,6 +59,9 @@ export default (option, Dayjs, dayjs) => { const offset = Math.abs(input) <= 16 ? input * 60 : input const newD = this.add(offset + localOffset, MIN) newD.$offset = offset + if (input === 0) { // UTC mode + newD.$u = true + } return newD } diff --git a/test/plugin/utc-utcOffset.test.js b/test/plugin/utc-utcOffset.test.js index f6566434a..52554ac09 100644 --- a/test/plugin/utc-utcOffset.test.js +++ b/test/plugin/utc-utcOffset.test.js @@ -24,7 +24,7 @@ it('Set utcOffset -> Get utcOffset', () => { expect(dayjs().utcOffset(-60).format()).toBe(moment().utcOffset(-60).format()) expect(dayjs().utcOffset(-8).format()).toBe(moment().utcOffset(-8).format()) - // expect(dayjs().utcOffset(0).format()).toBe(moment().utcOffset(0).format()) + expect(dayjs().utcOffset(0).format()).toBe(moment().utcOffset(0).format()) }) it('valueOf, toDate, toString, toISOString should be the same as original', () => { From 07644e445f2847ab8beb3751beb3e840cbbddbbe Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 18:44:33 +0800 Subject: [PATCH 12/17] update test --- test/plugin/utc-utcOffset.test.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/plugin/utc-utcOffset.test.js b/test/plugin/utc-utcOffset.test.js index 52554ac09..c753545c6 100644 --- a/test/plugin/utc-utcOffset.test.js +++ b/test/plugin/utc-utcOffset.test.js @@ -24,7 +24,6 @@ it('Set utcOffset -> Get utcOffset', () => { expect(dayjs().utcOffset(-60).format()).toBe(moment().utcOffset(-60).format()) expect(dayjs().utcOffset(-8).format()).toBe(moment().utcOffset(-8).format()) - expect(dayjs().utcOffset(0).format()).toBe(moment().utcOffset(0).format()) }) it('valueOf, toDate, toString, toISOString should be the same as original', () => { @@ -52,3 +51,8 @@ it('immutable', () => { expect(d.utcOffset()).not.toBe(du.utcOffset()) expect(d.format()).not.toBe(du.format()) }) + +it('utcOffset(0) enable utc mode', () => { + expect(dayjs().utcOffset(0).format()).toBe(moment().utcOffset(0).format()) + expect(dayjs().utcOffset(0).isUTC()).toBeTruthy() +}) From ad9da81c34548e1ef98d335a96f52bbf3b7bc4c2 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 19:28:15 +0800 Subject: [PATCH 13/17] update test --- src/plugin/utc/index.js | 7 +++++-- test/plugin/utc-utcOffset.test.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/plugin/utc/index.js b/src/plugin/utc/index.js index 15cc254f4..4f781c601 100644 --- a/src/plugin/utc/index.js +++ b/src/plugin/utc/index.js @@ -57,10 +57,12 @@ export default (option, Dayjs, dayjs) => { return oldUtcOffset.call(this) } const offset = Math.abs(input) <= 16 ? input * 60 : input - const newD = this.add(offset + localOffset, MIN) + const newD = this.add(offset + (this.$u ? 0 : localOffset), MIN) newD.$offset = offset if (input === 0) { // UTC mode newD.$u = true + } else { + newD.$u = false } return newD } @@ -73,7 +75,8 @@ export default (option, Dayjs, dayjs) => { } proto.valueOf = function () { - const addedOffset = !this.$utils().u(this.$offset) ? this.$offset + localOffset : 0 + const addedOffset = !this.$utils().u(this.$offset) + ? this.$offset + (this.$u ? 0 : localOffset) : 0 return this.$d.valueOf() - (addedOffset * MILLISECONDS_A_MINUTE) } diff --git a/test/plugin/utc-utcOffset.test.js b/test/plugin/utc-utcOffset.test.js index c753545c6..0419d438d 100644 --- a/test/plugin/utc-utcOffset.test.js +++ b/test/plugin/utc-utcOffset.test.js @@ -56,3 +56,18 @@ it('utcOffset(0) enable utc mode', () => { expect(dayjs().utcOffset(0).format()).toBe(moment().utcOffset(0).format()) expect(dayjs().utcOffset(0).isUTC()).toBeTruthy() }) + +test('UTC mode', () => { + const d = dayjs.utc('2000-01-01T06:00:00Z') + expect(d.isUTC()).toBeTruthy() + expect(d.utcOffset(0).isUTC()).toBeTruthy() + expect(d.utcOffset(1).isUTC()).toBeFalsy() +}) + +test('change hours when changing the utc offset in UTC mode', () => { + const d = dayjs.utc('2000-01-01T06:00:00Z') + expect(d.hour()).toBe(6) + expect(d.utcOffset(0).hour()).toBe(6) + expect(d.utcOffset(-60).hour()).toBe(5) + expect(d.utcOffset(60).hour()).toBe(7) +}) From 0b51b50fd4dbdf97c8e8aac469fe03413cc570bd Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 19:29:48 +0800 Subject: [PATCH 14/17] update test --- src/plugin/utc/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugin/utc/index.js b/src/plugin/utc/index.js index 4f781c601..43dc86477 100644 --- a/src/plugin/utc/index.js +++ b/src/plugin/utc/index.js @@ -76,7 +76,7 @@ export default (option, Dayjs, dayjs) => { proto.valueOf = function () { const addedOffset = !this.$utils().u(this.$offset) - ? this.$offset + (this.$u ? 0 : localOffset) : 0 + ? this.$offset + localOffset : 0 return this.$d.valueOf() - (addedOffset * MILLISECONDS_A_MINUTE) } From 294a5df9a5dbbd85850c88e709f6f2b296afee07 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 19:31:22 +0800 Subject: [PATCH 15/17] update test --- test/plugin/utc-utcOffset.test.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/plugin/utc-utcOffset.test.js b/test/plugin/utc-utcOffset.test.js index 0419d438d..3f82b9702 100644 --- a/test/plugin/utc-utcOffset.test.js +++ b/test/plugin/utc-utcOffset.test.js @@ -65,9 +65,12 @@ test('UTC mode', () => { }) test('change hours when changing the utc offset in UTC mode', () => { - const d = dayjs.utc('2000-01-01T06:00:00Z') + const d = dayjs.utc('2000-01-01T06:31:00Z') expect(d.hour()).toBe(6) expect(d.utcOffset(0).hour()).toBe(6) expect(d.utcOffset(-60).hour()).toBe(5) expect(d.utcOffset(60).hour()).toBe(7) + expect(d.utcOffset(-30).format('HH:mm')).toBe('06:01') + expect(d.utcOffset(30).format('HH:mm')).toBe('07:01') + expect(d.utcOffset(-1380).format('HH:mm')).toBe('07:31') }) From 95bde733d3d7b2e0a7185a185ac4738783fd77d4 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 21 Aug 2019 19:35:01 +0800 Subject: [PATCH 16/17] update test --- test/plugin/utc-utcOffset.test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/plugin/utc-utcOffset.test.js b/test/plugin/utc-utcOffset.test.js index 3f82b9702..bd652be7a 100644 --- a/test/plugin/utc-utcOffset.test.js +++ b/test/plugin/utc-utcOffset.test.js @@ -40,9 +40,10 @@ it('valueOf, toDate, toString, toISOString should be the same as original', () = it('clone', () => { const du = dayjs().utcOffset(9) - const du2 = du.clone() - expect(du.valueOf()).toBe(du2.valueOf()) - expect(du.format()).toBe(du2.format()) + const duClone = du.clone() + expect(du.valueOf()).toBe(duClone.valueOf()) + expect(du.format()).toBe(duClone.format()) + expect(du.utcOffset()).toBe(duClone.utcOffset()) }) it('immutable', () => { From 56fb33155060a8c119c63750016e60dcfcee2634 Mon Sep 17 00:00:00 2001 From: iamkun Date: Tue, 27 Aug 2019 15:29:28 +0800 Subject: [PATCH 17/17] update type --- src/plugin/utc/index.js | 6 +----- types/plugin/utc.d.ts | 2 ++ 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/plugin/utc/index.js b/src/plugin/utc/index.js index 43dc86477..40c6a4959 100644 --- a/src/plugin/utc/index.js +++ b/src/plugin/utc/index.js @@ -59,11 +59,7 @@ export default (option, Dayjs, dayjs) => { const offset = Math.abs(input) <= 16 ? input * 60 : input const newD = this.add(offset + (this.$u ? 0 : localOffset), MIN) newD.$offset = offset - if (input === 0) { // UTC mode - newD.$u = true - } else { - newD.$u = false - } + newD.$u = input === 0 // UTC mode return newD } diff --git a/types/plugin/utc.d.ts b/types/plugin/utc.d.ts index 2f14efb22..d25e2e847 100644 --- a/types/plugin/utc.d.ts +++ b/types/plugin/utc.d.ts @@ -11,6 +11,8 @@ declare module 'dayjs' { local(): Dayjs isUTC(): boolean + + utcOffset(offset: number): Dayjs } export function utc(config?: ConfigType, format?: string): Dayjs