Skip to content

Commit

Permalink
Merge af75753 into 00c223b
Browse files Browse the repository at this point in the history
  • Loading branch information
BePo65 authored Mar 25, 2023
2 parents 00c223b + af75753 commit 9bf4076
Show file tree
Hide file tree
Showing 10 changed files with 438 additions and 93 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## [1.11.7](https://github.com/iamkun/dayjs/compare/v1.11.6...v1.11.7) (2022-12-06)


### Bug Fixes

* Add locale (zh-tw) meridiem ([#2149](https://github.com/iamkun/dayjs/issues/2149)) ([1e9ba76](https://github.com/iamkun/dayjs/commit/1e9ba761ff4e3f2759106dfe1aa9054d5826451c))
* update fa locale ([#2151](https://github.com/iamkun/dayjs/issues/2151)) ([1c26732](https://github.com/iamkun/dayjs/commit/1c267321a1a01b4947e1482bac67d67ebc7c3dfa))

## [1.11.6](https://github.com/iamkun/dayjs/compare/v1.11.5...v1.11.6) (2022-10-21)


Expand Down
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ const dayjs = function (date, c) {
return date.clone()
}
// eslint-disable-next-line no-nested-ternary
const cfg = typeof c === 'object' ? c : {}
let cfg = {}
if ((typeof c === 'object') && !Array.isArray(c)) {
// clone c (contains cfg object)
cfg = JSON.parse(JSON.stringify(c))
}
cfg.date = date
cfg.args = arguments// eslint-disable-line prefer-rest-params
if (cfg.args === undefined) {
cfg.args = Array.prototype.slice.call(arguments) // eslint-disable-line prefer-rest-params
}
return new Dayjs(cfg) // eslint-disable-line no-use-before-define
}

Expand Down
82 changes: 60 additions & 22 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { u } from '../localizedFormat/utils'
import { daysInMonth } from './utils'

const formattingTokens = /(\[[^[]*\])|([-_:/.,()\s]+)|(A|a|YYYY|YY?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g

Expand Down Expand Up @@ -138,6 +139,30 @@ function correctHours(time) {
}
}

function checkOverflow(year, month, day, hours, minutes, seconds, milliseconds) {
let overflow = false
if (month) {
overflow = overflow || (month < 0 || month > 12)
}
if ((day !== undefined) && (day !== null)) {
overflow = overflow || (day < 1 || day > daysInMonth(year, month))
}
if (hours) {
overflow = overflow || (hours < 0 || hours > 24 ||
(hours === 24 && (minutes !== 0 || seconds !== 0 || milliseconds !== 0)))
}
if (minutes) {
overflow = overflow || (minutes < 0 || minutes > 59)
}
if (seconds) {
overflow = overflow || (seconds < 0 || seconds > 59)
}
if (milliseconds) {
overflow = overflow || (milliseconds < 0 || milliseconds > 999)
}
return overflow
}

function makeParser(format) {
format = u(format, locale && locale.formats)
const array = format.match(formattingTokens)
Expand All @@ -154,32 +179,48 @@ function makeParser(format) {
}
}
return function (input) {
const time = {}
for (let i = 0, start = 0; i < length; i += 1) {
const time = { strictMatch: true }
let start = 0
for (let i = 0; i < length; i += 1) {
const token = array[i]
if (typeof token === 'string') {
const separator = input.slice(start, start + token.length)
time.strictMatch = time.strictMatch && (separator === token)
start += token.length
} else {
const { regex, parser } = token
const part = input.slice(start)
const match = regex.exec(part)
const value = match[0]
parser.call(time, value)
input = input.replace(value, '')
if (match !== null) {
const value = match[0] // eslint-disable-line prefer-destructuring
parser.call(time, value)
input = input.replace(value, '')
if (match.index !== 0) {
time.strictMatch = false
start += match.index
}
} else {
time.strictMatch = false
break
}
}
}
if (start < input.length) {
time.strictMatch = false
}
correctHours(time)
return time
}
}

const parseFormattedInput = (input, format, utc) => {
const parseFormattedInput = (input, format, utc, strict) => {
try {
if (['x', 'X'].indexOf(format) > -1) return new Date((format === 'X' ? 1000 : 1) * input)
const parser = makeParser(format)
const {
year, month, day, hours, minutes, seconds, milliseconds, zone
year, month, day, hours, minutes, seconds, milliseconds, zone, strictMatch
} = parser(input)
const isOverflow = checkOverflow(year, month, day, hours, minutes, seconds, milliseconds)
const now = new Date()
const d = day || ((!year && !month) ? now.getDate() : 1)
const y = year || now.getFullYear()
Expand All @@ -191,19 +232,22 @@ const parseFormattedInput = (input, format, utc) => {
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))
let parsedDate = new Date('') // Invalid Date
if (!strict || (strict && strictMatch && !isOverflow)) {
if (zone) {
parsedDate = new Date(Date.UTC(y, M, d, h, m, s, ms + (zone.offset * 60 * 1000)))
} else if (utc) {
parsedDate = new Date(Date.UTC(y, M, d, h, m, s, ms))
} else {
parsedDate = new Date(y, M, d, h, m, s, ms)
}
}
return new Date(y, M, d, h, m, s, ms)
return parsedDate
} catch (e) {
return new Date('') // Invalid Date
}
}


export default (o, C, d) => {
d.p.customParseFormat = true
if (o && o.parseTwoDigitYear) {
Expand All @@ -229,22 +273,16 @@ export default (o, C, d) => {
if (!isStrictWithoutLocale && pl) {
locale = d.Ls[pl]
}
this.$d = parseFormattedInput(date, format, utc)
this.$d = parseFormattedInput(date, format, utc, isStrict)
this.init()
if (pl && pl !== true) this.$L = this.locale(pl).$L
// use != to treat
// input number 1410715640579 and format string '1410715640579' equal
// eslint-disable-next-line eqeqeq
if (isStrict && date != this.format(format)) {
this.$d = new Date('')
}
// reset global locale to make parallel unit test
locale = {}
} else if (format instanceof Array) {
const len = format.length
for (let i = 1; i <= len; i += 1) {
args[1] = format[i - 1]
const result = d.apply(this, args)
const result = d.apply(this, [date, cfg])
if (result.isValid()) {
this.$d = result.$d
this.$L = result.$L
Expand Down
26 changes: 26 additions & 0 deletions src/plugin/customParseFormat/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// handle negative values when overflowing month (handling positive and negative values)
function modMonth(n, x) {
return ((n % x) + x) % x
}

// code duplication as we cannot use 'isLeapYear' plugin here;
// 'isLeapYear' is only working with Dayjs objects
function isLeapYear(year) {
return ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)
}

// eslint-disable-next-line import/prefer-default-export
export const daysInMonth = (year, month) => {
if (Number.isNaN(year) || Number.isNaN(month)) {
return NaN
}
const monthAsIndex = month - 1
const monthInYear = modMonth(monthAsIndex, 12)
year += (monthAsIndex - monthInYear) / 12
// eslint-disable-next-line no-nested-ternary
return monthInYear === 1
? isLeapYear(year)
? 29
: 28
: 31 - ((monthInYear % 7) % 2)
}
3 changes: 2 additions & 1 deletion src/plugin/utc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ function offsetFromString(value = '') {
export default (option, Dayjs, dayjs) => {
const proto = Dayjs.prototype
dayjs.utc = function (date) {
const cfg = { date, utc: true, args: arguments } // eslint-disable-line prefer-rest-params
// eslint-disable-next-line prefer-rest-params
const cfg = { date, utc: true, args: Array.prototype.slice.call(arguments) }
return new Dayjs(cfg) // eslint-disable-line no-use-before-define
}

Expand Down
2 changes: 1 addition & 1 deletion test/comparison.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ test('is after invalid', () => {

// isBefore()

test('is after without units', () => {
test('is before without units', () => {
const m = dayjs(new Date(2011, 3, 2, 3, 4, 5, 10))
const mCopy = dayjs(m)
expect(m.isBefore(dayjs(new Date(2012, 3, 2, 3, 5, 5, 10)))).toBe(true, 'year is later')
Expand Down
Loading

0 comments on commit 9bf4076

Please # to comment.