Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365
const MILLISECONDS_A_MONTH = MILLISECONDS_A_YEAR / 12

const DURATION_REGEX_PARSE = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/
const TIME_REGEX_PARSE = /^(-|\+)?(?:(\d*)[. ])?(\d+):(\d+)(?::(\d+)(\.\d*)?)?$/
const DURATION_REGEX_FORMAT = /\[([^\]]+)]|YYYY|YY|Y|M{1,2}|D{1,2}|H{1,2}|m{1,2}|s{1,2}|SSS/g

const unitToMS = {
Expand Down Expand Up @@ -97,6 +98,17 @@ class Duration {
this.calMilliseconds()
return this
}
const t = input.match(TIME_REGEX_PARSE)
if (t) {
const sign = t[1] === '-' ? -1 : 1
this.$d.days = sign * (t[2] ? Number(t[2]) : 0)
this.$d.hours = sign * Number(t[3])
this.$d.minutes = sign * Number(t[4])
this.$d.seconds = sign * (t[5] ? Number(t[5]) : 0)
this.$d.milliseconds = sign * (t[6] ? Math.round(Number(`0${t[6]}`) * 1000) : 0)
this.calMilliseconds()
return this
}
}
return this
}
Expand Down
42 changes: 42 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,48 @@ describe('Creating', () => {
})
})

describe('Parse time string', () => {
it('HH:mm:ss', () => {
const d = dayjs.duration('02:04:33')
expect(d.hours()).toBe(2)
expect(d.minutes()).toBe(4)
expect(d.seconds()).toBe(33)
expect(d.asSeconds()).toBe(7473)
expect(d.toISOString()).toBe('PT2H4M33S')
})
it('HH:mm', () => {
const d = dayjs.duration('23:59')
expect(d.hours()).toBe(23)
expect(d.minutes()).toBe(59)
expect(d.seconds()).toBe(0)
expect(d.toISOString()).toBe('PT23H59M')
})
it('days.HH:mm:ss', () => {
const d = dayjs.duration('7.23:59:59')
expect(d.days()).toBe(7)
expect(d.hours()).toBe(23)
expect(d.minutes()).toBe(59)
expect(d.seconds()).toBe(59)
})
it('HH:mm:ss.SSS (fractional seconds)', () => {
const d = dayjs.duration('23:59:59.999')
expect(d.hours()).toBe(23)
expect(d.minutes()).toBe(59)
expect(d.seconds()).toBe(59)
expect(d.milliseconds()).toBe(999)
})
it('negative time string', () => {
const d = dayjs.duration('-23:59:59')
expect(d.hours()).toBe(-23)
expect(d.minutes()).toBe(-59)
expect(d.seconds()).toBe(-59)
expect(d.toISOString()).toBe('-PT23H59M59S')
})
it('Invalid string still returns P0D', () => {
expect(dayjs.duration('Invalid').toISOString()).toBe('P0D')
})
})

describe('Parse ISO string', () => {
it('Full ISO string', () => {
expect(dayjs.duration('P7Y6M4DT3H2M1S').toISOString()).toBe('P7Y6M4DT3H2M1S')
Expand Down
Loading