Skip to content

Commit

Permalink
Merge pull request #1015 from iamkun/dev
Browse files Browse the repository at this point in the history
D2M
  • Loading branch information
iamkun authored Aug 20, 2020
2 parents 6e5ffce + 1c49c83 commit bfbd570
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'
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{1,3})?$/
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
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ const parseDate = (cfg) => {
const d = date.match(C.REGEX_PARSE)
if (d) {
const m = d[2] - 1 || 0
const ms = (d[7] || '0').substring(0, 3)
if (utc) {
return new Date(Date.UTC(d[1], m, d[3]
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0))
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms))
}
return new Date(d[1], m, d[3]
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0)
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, ms)
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const prettyUnit = unit => `${$u.p(unit)}s`
class Duration {
constructor(input, unit, locale) {
this.$d = {}
this.$l = locale || 'en'
this.$l = locale
if (unit) {
return wrapper(input * unitToMS[prettyUnit(unit)], this)
}
Expand Down Expand Up @@ -172,7 +172,8 @@ export default (option, Dayjs, dayjs) => {
$d = dayjs
$u = dayjs().$utils()
dayjs.duration = function (input, unit) {
return wrapper(input, {}, unit)
const $l = dayjs.locale()
return wrapper(input, { $l }, unit)
}
dayjs.isDuration = isDuration
}
4 changes: 3 additions & 1 deletion src/plugin/timezone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const typeToPos = {
second: 5
}

const ms = 'ms'

export default (o, c, d) => {
const localUtcOffset = d().utcOffset()
const tzOffset = (timestamp, timezone) => {
Expand Down Expand Up @@ -55,7 +57,7 @@ export default (o, c, d) => {
proto.tz = function (timezone) {
const target = this.toDate().toLocaleString('en-US', { timeZone: timezone })
const diff = Math.round((this.toDate() - new Date(target)) / 1000 / 60)
return d(target).utcOffset(localUtcOffset - diff, true)
return d(target).utcOffset(localUtcOffset - diff, true).$set(ms, this.$ms)
}
d.tz = function (input, timezone) {
const previousOffset = tzOffset(+d(), timezone)
Expand Down
14 changes: 14 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ describe('Parse', () => {
expect(normalized.toISOString()).toEqual(expected)
})

it('parses unlimited millisecond', () => {
const date = '2019-03-25T06:41:00.999999999'
const ds = dayjs(date)
const ms = moment(date)
expect(ds.valueOf()).toEqual(ms.valueOf())
expect(ds.millisecond()).toEqual(ms.millisecond())
})

it('String Other, Null and isValid', () => {
global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn
expect(dayjs('otherString').toString().toLowerCase()).toBe(moment('otherString').toString().toLowerCase())
Expand Down Expand Up @@ -134,4 +142,10 @@ describe('REGEX_PARSE', () => {
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d.join('-')).toBe('2020/9/30-2020-9-30----')
})
it('2019-03-25T06:41:00.999999999', () => {
const date = '2019-03-25T06:41:00.999999999'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d.join('-')).toBe('2019-03-25T06:41:00.999999999-2019-03-25-06-41-00-999999999')
})
})
9 changes: 9 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ describe('Humanize', () => {
expect(dayjs.duration(1, 'minutes').locale('fr').humanize(true)).toBe('dans une minute')
expect(dayjs.duration(1, 'minutes').locale('es').humanize(true)).toBe('en un minuto')
})
it('Global Locale', () => {
dayjs.locale('en')
expect(dayjs.duration(1, 'minutes').humanize(true)).toBe('in a minute')
dayjs.locale('fr')
expect(dayjs.duration(1, 'minutes').humanize(true)).toBe('dans une minute')
dayjs.locale('es')
expect(dayjs.duration(1, 'minutes').humanize(true)).toBe('en un minuto')
dayjs.locale('en')
})
})

describe('Clone', () => {
Expand Down
8 changes: 8 additions & 0 deletions test/plugin/timezone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ describe('Parse', () => {
expect(newYork.tz('America/Los_Angeles').format()).toBe('2014-06-01T09:00:00-07:00')
expect(newYork.tz('Europe/London').format()).toBe('2014-06-01T17:00:00+01:00')
})

it('preserve milliseconds', () => {
const d = dayjs(1596735327399)
const oldMs = d.millisecond()
const dTz = d.tz('America/New_York')
const newMs = dTz.millisecond()
expect(oldMs).toEqual(newMs)
})
})

describe('Convert', () => {
Expand Down
12 changes: 10 additions & 2 deletions test/plugin/utc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('Parse UTC ', () => {
expect(dayjs().utc().format()).toBe(moment().utc().format())
})

it('Parse date string without timezome', () => {
it('Parse date string without timezone', () => {
const d = '2018-09-06'
expect(dayjs.utc(d).format()).toEqual(moment.utc(d).format())
expect(dayjs.utc(d).format()).toEqual('2018-09-06T00:00:00Z')
Expand All @@ -76,7 +76,7 @@ describe('Parse UTC ', () => {
expect(dayjs.utc(d2).format()).toEqual(moment.utc(d2).format())
})

it('Parse date string without timezome', () => {
it('Parse date string without timezone', () => {
const d = '2017-04-22 19:50:16'
expect(dayjs.utc(d).format()).toEqual('2017-04-22T19:50:16Z')
expect(dayjs.utc(d).format()).toEqual(moment.utc(d).format())
Expand All @@ -94,6 +94,14 @@ describe('Parse UTC ', () => {
expect(dayjs.utc(d).format()).toEqual('2018-09-06T19:34:28Z')
expect(dayjs.utc(d).format()).toEqual(moment.utc(d).format())
})

it('parses unlimited millisecond in utc', () => {
const date = '2019-03-25T06:41:00.999999999'
const ds = dayjs.utc(date)
const ms = moment.utc(date)
expect(ds.valueOf()).toEqual(ms.valueOf())
expect(ds.millisecond()).toEqual(ms.millisecond())
})
})

it('Clone retains the UTC mode', () => {
Expand Down

0 comments on commit bfbd570

Please sign in to comment.