Comments (4)
While Chrono is inherently tied to the proleptic Gregorian calendar, I don't think such specific functions are not a good fit for general purpose library. If you only care about the correct answer (and not the performance), the following can be used:
fn is_leap_year(year: i32) -> bool {
NaiveDate::from_ymd_opt(year, 2, 29).is_some()
}
fn last_day_of_month(year: i32, month: 32) -> u32 {
NaiveDate::from_ymd_opt(year, month + 1, 1).unwrap_or(NaiveDate::from_ymd(year + 1, 1, 1)).pred().day()
}
I think JodaTime does have methods like is_leap
, but mostly for supporting different calendar systems with varying leap definitions.
from chrono.
You can add this functionality for your project with:
trait NaiveDateExt {
fn days_in_month(&self) -> i32;
fn days_in_year(&self) -> i32;
fn is_leap_year(&self) -> bool;
}
impl NaiveDateExt for chrono::NaiveDate {
fn days_in_month(&self) -> i32 {
let month = self.month();
match month {
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
4 | 6 | 9 | 11 => 30,
2 => if self.is_leap_year() { 29 } else { 28 },
_ => panic!("Invalid month: {}" , month),
}
}
fn days_in_year(&self) -> i32 {
if self.is_leap_year() { 366 } else { 365 }
}
fn is_leap_year(&self) -> bool {
let year = self.year();
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
}
}
from chrono.
such specific functions are not a good fit for general purpose library
I apologize for necroposting, but could you explain why?
A safe variation of the last_day_of_month()
posted earlier,
pub fn last_of_month(year: i32, month: u32) -> Option<chrono::NaiveDate> {
chrono::NaiveDate::from_ymd_opt(year, month + 1, 1)
.or_else(|| chrono::NaiveDate::from_ymd_opt(year + 1, 1, 1))?
.pred_opt()
}
from chrono.
I have seen this request more than once, and I also think it is reasonable and useful to add.
#1247 includes NaiveDate::leap_year
, and #69 is the open issue for adding something like last_day_of_month()
. I used the same workaround as you in a number of cases.
I am not sure yet about the best API for something like last_day_of_month()
. In one case (the NaiveDate::diff_months_days()
method in #1247) it was not enough for me to know the number of days in the current month, it needed the number in the previous month. #69 (comment) suggests adding a type such as YearMonth
and implementing the method on that would be more flexible, and #203 also requests such a type.
from chrono.
Related Issues (20)
- Should `now` error on wasm (not on emscripten or wasi) without wasmbind? HOT 2
- Missing panic condition on doc HOT 1
- Why doesn't time support multiplication, division, mod? HOT 1
- Unexpected panics due to unwrap HOT 1
- Restart 0.5 branch? HOT 6
- input is not enough for unique date and time HOT 2
- Offer options to configure how to create rfc 3339 string HOT 6
- Split `clock` feature into `clock-now` and `clock-tz` HOT 6
- CI failures in MSRV jobs failing to pick up ahash version
- Operations on NaiveDate are not commutative. HOT 10
- Add `subsec` methods to `Duration` HOT 1
- Time formatting on en_US locale with '%X' not expected HOT 1
- FrameworkNotFound error when building for Apple using maturin HOT 1
- format paniced HOT 11
- panic with_timezone Jan 1, 1601 00:00 UTC HOT 32
- Add methods for float multiplication/division HOT 1
- panic with Local Timezone 2023/11/29 02:00:00 checked_sub_months HOT 2
- rfc HOT 1
- This API seems very complicated HOT 2
- Times before Unix epoch
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chrono.