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

Set year/month/day all at once #549

Closed
zyxw59 opened this issue Mar 22, 2021 · 5 comments
Closed

Set year/month/day all at once #549

zyxw59 opened this issue Mar 22, 2021 · 5 comments

Comments

@zyxw59
Copy link

zyxw59 commented Mar 22, 2021

With the Datelike trait, there are methods to return a new date with a different year/month/day. However, these do not provide the ability to easily set more than one of those at once, since the intermediate value might not be valid.

e.g.

let a = NaiveDate::from_ymd(2019, 1, 29);
let b = NaiveDate::from_ymd(2020, 2, 29);

fn with_year_month_1<D: Datelike>(date: &D, year: i32, month: u32) -> Option<D> {
  date.with_year(year)?.with_month(month)?
}

fn with_year_month_2<D: Datelike><date: &D, year: i32, month: u32) -> Option<D> {
  date.with_month(month)?.with_year(year)?
}

with_year_month_1(a, 2020, 2); // -> Some(2020-02-29)
with_year_month_2(a, 2020, 2); // -> None (2019-02-29 is invalid)
with_year_month_1(b, 2019, 1); // -> None (2019-02-29 is invalid)
with_year_month_2(b, 2019, 1); // -> Some(2020-01-29)

It would be useful to have a method which applied multiple of these methods at once, without checking for validity of intermediate dates.

@pitdicker
Copy link
Collaborator

If you want to replace two of the three parts of a date, is it not just as easy to create a new date?
Example:

use chrono::{NaiveDate, Datelike};

let a = NaiveDate::from_ymd(2019, 1, 29);
let b = NaiveDate::from_ymd(2020, 2, 29);

NaiveDate::from_ymd_opt(2020, 2, a.day()); // Some(2020-02-29)
NaiveDate::from_ymd_opt(2019, 1, b.day()); // Some(2019-01-29)

This is less tricky to get right.

@pitdicker pitdicker closed this as not planned Won't fix, can't repro, duplicate, stale Jul 4, 2023
@jtmoon79
Copy link
Contributor

jtmoon79 commented Jul 15, 2023

At first glance, I'm hesitant to add more API "surface area" for something that is fairly trivial for the user to do.

However, the OP makes a good point that there is this not-immediately-obvious "gotcha" that will probably trip-up others.
While @pitdicker provide some good alternate examples of how to achieve the desired end, the "gotcha" still exists and it could easily be protected against with fairly trivial new functions.

I recommend re-opening this Issue. (My two cents)

@pitdicker
Copy link
Collaborator

Maybe we should add some documentation to Datelike::with_month or even to the Datelike trait itself on how to change multiple values at once.

@pitdicker pitdicker reopened this Jul 16, 2023
@jtmoon79
Copy link
Contributor

Maybe we should add some documentation to Datelike::with_month or even to the Datelike trait itself on how to change multiple values at once.

My preference is the OPs suggestion but documentation is better than nothing.

@pitdicker
Copy link
Collaborator

Added some documentation in #1199.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants