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 elapsed_years
to Date
and DateTime
#557
Merged
Merged
Changes from 25 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
4d87d7f
Add elapsed_years() to Date
yozhgoor b3ee278
Add elapsed_years() to DateTime
yozhgoor a493f26
Add elapsed_years() to NaiveDateTime
yozhgoor 54274a2
Oops
yozhgoor fb25a77
Add elapsed_years() to NaiveDateTime
yozhgoor f059cb3
WIP
yozhgoor a5ce0b9
Remove elapsed_years from naive Date and DateTime
yozhgoor 73eac80
Add a consts module with WEEK_PER_YEAR
yozhgoor 351c1cf
Add better tests
yozhgoor 767f87a
Change variable name
yozhgoor 6d569fb
Clean up
yozhgoor 6ab485a
Clean up
yozhgoor 5d1af60
Update changelog and authors
yozhgoor cab008d
Replace the Utc use by the timezone of the Date
yozhgoor f676c1f
Clean up
yozhgoor 4c52ec7
Adapt changelog
yozhgoor 5e3eba3
Update src/lib.rs
yozhgoor bd6a7a7
Update src/lib.rs
yozhgoor 3006303
Update CHANGELOG.md
yozhgoor 520d83e
Merge branch 'main' into elapsed-years
Milo123459 3fa7eaa
Update CHANGELOG.md
Milo123459 a069d56
Update src/datetime.rs
Milo123459 a4a026a
Update
yozhgoor d82afcf
Use the clock feature when using `Utc::now` and `Utc::today`
yozhgoor b467923
Merge branch 'main' into elapsed-years
yozhgoor 7816be1
New blank line at EOF
yozhgoor 5a90aff
Merge branch 'elapsed-years' of github.com:yozhgoor/chrono into elaps…
yozhgoor ba17106
Merge branch 'main' into elapsed-years
yozhgoor 81fabab
Move the code from `datetime.rs` to `datetime/mod.rs` and delete `dat…
yozhgoor d893578
Clean up
yozhgoor 83aab1a
After review
yozhgoor 07f1e43
Add `crate::` to the use statement
yozhgoor 22945a2
Replace `TryFrom`
yozhgoor 492c802
Merge branch 'main' into elapsed-years
yozhgoor File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 | ||||
---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ | |||||
#[cfg(any(feature = "alloc", feature = "std", test))] | ||||||
use core::borrow::Borrow; | ||||||
use core::cmp::Ordering; | ||||||
use core::convert::TryFrom; | ||||||
use core::ops::{Add, Sub}; | ||||||
use core::{fmt, hash}; | ||||||
use oldtime::Duration as OldDuration; | ||||||
|
@@ -271,6 +272,20 @@ impl<Tz: TimeZone> Date<Tz> { | |||||
pub fn naive_local(&self) -> NaiveDate { | ||||||
self.date | ||||||
} | ||||||
|
||||||
/// Retrieve the elapsed years from now to the given [`Date`]. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consistency with other's function docs
Suggested change
|
||||||
#[cfg(feature = "clock")] | ||||||
pub fn elapsed_years(&self) -> u32 { | ||||||
let now = Utc::today().with_timezone(&self.timezone()); | ||||||
|
||||||
let years = if (now.month(), now.day()) < (self.month(), self.day()) { | ||||||
now.year() - self.year() - 1 | ||||||
} else { | ||||||
now.year() - self.year() | ||||||
}; | ||||||
|
||||||
u32::try_from(years).unwrap_or(0) | ||||||
} | ||||||
} | ||||||
|
||||||
/// Maps the local date to other date with given conversion function. | ||||||
|
@@ -494,3 +509,26 @@ where | |||||
write!(f, "{}{}", self.naive_local(), self.offset) | ||||||
} | ||||||
} | ||||||
|
||||||
#[cfg(test)] | ||||||
mod tests { | ||||||
use consts::f64; | ||||||
use offset::Utc; | ||||||
use oldtime::Duration; | ||||||
|
||||||
#[test] | ||||||
#[cfg(feature = "clock")] | ||||||
fn test_years_elapsed() { | ||||||
// This is always at least one year because 1 year = 52.1775 weeks. | ||||||
let one_year_ago = | ||||||
Utc::today() - Duration::weeks((f64::WEEKS_PER_YEAR * 1.5).ceil() as i64); | ||||||
// A bit more than 2 years. | ||||||
let two_year_ago = | ||||||
Utc::today() - Duration::weeks((f64::WEEKS_PER_YEAR * 2.5).ceil() as i64); | ||||||
|
||||||
assert_eq!(one_year_ago.elapsed_years(), 1); | ||||||
assert_eq!(two_year_ago.elapsed_years(), 2); | ||||||
// if the given Date is later than now, the function will always return 0. | ||||||
assert_eq!((Utc::today() + Duration::weeks(12)).elapsed_years(), 0); | ||||||
} | ||||||
} |
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
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.
Please remove this useless change