Skip to content

How do I make the friendly format show units bigger than days? #250

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

You must be logged in to vote

Does the section on rounding help answer your question?

For your code, I would do it like this, by specifying the largest unit:

use jiff::{
    civil::Date,
    fmt::friendly::{Designator, SpanPrinter},
    Unit, Zoned,
};

fn main() -> anyhow::Result<()> {
    let date: Date = "2015-06-10".parse()?;
    let span = Zoned::now().date().since((Unit::Year, date))?;

    println!("It has been {}", format!("{span:#}"));
    println!("It has been {}", humanize(span));

    Ok(())
}

// Turn duration into human readable format
fn humanize(span: jiff::Span) -> String {
    let printer = SpanPrinter::new().designator(Designator::HumanTime);
    printer.span_to_string(&span)
}

Has this output:

$ ca…

Replies: 1 comment 2 replies

Comment options

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

@BurntSushi
Comment options

Answer selected by BurntSushi
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