time.rs
//! Example showing how to localize Jiff timestamps
use icu_locale::locale;
use jiff::{Timestamp, ToSpan};
use l10n_embed::{Context, Localize};
fn main() -> Result<(), jiff::Error> {
// Create some timestamps
let current_timestamp = Timestamp::now();
let current_datetime = current_timestamp.in_tz("UTC")?;
let unix_epoch = Timestamp::UNIX_EPOCH;
let in_two_hours = current_timestamp.checked_add(2.hours())?;
let start_of_year = current_datetime.first_of_year()?.timestamp();
// Localize those timestamps into a relative time
let timestamps: [(&str, Timestamp); 4] = [
("Current time", current_timestamp),
("Unix epoch", unix_epoch),
("Two hours from now", in_two_hours),
("Since start of year (UTC)", start_of_year),
];
let context = Context::new(locale!("en-US"), true);
let mut buffer = String::new();
for (name, timestamp) in timestamps {
buffer.clear();
timestamp.localize(&context, &mut buffer);
println!("{name}: {buffer}");
}
Ok(())
}