Skip to content
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

Parse 'YYYY' correctly #969

Merged
merged 3 commits into from
Jul 29, 2020
Merged

Parse 'YYYY' correctly #969

merged 3 commits into from
Jul 29, 2020

Conversation

dyoshikawa
Copy link
Contributor

@dyoshikawa dyoshikawa commented Jul 25, 2020

When only 'YYYY', parsed result was incorrect.

TZ=Asia/Tokyo yarn jest ./test/parse.test.js

Output:

  ● Parse › moment-js like formatted dates

    expect(received).toBe(expected) // Object.is equality
    
    Expected value to be:
      "2018-01-01T00:00:00+09:00"
    Received:
      "2018-01-01T09:00:00+09:00"

      37 |     expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
      38 |     d = '2018'
    > 39 |     expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend
      40 |     d = '2018-05-02T11:12:13Z' // should go direct to new Date() rather our regex
      41 |     expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend
      42 |   })
      
      at Object.<anonymous> (test/parse.test.js:39:31)

Failed this test in my local env which is in Asia/tokyo time zone.

Compare dayjs and moment:

// TZ=Asia/Tokyo

import dayjs from 'dayjs'
import moment from 'moment'

console.log(dayjs('2018').format())
// 2018-01-01T09:00:00+09:00

console.log(moment('2018').format())
// 2018-01-01T00:00:00+09:00

So I think expected behavior: 2018-01-01T00:00:00+09:00 .

@codecov
Copy link

codecov bot commented Jul 25, 2020

Codecov Report

Merging #969 into dev will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##               dev      #969   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          169       169           
  Lines         1429      1430    +1     
  Branches       305       306    +1     
=========================================
+ Hits          1429      1430    +1     
Impacted Files Coverage Δ
src/constant.js 100.00% <100.00%> (ø)
src/index.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 73813ab...fdc46e9. Read the comment docs.

src/index.js Outdated
|| 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], d[2] ? d[2] - 1 : 0, d[3]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If d[2] was undefined, set default value.

@@ -60,10 +60,11 @@ const parseDate = (cfg) => {
const d = date.match(C.REGEX_PARSE)
if (d) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const _m = d[2] - 1 || 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I adopted it.

@iamkun iamkun merged commit 70c1239 into iamkun:dev Jul 29, 2020
iamkun pushed a commit that referenced this pull request Jul 29, 2020
## [1.8.31](v1.8.30...v1.8.31) (2020-07-29)

### Bug Fixes

* Rollback LocalePresetType to string ([#968](#968)) ([b342bd3](b342bd3))
* Update Regex to parse 'YYYY' correctly ([#969](#969)) ([70c1239](70c1239))
andrewhood125ruhuc added a commit to andrewhood125ruhuc/SidRH2 that referenced this pull request May 10, 2022
## [1.8.31](iamkun/dayjs@v1.8.30...v1.8.31) (2020-07-29)

### Bug Fixes

* Rollback LocalePresetType to string ([#968](iamkun/dayjs#968)) ([b342bd3](iamkun/dayjs@b342bd3))
* Update Regex to parse 'YYYY' correctly ([#969](iamkun/dayjs#969)) ([70c1239](iamkun/dayjs@70c1239))
andrewhood125ruhuc added a commit to andrewhood125ruhuc/SidRH2 that referenced this pull request May 10, 2022
## [1.8.31](iamkun/dayjs@v1.8.30...v1.8.31) (2020-07-29)

### Bug Fixes

* Rollback LocalePresetType to string ([#968](iamkun/dayjs#968)) ([b342bd3](iamkun/dayjs@b342bd3))
* Update Regex to parse 'YYYY' correctly ([#969](iamkun/dayjs#969)) ([70c1239](iamkun/dayjs@70c1239))
splashwizard pushed a commit to splashwizard/tracking-time that referenced this pull request Oct 21, 2024
## [1.8.31](iamkun/dayjs@v1.8.30...v1.8.31) (2020-07-29)

### Bug Fixes

* Rollback LocalePresetType to string ([#968](iamkun/dayjs#968)) ([b342bd3](iamkun/dayjs@b342bd3))
* Update Regex to parse 'YYYY' correctly ([#969](iamkun/dayjs#969)) ([70c1239](iamkun/dayjs@70c1239))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants