-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issue #1596 - Provide way to specify multiple formats when creating a date in UTC #1914
Open
BePo65
wants to merge
25
commits into
iamkun:dev
Choose a base branch
from
BePo65:fix/issue1596
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
54164cc
chore(release): 1.11.5 [skip ci]
semantic-release-bot 52e42e7
Merge branch 'master' of github.com:iamkun/dayjs
BePo65 29813a8
Merge branch 'master' of github.com:iamkun/dayjs
BePo65 33fbeae
Merge branch 'master' of github.com:iamkun/dayjs
BePo65 5ff4326
test: update duplicate test descriptions
BePo65 27cf19f
test: move tests for utc with customParseFormat to separate file
BePo65 9925918
fix: parsing with multiple formats does not respect 'utc'
BePo65 4835039
test: reorder and rename tests
BePo65 209fb14
fix: arguments is of type object, but parse expects an array
BePo65 006af9a
fix: just using c parameter creates self reference
BePo65 f6dee59
fix: taking care of date with 'Z' (offset +00:00) in strict mode
BePo65 6ce64f2
test: add test for offset + strict and without utc
BePo65 f7b8488
fix: disallow overflow on customParseFormat with 'strict'
BePo65 a45fced
fix: handle parsing of token, when not found at beginning of input
BePo65 bf167af
test: add tests for incomatibilities with moment.js
BePo65 c6d7ce3
test: remove 'strict mode' tests (are now part of 'compatibility')
BePo65 0ada826
test: moved test 'invalid Dates' to 'incomatibilities' tests
BePo65 0aa776d
test: add new tests for 'strict mode'
BePo65 9fb3c2b
test: add test cases for customParseFormat
BePo65 1d6aa7c
test: move test case 'parse X x' to the end of the file
BePo65 8118da1
test: improve code coverage for customParseFormat
BePo65 5f5dbd9
refactor: replace spread operator for compatibility with older js ver…
BePo65 375d551
fix: error reported by eslint (line-length)
BePo65 47a683b
test: add tests for customParseFormat.utils.js
BePo65 af75753
test: add tests for issue #1734
BePo65 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can use the built-in API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry for the late reply, but somehow I missed this comment :-(
Of course we could use
dayjs(year + '-' + month).daysInMonth()
instead of my 'auxiliary' method.But the problem is that we do not have the dayjs object available to the
checkOverflow()
function.Therefore I created the
daysInMonth
method in the customParseFormat/utils file.As an additional bonus, this function runs faster than creating a new dayjs object (to be exact, more than 1 instance is created for getting daysInMonth).
What do you think about this?