Skip to content

feat: Add negative years support - #2640

Merged
iamkun merged 10 commits into
iamkun:devfrom
klm-turing:feat-negative-year-support
May 12, 2024
Merged

feat: Add negative years support #2640
iamkun merged 10 commits into
iamkun:devfrom
klm-turing:feat-negative-year-support

Conversation

@klm-turing

@klm-turing klm-turing commented Apr 26, 2024

Copy link
Copy Markdown
Contributor

Add ability to parse negative years

This fix 2636 issue

@willismonroe

Copy link
Copy Markdown

This looks good, I added a test (in manipulate.test.js):

it('Add and subtract with negative years', () => {
  expect(dayjs('-2006').add(1, 'y')).toEqual(dayjs('-2005'))
  expect(dayjs('-2006').subtract(1, 'y')).toEqual(dayjs('-2007'))
})

Just to make sure that adding/subtracting were working well. I tried to commit to your fork, but it didn't work.

@klm-turing

klm-turing commented Apr 26, 2024

Copy link
Copy Markdown
Contributor Author

@willismonroe I added those test for you
Screenshot from 2024-04-26 17-41-41

@willismonroe

Copy link
Copy Markdown

That looks good!

@klm-turing

klm-turing commented Apr 29, 2024

Copy link
Copy Markdown
Contributor Author

Hi Dear @willismonroe It's now a plugin to be extended as @iamkun suggested

Comment thread test/plugin/negativeYear.test.js Outdated
const date3 = '01-03--2021'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).format('YYYY-MM-DD')).toBe(`-${moment(date).format('YYYY-MM-DD')}`)
expect(dayjs(date).format()).toBe(`-${moment(date).format()}`)

@iamkun iamkun Apr 30, 2024

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

could we remove moment in this test case? as far as i know, moment does not support nagative year

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@iamkun Sure

Comment thread src/plugin/negativeYear/index.js Outdated
if (typeof date === 'string' && date.indexOf(`-${fullYear}`) !== -1) {
return dayjs(newDate).subtract(fullYear * 2, 'year').toDate()
}
return newDate

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

we can return the passed in the original cfg.date directly, without dealing with logic like !date

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@iamkun Got you

@klm-turing

klm-turing commented Apr 30, 2024

Copy link
Copy Markdown
Contributor Author

@iamkun Changes are done. Let me know if anything else will be great

Comment thread src/plugin/negativeYear/index.js Outdated
if (typeof date === 'string' && date.indexOf(`-${fullYear}`) !== -1) {
return dayjs(newDate).subtract(fullYear * 2, 'year').toDate()
}
return newDate

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

maybe we should return date not newDate, cause cfg.date is the untouched original one, just leave it to the current main parse logic

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@iamkun Updated

@iamkun
iamkun merged commit 6a42e0d into iamkun:dev May 12, 2024
@iamkun

iamkun commented May 12, 2024

Copy link
Copy Markdown
Owner

THX

@klm-turing
klm-turing deleted the feat-negative-year-support branch May 13, 2024 15:35
github-actions Bot pushed a commit that referenced this pull request Jul 18, 2024
## [1.11.12](v1.11.11...v1.11.12) (2024-07-18)

### Bug Fixes

* Add NegativeYear Plugin support  ([#2640](#2640)) ([6a42e0d](6a42e0d))
* add UTC support to negativeYear plugin ([#2692](#2692)) ([f3ef705](f3ef705))
* Fix zero offset issue when use tz with locale ([#2532](#2532)) ([d0e6738](d0e6738))
* Improve typing for min/max plugin ([#2573](#2573)) ([4fbe94a](4fbe94a))
* timezone plugin currect parse UTC tz ([#2693](#2693)) ([b575c81](b575c81))
splashwizard pushed a commit to splashwizard/tracking-time that referenced this pull request Oct 21, 2024
## [1.11.12](iamkun/dayjs@v1.11.11...v1.11.12) (2024-07-18)

### Bug Fixes

* Add NegativeYear Plugin support  ([#2640](iamkun/dayjs#2640)) ([6a42e0d](iamkun/dayjs@6a42e0d))
* add UTC support to negativeYear plugin ([#2692](iamkun/dayjs#2692)) ([f3ef705](iamkun/dayjs@f3ef705))
* Fix zero offset issue when use tz with locale ([#2532](iamkun/dayjs#2532)) ([d0e6738](iamkun/dayjs@d0e6738))
* Improve typing for min/max plugin ([#2573](iamkun/dayjs#2573)) ([4fbe94a](iamkun/dayjs@4fbe94a))
* timezone plugin currect parse UTC tz ([#2693](iamkun/dayjs#2693)) ([b575c81](iamkun/dayjs@b575c81))
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.

Can't input negative years

3 participants