Skip to content

Why doesn't Jiff support 3.years() + 1.month() + 5.days() + 10.hours()? #41

Answered by BurntSushi
BurntSushi asked this question in Q&A
Discussion options

You must be logged in to vote

I think the simplest way to look at the essential problem here is to ask this: what is the answer to 2.months().days(25) + 10.days()? Well, Jiff provides Span::checked_add. And surely, span1 + span2 would just use Span::checked_add and panic on failure, right? Right? Right? And surely, there is absolutely nothing wrong with adding together these two spans!

use jiff::ToSpan;

fn main() -> anyhow::Result<()> {
    let sp1 = 2.months().days(25);
    let sp2 = 10.days();
    let sum = sp1.checked_add(sp2)?;
    println!("{sum}");
    Ok(())
}

Has this output:

$ cargo -q r
Error: using largest unit (which is 'month') in given span requires that a relative reference time be given, but none was …

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by BurntSushi
Comment options

You must be logged in to vote
2 replies
@BurntSushi
Comment options

@epidemian
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants