Skip to content

Commit

Permalink
Merge pull request #972 from iamkun/dev
Browse files Browse the repository at this point in the history
D2M
  • Loading branch information
iamkun authored Jul 29, 2020
2 parents e3750ec + 70c1239 commit aa88c69
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 147 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{1,3})?$/
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
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ const parseDate = (cfg) => {
if (typeof date === 'string' && !/Z$/i.test(date)) {
const d = date.match(C.REGEX_PARSE)
if (d) {
const m = d[2] - 1 || 0
if (utc) {
return new Date(Date.UTC(d[1], d[2] - 1, d[3]
return new Date(Date.UTC(d[1], m, d[3]
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0))
}
return new Date(d[1], d[2] - 1, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0)
return new Date(d[1], m, d[3]
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0)
}
}

Expand Down
9 changes: 7 additions & 2 deletions test/plugin/utc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,17 @@ describe('Parse UTC ', () => {

it('Parse date string without timezome', () => {
const d = '2018-09-06'
const d2 = '2018-09'
expect(dayjs.utc(d).format()).toEqual(moment.utc(d).format())
expect(dayjs.utc(d).format()).toEqual('2018-09-06T00:00:00Z')
expect(dayjs(d).utc().format()).toEqual(moment(d).utc().format())
const d2 = '2018-09'
expect(dayjs.utc(d2).format()).toEqual(moment.utc(d2).format())
expect(dayjs.utc(d2).format()).toEqual('2018-09-01T00:00:00Z')
expect(dayjs(d).utc().format()).toEqual(moment(d).utc().format())
expect(dayjs(d2).utc().format()).toEqual(moment(d2).utc().format())
const d3 = '2018'
expect(dayjs.utc(d3).format()).toEqual(moment.utc(d3).format())
expect(dayjs.utc(d3).format()).toEqual('2018-01-01T00:00:00Z')
expect(dayjs(d3).utc().format()).toEqual(moment(d3).utc().format())
})

it('creating with utc with timezone', () => {
Expand Down
10 changes: 5 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ declare function dayjs (date?: dayjs.ConfigType): dayjs.Dayjs

declare function dayjs (date?: dayjs.ConfigType, format?: dayjs.OptionType, strict?: boolean): dayjs.Dayjs

declare function dayjs (date?: dayjs.ConfigType, format?: dayjs.OptionType, locale?: LocalePresetType, strict?: boolean): dayjs.Dayjs
declare function dayjs (date?: dayjs.ConfigType, format?: dayjs.OptionType, locale?: string, strict?: boolean): dayjs.Dayjs

declare namespace dayjs {
export type ConfigType = string | number | Date | Dayjs

export type OptionType = { locale?: LocalePresetType, format?: string, utc?: boolean } | string | string[]
export type OptionType = { locale?: string, format?: string, utc?: boolean } | string | string[]

type UnitTypeShort = 'd' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms'
export type UnitType = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year' | 'date' | UnitTypeShort;
Expand Down Expand Up @@ -96,16 +96,16 @@ declare namespace dayjs {

isAfter(date: ConfigType, unit?: OpUnitType): boolean

locale(): LocalePresetType
locale(): string

locale(preset: LocalePresetType | ILocale, object?: Partial<ILocale>): Dayjs
locale(preset: string | ILocale, object?: Partial<ILocale>): Dayjs
}

export type PluginFunc<T = unknown> = (option: T, c: typeof Dayjs, d: typeof dayjs) => void

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

export function locale(preset?: LocalePresetType | 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
137 changes: 0 additions & 137 deletions types/locale/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,140 +31,3 @@ declare interface ILocale {
yy: string
}>
}

declare type LocalePresetType =
| 'af'
| 'ar-dz'
| 'ar-kw'
| 'ar-ly'
| 'ar-ma'
| 'ar-sa'
| 'ar-tn'
| 'ar'
| 'az'
| 'be'
| 'bg'
| 'bi'
| 'bm'
| 'bn'
| 'bo'
| 'br'
| 'bs'
| 'ca'
| 'cs'
| 'cv'
| 'cy'
| 'da'
| 'de-at'
| 'de-ch'
| 'de'
| 'dv'
| 'el'
| 'en-SG'
| 'en-au'
| 'en-ca'
| 'en-gb'
| 'en-ie'
| 'en-il'
| 'en-in'
| 'en-nz'
| 'en-tt'
| 'en'
| 'eo'
| 'es-do'
| 'es-us'
| 'es'
| 'et'
| 'eu'
| 'fa'
| 'fi'
| 'fo'
| 'fr-ca'
| 'fr-ch'
| 'fr'
| 'fy'
| 'ga'
| 'gd'
| 'gl'
| 'gom-latn'
| 'gu'
| 'he'
| 'hi'
| 'hr'
| 'hu'
| 'hy-am'
| 'id'
| 'is'
| 'it-ch'
| 'it'
| 'ja'
| 'jv'
| 'ka'
| 'kk'
| 'km'
| 'kn'
| 'ko'
| 'ku'
| 'ky'
| 'lb'
| 'lo'
| 'lt'
| 'lv'
| 'me'
| 'mi'
| 'mk'
| 'ml'
| 'mn'
| 'mr'
| 'ms-my'
| 'ms'
| 'mt'
| 'my'
| 'nb'
| 'ne'
| 'nl-be'
| 'nl'
| 'nn'
| 'oc-lnc'
| 'pa-in'
| 'pl'
| 'pt-br'
| 'pt'
| 'ro'
| 'ru'
| 'rw'
| 'sd'
| 'se'
| 'si'
| 'sk'
| 'sl'
| 'sq'
| 'sr-cyrl'
| 'sr'
| 'ss'
| 'sv'
| 'sw'
| 'ta'
| 'te'
| 'tet'
| 'tg'
| 'th'
| 'tk'
| 'tl-ph'
| 'tlh'
| 'tr'
| 'tzl'
| 'tzm-latn'
| 'tzm'
| 'ug-cn'
| 'uk'
| 'ur'
| 'uz-latn'
| 'uz'
| 'vi'
| 'x-pseudo'
| 'yo'
| 'zh-cn'
| 'zh-hk'
| 'zh-tw'
| 'zh'

0 comments on commit aa88c69

Please sign in to comment.