From ac5d8eb7844dc849f38afbeaf70efdb218174f8a Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Fri, 11 Aug 2023 15:31:47 -0500 Subject: [PATCH] Add UTC format for timestamp formats. Thanks to @rauljordan for these changes. Add tests for new UTC timestamp format Add documentation for timestamp utc Update CHANGELOG.md --- CHANGELOG.md | 2 ++ cli/src/template_builder.rs | 7 +++++++ cli/tests/test_commit_template.rs | 13 +++++++++++++ docs/templates.md | 1 + 4 files changed, 23 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 722a2abbc..2a5bcf0ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -90,6 +90,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Revsets gained a new function `mine()` that aliases `author(exact:"your_email")`. +* `jj log` timestamp format now accepts `.utc()` to convert a timestamp to UTC. + ### Fixed bugs * `jj config set --user` and `jj config edit --user` can now be used outside of any repository. diff --git a/cli/src/template_builder.rs b/cli/src/template_builder.rs index cc40ae866..166827acf 100644 --- a/cli/src/template_builder.rs +++ b/cli/src/template_builder.rs @@ -448,6 +448,13 @@ fn build_timestamp_method<'a, L: TemplateLanguage<'a>>( time_util::format_absolute_timestamp_with(×tamp, &format) })) } + "utc" => { + template_parser::expect_no_arguments(function)?; + language.wrap_timestamp(TemplateFunction::new(self_property, |mut timestamp| { + timestamp.tz_offset = 0; + timestamp + })) + } _ => return Err(TemplateParseError::no_such_method("Timestamp", function)), }; Ok(property) diff --git a/cli/tests/test_commit_template.rs b/cli/tests/test_commit_template.rs index d21f84ef2..495d733b4 100644 --- a/cli/tests/test_commit_template.rs +++ b/cli/tests/test_commit_template.rs @@ -110,6 +110,19 @@ fn test_log_author_timestamp_ago() { ); } +#[test] +fn test_log_author_timestamp_utc() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); + let repo_path = test_env.env_root().join("repo"); + + let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "author.timestamp().utc()"]); + insta::assert_snapshot!(stdout, @r###" + @ 2001-02-02 21:05:07.000 +00:00 + ◉ 1970-01-01 00:00:00.000 +00:00 + "###); +} + #[test] fn test_log_default() { let test_env = TestEnvironment::default(); diff --git a/docs/templates.md b/docs/templates.md index 74f27b4d7..4601a5716 100644 --- a/docs/templates.md +++ b/docs/templates.md @@ -154,6 +154,7 @@ The following methods are defined. * `.ago() -> String`: Format as relative timestamp. * `.format(format: String) -> String`: Format with [the specified strftime-like format string](https://docs.rs/chrono/latest/chrono/format/strftime/). +* `.utc() -> Timestamp`: Convert timestamp into UTC timezone. ### TimestampRange type