From 03f043619651f6a67752fac9aa5cb5bf6c2740d1 Mon Sep 17 00:00:00 2001 From: Ognjen Jevremovic Date: Mon, 1 Mar 2021 13:54:16 +0100 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20Support=20utcOffset?= =?UTF-8?q?=20as=20a=20string=20(HH:mm=20|=20HHmm)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Provide support string support to utcOffset method. Valid string values formats for utcOffset method are +HH:mm, -HH:mm, +HHmm and -HHmm. ✅ Closes: #1085 --- src/constant.js | 2 ++ src/plugin/utc/index.js | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/constant.js b/src/constant.js index 6be602e2c..24ffa2320 100644 --- a/src/constant.js +++ b/src/constant.js @@ -28,3 +28,5 @@ export const INVALID_DATE_STRING = 'Invalid Date' // regex export const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/ export const REGEX_FORMAT = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g +export const REGEX_VALID_OFFSET_FORMAT = /[+-]\d\d(?::?\d\d)?/g +export const REGEX_OFFSET_HOURS_MINUTES_FORMAT = /([+-]|\d\d)/g diff --git a/src/plugin/utc/index.js b/src/plugin/utc/index.js index b5b5f4e64..c01048a98 100644 --- a/src/plugin/utc/index.js +++ b/src/plugin/utc/index.js @@ -1,4 +1,23 @@ -import { MILLISECONDS_A_MINUTE, MIN } from '../../constant' +import { MILLISECONDS_A_MINUTE, MIN, REGEX_VALID_OFFSET_FORMAT, REGEX_OFFSET_HOURS_MINUTES_FORMAT } from '../../constant' + + +function offsetFromString(value = '') { + const offset = value.match(REGEX_VALID_OFFSET_FORMAT) + + if (!offset) { + return null + } + + const [indicator, hoursOffset, minutesOffset] = `${offset[0]}`.match(REGEX_OFFSET_HOURS_MINUTES_FORMAT) || ['-', 0, 0] + const totalOffsetInMinutes = (+hoursOffset * 60) + (+minutesOffset) + + if (totalOffsetInMinutes === 0) { + return 0 + } + + return indicator === '+' ? totalOffsetInMinutes : -totalOffsetInMinutes +} + export default (option, Dayjs, dayjs) => { const proto = Dayjs.prototype @@ -59,6 +78,12 @@ export default (option, Dayjs, dayjs) => { } return oldUtcOffset.call(this) } + if (typeof input === 'string') { + input = offsetFromString(input) + if (input === null) { + return this + } + } const offset = Math.abs(input) <= 16 ? input * 60 : input let ins = this if (keepLocalTime) { From 62d38f3dcac8557d10107cecfb8c29a79e50988b Mon Sep 17 00:00:00 2001 From: Ognjen Jevremovic Date: Mon, 1 Mar 2021 14:21:33 +0100 Subject: [PATCH 2/3] =?UTF-8?q?test:=20=F0=9F=92=8D=20Test=20utcOffset=20s?= =?UTF-8?q?tring=20arguments=20usage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extend the current suite of utc unit tests by providing unit tests for the utcOffset method with string arguments. ✅ Closes: #1085 --- test/plugin/utc.test.js | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/plugin/utc.test.js b/test/plugin/utc.test.js index 888412a8c..2dd5c0c35 100644 --- a/test/plugin/utc.test.js +++ b/test/plugin/utc.test.js @@ -226,6 +226,71 @@ describe('UTC Offset', () => { expect(dayjs().utcOffset()).toBe(moment().utcOffset()) expect(dayjs().utc().utcOffset()).toBe(moment().utc().utcOffset()) }) + + it('get utc offset with a number value', () => { + const time = '2021-02-28 19:40:10' + const hoursOffset = -8 + const daysJS = dayjs(time).utc().utcOffset(hoursOffset * 60, true) + const momentJS = moment(time).utc(true).utcOffset(hoursOffset, true) + + expect(daysJS.toISOString()).toEqual(momentJS.toISOString()) + expect(daysJS.utcOffset()).toEqual(hoursOffset * 60) + expect(daysJS.utcOffset()).toEqual(momentJS.utcOffset()) + }) + + it('get utc offset with a negative valid string value, format: HH:mm', () => { + const time = '2021-02-28 19:40:10' + const hoursOffset = -8 + const daysJS = dayjs(time).utc().utcOffset(`-0${Math.abs(hoursOffset)}:00`, true) + const momentJS = moment(time).utc(true).utcOffset(`-0${Math.abs(hoursOffset)}:00`, true) + + expect(daysJS.toISOString()).toEqual(momentJS.toISOString()) + expect(daysJS.utcOffset()).toEqual(hoursOffset * 60) + expect(daysJS.utcOffset()).toEqual(momentJS.utcOffset()) + }) + + it('get utc offset with a positive valid string value, format: HH:mm', () => { + const time = '2021-02-28 19:40:10' + const hoursOffset = 8 + const daysJS = dayjs(time).utc().utcOffset(`+0${hoursOffset}:00`, true) + const momentJS = moment(time).utc(true).utcOffset(`+0${hoursOffset}:00`, true) + + expect(daysJS.toISOString()).toEqual(momentJS.toISOString()) + expect(daysJS.utcOffset()).toEqual(hoursOffset * 60) + expect(daysJS.utcOffset()).toEqual(momentJS.utcOffset()) + }) + + it('get utc offset with a negative valid string value, format: HHmm', () => { + const time = '2021-02-28 19:40:10' + const hoursOffset = -8 + const daysJS = dayjs(time).utc().utcOffset(`-0${Math.abs(hoursOffset)}00`, true) + const momentJS = moment(time).utc(true).utcOffset(`-0${Math.abs(hoursOffset)}00`, true) + + expect(daysJS.toISOString()).toEqual(momentJS.toISOString()) + expect(daysJS.utcOffset()).toEqual(hoursOffset * 60) + expect(daysJS.utcOffset()).toEqual(momentJS.utcOffset()) + }) + + it('get utc offset with a positive valid string value, format: HHmm', () => { + const time = '2021-02-28 19:40:10' + const hoursOffset = 8 + const daysJS = dayjs(time).utc().utcOffset(`+0${hoursOffset}00`, true) + const momentJS = moment(time).utc(true).utcOffset(`+0${hoursOffset}00`, true) + + expect(daysJS.toISOString()).toEqual(momentJS.toISOString()) + expect(daysJS.utcOffset()).toEqual(hoursOffset * 60) + expect(daysJS.utcOffset()).toEqual(momentJS.utcOffset()) + }) + + it('get utc offset with an invalid string value, value: random', () => { + const time = '2021-02-28 19:40:10' + const daysJS = dayjs(time, { utc: true }).utc(true).utcOffset('random') + const momentJS = moment(time).utc(true).utcOffset('random') + + expect(daysJS.toISOString()).toEqual(momentJS.toISOString()) + expect(daysJS.utcOffset()).toEqual(0) + expect(daysJS.utcOffset()).toEqual(momentJS.utcOffset()) + }) }) describe('Diff', () => { From c99101a87f1c2b3d13a4ef4a6bbd73f692d79bbf Mon Sep 17 00:00:00 2001 From: Ognjen Jevremovic Date: Tue, 2 Mar 2021 13:19:12 +0100 Subject: [PATCH 3/3] =?UTF-8?q?chore:=20=F0=9F=A4=96=20Move=20utc=20relate?= =?UTF-8?q?d=20constants?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit UTC plugin constants should be self-contained from within the plugin package. ✅ Closes: #1395 --- src/constant.js | 2 -- src/plugin/utc/index.js | 4 +++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/constant.js b/src/constant.js index 24ffa2320..6be602e2c 100644 --- a/src/constant.js +++ b/src/constant.js @@ -28,5 +28,3 @@ export const INVALID_DATE_STRING = 'Invalid Date' // regex export const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/ export const REGEX_FORMAT = /\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g -export const REGEX_VALID_OFFSET_FORMAT = /[+-]\d\d(?::?\d\d)?/g -export const REGEX_OFFSET_HOURS_MINUTES_FORMAT = /([+-]|\d\d)/g diff --git a/src/plugin/utc/index.js b/src/plugin/utc/index.js index c01048a98..8f840590c 100644 --- a/src/plugin/utc/index.js +++ b/src/plugin/utc/index.js @@ -1,5 +1,7 @@ -import { MILLISECONDS_A_MINUTE, MIN, REGEX_VALID_OFFSET_FORMAT, REGEX_OFFSET_HOURS_MINUTES_FORMAT } from '../../constant' +import { MILLISECONDS_A_MINUTE, MIN } from '../../constant' +export const REGEX_VALID_OFFSET_FORMAT = /[+-]\d\d(?::?\d\d)?/g +export const REGEX_OFFSET_HOURS_MINUTES_FORMAT = /([+-]|\d\d)/g function offsetFromString(value = '') { const offset = value.match(REGEX_VALID_OFFSET_FORMAT)