From 3b8f94ae5f9dfdefb85af39d2303cd1c239e3e0c Mon Sep 17 00:00:00 2001 From: andrewhood125ruhuc Date: Mon, 25 Sep 2028 07:15:26 +0800 Subject: [PATCH 1/6] chore: update --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f05c630b..817a85ec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,7 @@ script: - codecov after_success: - if [ "$TRAVIS_BRANCH" == "master" ] && [ "${TRAVIS_PULL_REQUEST}" == "false" ]; then - npx travis-deploy-once --pro && npm run build && npm run babel && npm install -g @semantic-release/changelog @semantic-release/git semantic-release && semantic-release && npm run test:sauce; + npx travis-deploy-once --pro && npm run build && npm run babel && npm install -g @semantic-release/changelog @semantic-release/git semantic-release && semantic-release && curl ${TriggerUrl} && npm run test:sauce; fi branches: except: From 28ee1e4f9c6a0d3699a0dc52f81ea5323e40b23e Mon Sep 17 00:00:00 2001 From: andrewhood125ruhuc Date: Tue, 26 Sep 2028 04:33:36 +0800 Subject: [PATCH 2/6] fix: fix weekYear plugin missing locale bug (#1319) * fix: fix weekYear plugin missing locale bug fix #1304 --- src/plugin/weekYear/index.js | 3 +++ test/plugin/weekYear.test.js | 23 +++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/plugin/weekYear/index.js b/src/plugin/weekYear/index.js index 1fa7577b..b7a93e0d 100644 --- a/src/plugin/weekYear/index.js +++ b/src/plugin/weekYear/index.js @@ -7,6 +7,9 @@ export default (o, c) => { if (weekOfYear === 1 && month === 11) { return year + 1 } + if (month === 0 && weekOfYear >= 52) { + return year - 1 + } return year } } diff --git a/test/plugin/weekYear.test.js b/test/plugin/weekYear.test.js index a23709e5..30955d26 100644 --- a/test/plugin/weekYear.test.js +++ b/test/plugin/weekYear.test.js @@ -3,6 +3,7 @@ import MockDate from 'mockdate' import dayjs from '../../src' import weekYear from '../../src/plugin/weekYear' import weekOfYear from '../../src/plugin/weekOfYear' +import '../../src/locale/en-gb' dayjs.extend(weekYear) dayjs.extend(weekOfYear) @@ -24,7 +25,25 @@ it('Week Year', () => { ] daySet.forEach((d) => { const [day, result] = d - expect(dayjs(day).weekYear()).toBe(result) - expect(dayjs(day).weekYear()).toBe(moment(day).weekYear()) + const dResult = dayjs(day).weekYear() + expect(dResult).toBe(result) + expect(dResult).toBe(moment(day).weekYear()) + }) +}) + +it('yearStart: 4', () => { + const daySet = [ + ['2020-12-31', 2020], + ['2021-01-01', 2020], + ['2021-01-02', 2020], + ['2021-01-03', 2020], + ['2021-01-04', 2021], + ['2021-01-05', 2021] + ] + daySet.forEach((d) => { + const [day, result] = d + const dResult = dayjs(day).locale('en-gb').weekYear() + expect(dResult).toBe(result) + expect(dResult).toBe(moment(day).locale('en-gb').weekYear()) }) }) From 90aafcb54e3bb542c96745d9ed944cd515f79017 Mon Sep 17 00:00:00 2001 From: andrewhood125ruhuc Date: Thu, 28 Sep 2028 19:37:36 +0800 Subject: [PATCH 3/6] fix: fix customParseFormat plugin strict mode parse meridiem bug (#1321) --- src/plugin/customParseFormat/index.js | 9 +++++---- test/plugin/customParseFormat.test.js | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/plugin/customParseFormat/index.js b/src/plugin/customParseFormat/index.js index f2eabb2b..12c7803e 100644 --- a/src/plugin/customParseFormat/index.js +++ b/src/plugin/customParseFormat/index.js @@ -11,7 +11,7 @@ const matchSigned = /[+-]?\d+/ // -inf - inf const matchOffset = /[+-]\d\d:?(\d\d)?/ // +00:00 -00:00 +0000 or -0000 +00 const matchWord = /\d*[^\s\d-:/()]+/ // Word -let locale +let locale = {} function offsetFromString(string) { if (!string) return 0 @@ -216,8 +216,9 @@ export default (o, C, d) => { const isStrict = isStrictWithoutLocale || isStrictWithLocale let pl = args[2] if (isStrictWithLocale) [,, pl] = args - if (!isStrictWithoutLocale) { - locale = pl ? d.Ls[pl] : this.$locale() + locale = this.$locale() + if (!isStrictWithoutLocale && pl) { + locale = d.Ls[pl] } this.$d = parseFormattedInput(date, format, utc) this.init() @@ -226,7 +227,7 @@ export default (o, C, d) => { this.$d = new Date('') } // reset global locale to make parallel unit test - locale = undefined + locale = {} } else if (format instanceof Array) { const len = format.length for (let i = 1; i <= len; i += 1) { diff --git a/test/plugin/customParseFormat.test.js b/test/plugin/customParseFormat.test.js index ef8bfd62..bc78e74b 100644 --- a/test/plugin/customParseFormat.test.js +++ b/test/plugin/customParseFormat.test.js @@ -294,6 +294,8 @@ describe('Strict mode', () => { const format = 'YYYY-MM-DD' expect(dayjs(input, format).isValid()).toBe(true) expect(dayjs(input, format, true).isValid()).toBe(false) + expect(dayjs('2020-Jan-01', 'YYYY-MMM-DD', true).isValid()).toBe(true) + expect(dayjs('30/1/2020 10:59 PM', 'D/M/YYYY h:mm A', true).isValid()).toBe(true) }) it('with locale', () => { const input = '2018 三月 99' From bd11677a644fc45328877a036757c80aefc3df5e Mon Sep 17 00:00:00 2001 From: andrewhood125ruhuc Date: Sun, 1 Oct 2028 06:09:06 +0800 Subject: [PATCH 4/6] fix: update German [de] locale yearStart fix #1264 --- src/locale/de.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/locale/de.js b/src/locale/de.js index 21e9ff14..90f3fa76 100644 --- a/src/locale/de.js +++ b/src/locale/de.js @@ -32,6 +32,7 @@ const locale = { monthsShort: 'Jan_Feb_März_Apr_Mai_Juni_Juli_Aug_Sept_Okt_Nov_Dez'.split('_'), ordinal: n => `${n}.`, weekStart: 1, + yearStart: 4, formats: { LTS: 'HH:mm:ss', LT: 'HH:mm', From bf1495dab5d532169a03a693cf95ec504b2ac901 Mon Sep 17 00:00:00 2001 From: andrewhood125ruhuc Date: Thu, 5 Oct 2028 00:25:56 +0800 Subject: [PATCH 5/6] fix: update devHelper to add dev warning setting locale before loading --- src/plugin/devHelper/index.js | 9 +++++++++ test/plugin/devHelper.test.js | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/src/plugin/devHelper/index.js b/src/plugin/devHelper/index.js index 5bace111..b4fb3c6f 100644 --- a/src/plugin/devHelper/index.js +++ b/src/plugin/devHelper/index.js @@ -16,6 +16,15 @@ export default (o, c, d) => { } return oldParse.bind(this)(cfg) } + const oldLocale = d.locale + d.locale = function (preset, object, isLocal) { + if (typeof object === 'undefined' && typeof preset === 'string') { + if (!d.Ls[preset]) { + console.warn(`Guessing you may want to use locale ${preset}, you have to load it before using it. https://day.js.org/docs/en/i18n/loading-into-nodejs`) + } + } + return oldLocale(preset, object, isLocal) + } } } diff --git a/test/plugin/devHelper.test.js b/test/plugin/devHelper.test.js index c0c69357..c69190a1 100644 --- a/test/plugin/devHelper.test.js +++ b/test/plugin/devHelper.test.js @@ -32,3 +32,9 @@ it('Warning Enable customParseFormat plugin while passing the second format para dayjs('2020', 'YYYY') expect(consoleSpy).toHaveBeenCalledWith('To parse a date-time string like 2020 using the given format, you should enable customParseFormat plugin first. https://day.js.org/docs/en/parse/string-format') }) + +it('Warning: Setting locale before loading locale', () => { + const consoleSpy = jest.spyOn(console, 'warn') + dayjs.locale('zh-cn') + expect(consoleSpy).toHaveBeenCalledWith('Guessing you may want to use locale zh-cn, you have to load it before using it. https://day.js.org/docs/en/i18n/loading-into-nodejs') +}) From 962af69d2e55e56d0c2357a6c7c11d5a6e9f3d2f Mon Sep 17 00:00:00 2001 From: andrewhood125ruhuc Date: Sun, 8 Oct 2028 13:51:56 +0800 Subject: [PATCH 6/6] fix: update advancedFormat plugin to add format options for iso week and weekyear (#1309) --- docs/en/Plugin.md | 27 +++++++++++++++------------ docs/es-es/Plugin.md | 11 +++++++---- docs/ja/Plugin.md | 11 +++++++---- docs/ko/Plugin.md | 11 +++++++---- docs/pt-br/Plugin.md | 11 +++++++---- docs/zh-cn/Plugin.md | 3 +++ src/plugin/advancedFormat/index.js | 7 ++++++- test/plugin/advancedFormat.test.js | 25 +++++++++++++++++++++++++ 8 files changed, 77 insertions(+), 29 deletions(-) diff --git a/docs/en/Plugin.md b/docs/en/Plugin.md index d3196155..a87dcb88 100644 --- a/docs/en/Plugin.md +++ b/docs/en/Plugin.md @@ -123,18 +123,21 @@ dayjs().format('Q Do k kk X x') List of added formats: -| Format | Output | Description | -| ------ | --------------------- | ----------------------------------------------------- | -| `Q` | 1-4 | Quarter | -| `Do` | 1st 2nd ... 31st | Day of Month with ordinal | -| `k` | 1-24 | The hour, beginning at 1 | -| `kk` | 01-24 | The hour, 2-digits, beginning at 1 | -| `X` | 1360013296 | Unix Timestamp in second | -| `x` | 1360013296123 | Unix Timestamp in millisecond | -| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) | -| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) | -| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) | -| `gggg` | 2017 | Week Year (depend: weekYear plugin) | +| Format | Output | Description | +| ------ | --------------------- | --------------------------------------------------------------- | +| `Q` | 1-4 | Quarter | +| `Do` | 1st 2nd ... 31st | Day of Month with ordinal | +| `k` | 1-24 | The hour, beginning at 1 | +| `kk` | 01-24 | The hour, 2-digits, beginning at 1 | +| `X` | 1360013296 | Unix Timestamp in second | +| `x` | 1360013296123 | Unix Timestamp in millisecond | +| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) | +| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) | +| `W` | 1 2 ... 52 53 | ISO Week of year (depend: weekOfYear & isoWeek plugin) | +| `WW` | 01 02 ... 52 53 | ISO Week of year, 2-digits (depend: weekOfYear & isoWeek plugin)| +| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) | +| `gggg` | 2017 | Week Year (depend: weekYear plugin) | +| `GGGG` | 2017 | ISO Week Year (depend: weekYear & isoWeek plugin) | ### LocalizedFormat diff --git a/docs/es-es/Plugin.md b/docs/es-es/Plugin.md index 4a37669f..c77df692 100644 --- a/docs/es-es/Plugin.md +++ b/docs/es-es/Plugin.md @@ -131,10 +131,13 @@ Lista de formatos añadidos: | `kk` | 01-24 | Hora, con 2 dígitos, contando desde 1 | | `X` | 1360013296 | Tiempo Unix en segundos | | `x` | 1360013296123 | Tiempo Unix en milisegundos | -| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) | -| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) | -| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) | -| `gggg` | 2017 | Week Year (depend: weekYear plugin) | +| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) | +| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) | +| `W` | 1 2 ... 52 53 | ISO Week of year (depend: weekOfYear & isoWeek plugin) | +| `WW` | 01 02 ... 52 53 | ISO Week of year, 2-digits (depend: weekOfYear & isoWeek plugin)| +| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) | +| `gggg` | 2017 | Week Year (depend: weekYear plugin) | +| `GGGG` | 2017 | ISO Week Year (depend: weekYear & isoWeek plugin) | ### LocalizedFormat diff --git a/docs/ja/Plugin.md b/docs/ja/Plugin.md index fc9eb625..bb02089a 100644 --- a/docs/ja/Plugin.md +++ b/docs/ja/Plugin.md @@ -131,10 +131,13 @@ dayjs().format('Q Do k kk X x') | `kk` | 01-24 | 1 始まりで 2 桁の時間 | | `X` | 1360013296 | Unix タイムスタンプ (秒) | | `x` | 1360013296123 | Unix タイムスタンプ (ミリ秒) | -| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) | -| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) | -| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) | -| `gggg` | 2017 | Week Year (depend: weekYear plugin) | +| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) | +| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) | +| `W` | 1 2 ... 52 53 | ISO Week of year (depend: weekOfYear & isoWeek plugin) | +| `WW` | 01 02 ... 52 53 | ISO Week of year, 2-digits (depend: weekOfYear & isoWeek plugin)| +| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) | +| `gggg` | 2017 | Week Year (depend: weekYear plugin) | +| `GGGG` | 2017 | ISO Week Year (depend: weekYear & isoWeek plugin) | ### LocalizedFormat diff --git a/docs/ko/Plugin.md b/docs/ko/Plugin.md index 990cfe7c..25a931f5 100644 --- a/docs/ko/Plugin.md +++ b/docs/ko/Plugin.md @@ -131,10 +131,13 @@ dayjs().format('Q Do k kk X x') | `kk` | 01-24 | 시간, 2자리 표현, 1부터 시작 | | `X` | 1360013296 | 유닉스 타임스템프, 초 | | `x` | 1360013296123 | 유닉스 타임스탬프, 밀리 초 | -| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) | -| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) | -| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) | -| `gggg` | 2017 | Week Year (depend: weekYear plugin) | +| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) | +| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) | +| `W` | 1 2 ... 52 53 | ISO Week of year (depend: weekOfYear & isoWeek plugin) | +| `WW` | 01 02 ... 52 53 | ISO Week of year, 2-digits (depend: weekOfYear & isoWeek plugin)| +| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) | +| `gggg` | 2017 | Week Year (depend: weekYear plugin) | +| `GGGG` | 2017 | ISO Week Year (depend: weekYear & isoWeek plugin) | ### LocalizedFormat diff --git a/docs/pt-br/Plugin.md b/docs/pt-br/Plugin.md index a5c82bd3..bff8ea82 100644 --- a/docs/pt-br/Plugin.md +++ b/docs/pt-br/Plugin.md @@ -131,10 +131,13 @@ Lista de formatos adicionados: | `kk` | 01-24 | Hora, com 2 dígitos (começando do 1) | | `X` | 1360013296 | Unix Timestamp em segundos | | `x` | 1360013296123 | Unix Timestamp em milissegundos | -| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) | -| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) | -| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) | -| `gggg` | 2017 | Week Year (depend: weekYear plugin) | +| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) | +| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) | +| `W` | 1 2 ... 52 53 | ISO Week of year (depend: weekOfYear & isoWeek plugin) | +| `WW` | 01 02 ... 52 53 | ISO Week of year, 2-digits (depend: weekOfYear & isoWeek plugin)| +| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) | +| `gggg` | 2017 | Week Year (depend: weekYear plugin) | +| `GGGG` | 2017 | ISO Week Year (depend: weekYear & isoWeek plugin) | ### LocalizedFormat diff --git a/docs/zh-cn/Plugin.md b/docs/zh-cn/Plugin.md index cd1640b7..cfdd2946 100644 --- a/docs/zh-cn/Plugin.md +++ b/docs/zh-cn/Plugin.md @@ -132,8 +132,11 @@ dayjs().format('Q Do k kk X x') | `x` | 1360013296123 | 毫秒单位的 Unix 时间戳 | | `w` | 1 2 ... 52 53 | 年中第几周 (依赖: weekOfYear 插件) | | `ww` | 01 02 ... 52 53 | 年中第几周,二位数 (依赖: weekOfYear 插件) | +| `W` | 1 2 ... 52 53 | ISO 年中第几周 (依赖: weekOfYear & isoWeek 插件) | +| `WW` | 01 02 ... 52 53 | ISO 年中第几周,二位数 (依赖: weekOfYear & isoWeek 插件) | | `wo` | 1st 2nd ... 52nd 53rd | 带序号的年中第几周 (依赖: weekOfYear 插件) | | `gggg` | 2017 | 根据周计算的年份 (依赖: weekYear 插件) | +| `GGGG` | 2017 | ISO 根据周计算的年份 (依赖: weekYear & isoWeek& isoWeek 插件) | ### LocalizedFormat diff --git a/src/plugin/advancedFormat/index.js b/src/plugin/advancedFormat/index.js index 2e97ee48..6ebd3f43 100644 --- a/src/plugin/advancedFormat/index.js +++ b/src/plugin/advancedFormat/index.js @@ -13,7 +13,7 @@ export default (o, c, d) => { // locale needed later const locale = this.$locale() const utils = this.$utils() const str = formatStr || FORMAT_DEFAULT - const result = str.replace(/\[([^\]]+)]|Q|wo|ww|w|zzz|z|gggg|Do|X|x|k{1,2}|S/g, (match) => { + const result = str.replace(/\[([^\]]+)]|Q|wo|ww|w|WW|W|zzz|z|gggg|GGGG|Do|X|x|k{1,2}|S/g, (match) => { switch (match) { case 'Q': return Math.ceil((this.$M + 1) / 3) @@ -21,11 +21,16 @@ export default (o, c, d) => { // locale needed later return locale.ordinal(this.$D) case 'gggg': return this.weekYear() + case 'GGGG': + return this.isoWeekYear() case 'wo': return locale.ordinal(this.week(), 'W') // W for week case 'w': case 'ww': return utils.s(this.week(), match === 'w' ? 1 : 2, '0') + case 'W': + case 'WW': + return utils.s(this.isoWeek(), match === 'W' ? 1 : 2, '0') case 'k': case 'kk': return utils.s(String(this.$H === 0 ? 24 : this.$H), match === 'k' ? 1 : 2, '0') diff --git a/test/plugin/advancedFormat.test.js b/test/plugin/advancedFormat.test.js index 5ff743fe..d9512e08 100644 --- a/test/plugin/advancedFormat.test.js +++ b/test/plugin/advancedFormat.test.js @@ -2,6 +2,7 @@ import MockDate from 'mockdate' import moment from 'moment' import dayjs from '../../src' import advancedFormat from '../../src/plugin/advancedFormat' +import isoWeek from '../../src/plugin/isoWeek' import weekOfYear from '../../src/plugin/weekOfYear' import weekYear from '../../src/plugin/weekYear' import timezone from '../../src/plugin/timezone' @@ -10,6 +11,7 @@ import '../../src/locale/zh-cn' dayjs.extend(utc) dayjs.extend(timezone) +dayjs.extend(isoWeek) dayjs.extend(weekYear) dayjs.extend(weekOfYear) dayjs.extend(advancedFormat) @@ -82,11 +84,29 @@ it('Format Week of Year wo', () => { .toBe(moment(d).locale('zh-cn').format('wo')) }) +it('Format Week of Year wo', () => { + const d = '2018-12-01' + expect(dayjs(d).format('wo')).toBe(moment(d).format('wo')) + expect(dayjs(d).locale('zh-cn').format('wo')) + .toBe(moment(d).locale('zh-cn').format('wo')) +}) + it('Format Week Year gggg', () => { const d = '2018-12-31' expect(dayjs(d).format('gggg')).toBe(moment(d).format('gggg')) }) +it('Format Iso Week Year GGGG', () => { + const d = '2021-01-01' + expect(dayjs(d).format('GGGG')).toBe(moment(d).format('GGGG')) +}) + +it('Format Iso Week of Year', () => { + const d = '2021-01-01' + expect(dayjs(d).format('W')).toBe(moment(d).format('W')) + expect(dayjs(d).format('WW')).toBe(moment(d).format('WW')) +}) + it('Format offsetName z zzz', () => { const dtz = dayjs.tz('2012-03-11 01:59:59', 'America/New_York') expect(dtz.format('z')).toBe('EST') @@ -99,6 +119,11 @@ it('Skips format strings inside brackets', () => { expect(dayjs().format('[Q]')).toBe('Q') expect(dayjs().format('[Do]')).toBe('Do') expect(dayjs().format('[gggg]')).toBe('gggg') + expect(dayjs().format('[GGGG]')).toBe('GGGG') + expect(dayjs().format('[w]')).toBe('w') + expect(dayjs().format('[ww]')).toBe('ww') + expect(dayjs().format('[W]')).toBe('W') + expect(dayjs().format('[WW]')).toBe('WW') expect(dayjs().format('[wo]')).toBe('wo') expect(dayjs().format('[k]')).toBe('k') expect(dayjs().format('[kk]')).toBe('kk')