From 6545739d8a20ed71fb894e865ce2deb8a721ca8e Mon Sep 17 00:00:00 2001 From: EdgarsN Date: Mon, 9 Nov 2020 05:12:47 +0200 Subject: [PATCH 1/3] fix: Update lv (Latvian) locale relative time (#1192) --- src/locale/lv.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/locale/lv.js b/src/locale/lv.js index b06bd65a9..7f8009616 100644 --- a/src/locale/lv.js +++ b/src/locale/lv.js @@ -17,6 +17,21 @@ const locale = { LL: 'YYYY. [gada] D. MMMM', LLL: 'YYYY. [gada] D. MMMM, HH:mm', LLLL: 'YYYY. [gada] D. MMMM, dddd, HH:mm' + }, + relativeTime: { + future: 'pēc %s', + past: 'pirms %s', + s: 'dažām sekundēm', + m: 'minūtes', + mm: '%d minūtēm', + h: 'stundas', + hh: '%d stundām', + d: 'dienas', + dd: '%d dienām', + M: 'mēneša', + MM: '%d mēnešiem', + y: 'gada', + yy: '%d gadiem' } } From a694da6befe008a7ea24981c98954c8789bea75c Mon Sep 17 00:00:00 2001 From: verzac Date: Wed, 11 Nov 2020 00:05:11 +0700 Subject: [PATCH 2/3] fix: add duration.format() to duration plugin --- src/constant.js | 2 +- src/plugin/duration/index.js | 23 ++++++++++++++++++++++- test/plugin/duration.test.js | 24 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/constant.js b/src/constant.js index a4f034fa2..22c1319e0 100644 --- a/src/constant.js +++ b/src/constant.js @@ -27,4 +27,4 @@ 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{2,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_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 diff --git a/src/plugin/duration/index.js b/src/plugin/duration/index.js index 50f660484..02f12bcb4 100644 --- a/src/plugin/duration/index.js +++ b/src/plugin/duration/index.js @@ -1,4 +1,4 @@ -import { MILLISECONDS_A_DAY, MILLISECONDS_A_HOUR, MILLISECONDS_A_MINUTE, MILLISECONDS_A_SECOND, MILLISECONDS_A_WEEK } from '../../constant' +import { MILLISECONDS_A_DAY, MILLISECONDS_A_HOUR, MILLISECONDS_A_MINUTE, MILLISECONDS_A_SECOND, MILLISECONDS_A_WEEK, REGEX_FORMAT } from '../../constant' const MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365 const MILLISECONDS_A_MONTH = MILLISECONDS_A_DAY * 30 @@ -105,6 +105,27 @@ class Duration { return this.toISOString() } + format(formatStr) { + const str = formatStr || 'YYYY-MM-DDTHH:mm:ss' + const matches = { + Y: this.$d.years, + YY: $u.s(this.$d.years, 2, '0'), + YYYY: $u.s(this.$d.years, 4, '0'), + M: this.$d.months, + MM: $u.s(this.$d.months, 2, '0'), + D: this.$d.days, + DD: $u.s(this.$d.days, 2, '0'), + H: this.$d.hours, + HH: $u.s(this.$d.hours, 2, '0'), + m: this.$d.minutes, + mm: $u.s(this.$d.minutes, 2, '0'), + s: this.$d.seconds, + ss: $u.s(this.$d.seconds, 2, '0'), + SSS: $u.s(this.$d.milliseconds, 3, '0') + } + return str.replace(REGEX_FORMAT, (match, $1) => $1 || String(matches[match])) + } + as(unit) { return this.$ms / (unitToMS[prettyUnit(unit)]) } diff --git a/test/plugin/duration.test.js b/test/plugin/duration.test.js index 5e5447658..15e60b925 100644 --- a/test/plugin/duration.test.js +++ b/test/plugin/duration.test.js @@ -208,3 +208,27 @@ describe('prettyUnit', () => { m: 12 }).toISOString()).toBe('P12MT12M') }) + +describe('Format', () => { + test('no formatStr', () => { + const d = dayjs.duration(15, 'seconds') + .add(13, 'hours') + .add(35, 'minutes') + .add(16, 'days') + .add(10, 'months') + .add(22, 'years') + expect(d.format()).toBe('0022-10-16T13:35:15') + }) + + test('with formatStr for all tokens', () => { + const d = dayjs.duration(1, 'seconds') + .add(8, 'hours') + .add(5, 'minutes') + .add(6, 'days') + .add(9, 'months') + .add(2, 'years') + .add(10, 'milliseconds') + expect(d.format('Y/YY.YYYYTESTM:MM:D:DD:H:HH:m:mm:s:ss:SSS')) + .toBe('2/02.0002TEST9:09:6:06:8:08:5:05:1:01:010') + }) +}) From 7ba0d19b1d7cedbcdde5c3ae7135f52f2d5bcd17 Mon Sep 17 00:00:00 2001 From: verzac Date: Thu, 12 Nov 2020 21:33:50 +0700 Subject: [PATCH 3/3] feat(duration): add types for duration.format() --- types/plugin/duration.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/plugin/duration.d.ts b/types/plugin/duration.d.ts index c09dc9f09..3695eeaaf 100644 --- a/types/plugin/duration.d.ts +++ b/types/plugin/duration.d.ts @@ -51,6 +51,8 @@ declare namespace plugin { toISOString(): string + format(formatStr?: string): string + locale(locale: string): Duration } }