Skip to content

Commit

Permalink
Merge pull request #877 from iamkun/dev
Browse files Browse the repository at this point in the history
D2M
  • Loading branch information
iamkun committed Apr 21, 2020
2 parents 3188e15 + 4141084 commit 9dbe5b0
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class Dayjs {
const argumentStart = [0, 0, 0, 0]
const argumentEnd = [23, 59, 59, 999]
return Utils.w(this.toDate()[method].apply( // eslint-disable-line prefer-spread
this.toDate(),
this.toDate('s'),
(isStartOf ? argumentStart : argumentEnd).slice(slice)
), this)
}
Expand Down
15 changes: 7 additions & 8 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,20 @@ const parseFormattedInput = (input, format, utc) => {
const {
year, month, day, hours, minutes, seconds, milliseconds, zone
} = parser(input)
if (zone) {
return new Date(Date.UTC(
year, month - 1, day,
hours || 0,
minutes || 0, seconds || 0, milliseconds || 0
) + (zone.offset * 60 * 1000))
}
const now = new Date()
const d = day || ((!year && !month) ? now.getDate() : 1)
const y = year || now.getFullYear()
const M = month > 0 ? month - 1 : now.getMonth()
let M = 0
if (!(year && !month)) {
M = month > 0 ? month - 1 : now.getMonth()
}
const h = hours || 0
const m = minutes || 0
const s = seconds || 0
const ms = milliseconds || 0
if (zone) {
return new Date(Date.UTC(y, M, d, h, m, s, ms + (zone.offset * 60 * 1000)))
}
if (utc) {
return new Date(Date.UTC(y, M, d, h, m, s, ms))
}
Expand Down
8 changes: 6 additions & 2 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ class Duration {
$ms %= MILLISECONDS_A_HOUR
this.$d.minutes = Math.floor($ms / MILLISECONDS_A_MINUTE)
$ms %= MILLISECONDS_A_MINUTE
this.$d.seconds = $ms / MILLISECONDS_A_SECOND
this.$d.seconds = Math.floor($ms / MILLISECONDS_A_SECOND)
$ms %= MILLISECONDS_A_SECOND
this.$d.milliseconds = $ms
}

toISOString() {
Expand Down Expand Up @@ -111,8 +113,10 @@ class Duration {
const pUnit = prettyUnit(unit)
if (pUnit === 'milliseconds') {
base %= 1000
} else {
} else if (pUnit === 'weeks') {
base = Math.floor(base / unitToMS[pUnit])
} else {
base = this.$d[pUnit]
}
return base
}
Expand Down
8 changes: 8 additions & 0 deletions src/plugin/utc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,12 @@ export default (option, Dayjs, dayjs) => {
proto.toString = function () {
return this.toDate().toUTCString()
}

const oldToDate = proto.toDate
proto.toDate = function (type) {
if (type === 's' && this.$offset) {
return dayjs(this.format('YYYY-MM-DD HH:mm:ss:SSS')).toDate()
}
return oldToDate.call(this)
}
}
19 changes: 19 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ it('parse HH:mm:ss but only one digit', () => {
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})

describe('parse YYYY / YYYY-MM only', () => {
it('YYYY', () => {
const input = '2001 +08:00'
const format = 'YYYY Z'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
const input2 = '2001'
const format2 = 'YYYY'
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
})
it('YYYY-MM', () => {
const input = '2001-01 +08:00'
const format = 'YYYY-MM Z'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
const input2 = '2001-01'
const format2 = 'YYYY-MM'
expect(dayjs(input2, format2).valueOf()).toBe(moment(input2, format2).valueOf())
})
})

it('parse hh:mm:ss but only one digit', () => {
const input = '0:0:1'
const format = 'hh:mm:ss'
Expand Down
2 changes: 2 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,15 @@ describe('Seconds', () => {
expect(dayjs.duration(500).seconds()).toBe(0)
expect(dayjs.duration(1500).seconds()).toBe(1)
expect(dayjs.duration(15000).seconds()).toBe(15)
expect(dayjs.duration(61000).seconds()).toBe(1) // 1 minute 1 second
expect(dayjs.duration(500).asSeconds()).toBe(0.5)
expect(dayjs.duration(1500).asSeconds()).toBe(1.5)
expect(dayjs.duration(15000).asSeconds()).toBe(15)
})

describe('Minutes', () => {
expect(dayjs.duration(100000).minutes()).toBe(1)
expect(dayjs.duration(61000).minutes()).toBe(1) // 1 minute 1 second
expect(dayjs.duration(100000).asMinutes().toFixed(2)).toBe('1.67')
})

Expand Down
7 changes: 7 additions & 0 deletions test/plugin/utc-utcOffset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,11 @@ test('utc startOf', () => {
.valueOf())
.toBe(dayjs(d).utc().utcOffset(480).endOf('day')
.valueOf())
const d2 = '2017-07-20T11:00:00+00:00'
const d2d = dayjs(d2).utcOffset(-12).startOf('day').valueOf()
const d2m = moment(d2).utcOffset(-12).startOf('day').valueOf()
expect(d2d)
.toBe(d2m)
expect(d2d)
.toBe(1500465600000)
})
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ declare namespace dayjs {

export function extend<T = unknown>(plugin: PluginFunc<T>, option?: T): Dayjs

export function locale(preset: string | ILocale, object?: Partial<ILocale>, isLocal?: boolean): string
export function locale(preset?: string | ILocale, object?: Partial<ILocale>, isLocal?: boolean): string

export function isDayjs(d: any): d is Dayjs

Expand Down

0 comments on commit 9dbe5b0

Please sign in to comment.