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:
parent
2466414f39
commit
2007b9be53
2 changed files with 4 additions and 6 deletions
|
@ -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(×tamp))
|
||||
let now = Timestamp::now();
|
||||
let format = timeago::Formatter::new();
|
||||
let out_property = TemplateFunction::new(self_property, move |timestamp| {
|
||||
Ok(time_util::format_duration(×tamp, &now, &format))
|
||||
});
|
||||
Ok(language.wrap_string(out_property))
|
||||
});
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue