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

feat(dal): change the month add and sub for days add and sub #4071

Merged
merged 1 commit into from
Sep 1, 2023

Conversation

p-fernandez
Copy link
Contributor

What change does this PR introduce?

Replaces the usage of addMonths and subMonths from date-fns to calculate the data retention dates to addDays and subDays. Also creates a single source of truth for the data retention values.

Why was this change needed?

This is my candidate to our bug of the year.
Today the pipelines started to fail for 2 tests blocking any API deployment. Doing a git bisect to find when we introduced the bug led to no conclusion, bug was still happening in commits from months ago!
Weird. Let's debug the test. Oh, it is failing because wrong dates calculation. Let's debug more. It is just one day, so weird... we are just substracting one month... wait a minute...!
Lightbulb moment. Today it is the last day of the month. Always tricky to be in the boundary. And to work with dates. Nah, not a problem of timezones (luckily!). Daytime light savings? No, our error difference is one full day, not one hour. What's going on?
Dig, dig, dig... No... I can't believe it! It is date-fns behaviour.

Being at the last day of the month, if you add one month it automatically takes you to the last day of the following month. That's the behaviour of addMonths. For substracting months, the behaviour of subMonths is... to reuse addMonths with a negative number. D.R.Y..
But there is a small problem. Well... small...

If for 28th February we add one month and then substract one month we get again the 28th February.
BUT... if for 31th August we add one month and then substract one month we get... 30th August.

Due the nature of the months it is very argueable when adding or substracting a month in which day you should end up depending on which month you are. I would favour consistency but this discussion on what's right is not easy.
Anyway, @scopsy kindly opened them a GitHub issue for discussion:
date-fns/date-fns#3506

Love, peace, and much support to the date-fns developers, maintainers and contributors. Your work is appreciated.

Other information (Screenshots)

@p-fernandez p-fernandez merged commit 190cfa8 into next Sep 1, 2023
27 checks passed
@p-fernandez p-fernandez deleted the feat-do-not-trust-date-fns-sub-months branch September 1, 2023 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants