-
Notifications
You must be signed in to change notification settings - Fork 533
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
Add NaiveDate::diff_months_days
#1247
Conversation
6dbdf56
to
5719b98
Compare
Codecov Report
@@ Coverage Diff @@
## 0.4.x #1247 +/- ##
=======================================
Coverage 91.38% 91.39%
=======================================
Files 37 37
Lines 16467 16506 +39
=======================================
+ Hits 15049 15086 +37
- Misses 1418 1420 +2
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
Code coverage is not happy, #1248 should help. |
5719b98
to
1fc6d8b
Compare
#416 seems to be a related request and can be solved with |
1fc6d8b
to
c33cd56
Compare
I think I'm open to something like |
I prefer just
I'll split it out |
c33cd56
to
1f6287a
Compare
NaiveDate::leap_year
and NaiveDate::diff_months_days
NaiveDate::diff_months_days
@djc What would you say if instead of this PR I work on adding struct CalendarPeriod {
months: u32,
days: u32,
time: core::time::Duration,
} from your comment in #579 (comment)? In other words create a type with full ISO 8601 duration support (I think I can do something reasonable with fractions also). I would like to start with adding the type and basic operations first, and add parsing and formatting later. |
That sounds much more interesting to me, particularly because I would like to excise https://github.com/PoiScript/iso8601-duration from my dependency trees. (At least that was used by async-graphql before -- apparently it is now using https://docs.rs/iso8601/latest/iso8601/.) |
Closing. I'll be back 😄 |
This is my oldest branch of chrono, that never got turned into a PR. From ca. 1½ year ago.
I kept putting it off, because arguing for the utility of something is not my hobby 😄.
NaiveDate::leap_year()
Sometimes you just need to know whether the current date falls in a leap year.
A naive way to determine this is to do it manually:
But that kind of defeats the purpose of using chrono, and it involves 3 divisions.
Better to do:
But I would say that is pretty creative API use, that few will come up with.
It also involves some work, while the information is right there as a flag in
NaiveDate
. I propose to expose it withNaiveDate::leap_year()
.See #29 and #69 for related issues.
NaiveDate::diff_months_days()
For a project I needed to know the number of whole months and remaining days between two dates.
This turns out to be a bit difficult, until you know how to look at the problem.
The correct answer is different if you are counting down towards a date, compared to counting the number of months and days since an event.
In both cases there is a reference date, and when using this method as
reference.diff_months_days(other)
you get the right answer without having to think about it much.NaiveDate::diff_months_days
provides a human way to talk about larger durations: "there are 4 months and 13 days between those dates". Thedays
value can then of course be rounded to something like weeks without much effort: "there are 4 months and 2 weeks between those dates". Or "4½ months".Also the value of
months
can be turned intoyears
andmonths
without much effort.But the difficult part, getting
months
anddays
, is whatNaiveDate::diff_months_days
is for.