-
Notifications
You must be signed in to change notification settings - Fork 544
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
Ergonomics for naive::Days and Months #1208
Comments
What's your use case for this? It seems unergonomic because you're using them in a way that we didn't foresee when designing them. Also how is |
Indeed, or the constructor
E.g., if I want an iterator that yields the same day/time on consecutive months, I currently write: struct It {
base: DateTime<Utc>,
here: u32,
}
impl It {
fn new(base: DateTime<Utc>) -> Self {
Self { base, here: 0 }
}
}
impl Iterator for It {
type Item = DateTime<Utc>;
fn next(&mut self) -> Option<Self::Item> {
let m = self.here;
self.here += 1;
self.base.checked_add_months(Months::new(m))
}
}
fn main() {
let dt = Utc.with_ymd_and_hms(2023, 07, 31, 12, 34, 56).single().unwrap();
dbg!(It::new(dt).take(5).collect::<Vec<_>>());
} Whereas the following would be easier and clearer (less unit confusion): // ...
here: Months,
// ...
Self { base, here: Months(0) }
// ...
let m = self.here;
self.here += Months(1); // or self.here.0 += 1, vel sim.
self.base.checked_add_months(m) I also can't write a helper that accepts fn consecutive(dt: DateTime<Utc>, months: Months) -> Vec<DateTime<Utc>> {
It::new(dt).take(months/* uh oh... */).collect()
} When I initially encountered |
Yes, they mostly exist for the |
Ok… |
Also I think all of this will be superseded by the |
naive::Days
andMonths
are extremely unergonomic at the moment; it's impossible to recover the number of days/months once one has been constructed, and the constructorsDays::new
andMonths::new
are rather verbose.Would it be possible for
chrono
to provideFrom
andInto
impls foru64
/u32
(respectively) and/or make each integerpub
so users can construct and destructure them?The text was updated successfully, but these errors were encountered: