ok/jj
1
0
Fork 0
forked from mirrors/jj

templater: use cached current timestamp to calculate .ago()

For better or worse, this produces more stable output. It's also a bit faster.
This commit is contained in:
Yuya Nishihara 2023-03-14 11:59:31 +09:00
parent 2466414f39
commit 2007b9be53
2 changed files with 4 additions and 6 deletions

View file

@ -665,8 +665,10 @@ fn builtin_timestamp_methods<'a, L: TemplateLanguage<'a>>(
let mut map = TemplateBuildMethodFnMap::<L, Timestamp>::new();
map.insert("ago", |language, _build_ctx, self_property, function| {
template_parser::expect_no_arguments(function)?;
let out_property = TemplateFunction::new(self_property, |timestamp| {
Ok(time_util::format_timestamp_relative_to_now(&timestamp))
let now = Timestamp::now();
let format = timeago::Formatter::new();
let out_property = TemplateFunction::new(self_property, move |timestamp| {
Ok(time_util::format_duration(&timestamp, &now, &format))
});
Ok(language.wrap_string(out_property))
});

View file

@ -81,7 +81,3 @@ pub fn format_duration(from: &Timestamp, to: &Timestamp, format: &timeago::Forma
.map(|duration| format.convert(duration))
.unwrap_or_else(|| "<out-of-range date>".to_string())
}
pub fn format_timestamp_relative_to_now(timestamp: &Timestamp) -> String {
format_duration(timestamp, &Timestamp::now(), &timeago::Formatter::new())
}