Skip to content

Commit

Permalink
settings: parse TOML date-time value as well as string timestamp
Browse files Browse the repository at this point in the history
We can usually omit quotes in --config=NAME=VALUE, but an RFC3339 string is a
valid TOML date-time expression. It's weird that quoting is required to specify
a date-time value.
  • Loading branch information
yuja committed Dec 18, 2024
1 parent 4f08c62 commit 6bbaf79
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
8 changes: 4 additions & 4 deletions cli/tests/test_revset_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ fn test_revset_committer_date_with_time_zone() {
test_env.jj_cmd_ok(
&repo_path,
&[
"--config=debug.commit-timestamp='2023-01-25T11:30:00-05:00'",
"--config=debug.commit-timestamp=2023-01-25T11:30:00-05:00",
"describe",
"-m",
"first",
Expand All @@ -749,7 +749,7 @@ fn test_revset_committer_date_with_time_zone() {
test_env.jj_cmd_ok(
&repo_path,
&[
"--config=debug.commit-timestamp='2023-01-25T12:30:00-05:00'",
"--config=debug.commit-timestamp=2023-01-25T12:30:00-05:00",
"new",
"-m",
"second",
Expand All @@ -758,7 +758,7 @@ fn test_revset_committer_date_with_time_zone() {
test_env.jj_cmd_ok(
&repo_path,
&[
"--config=debug.commit-timestamp='2023-01-25T13:30:00-05:00'",
"--config=debug.commit-timestamp=2023-01-25T13:30:00-05:00",
"new",
"-m",
"third",
Expand All @@ -768,7 +768,7 @@ fn test_revset_committer_date_with_time_zone() {
let mut log_commits_before_and_after =
|committer_date: &str, now: &str, tz: &str| -> (String, String) {
test_env.add_env_var("TZ", tz);
let config = format!("debug.commit-timestamp='{now}'");
let config = format!("debug.commit-timestamp={now}");
let before_log = test_env.jj_cmd_success(
&repo_path,
&[
Expand Down
9 changes: 7 additions & 2 deletions lib/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,17 @@ impl SignSettings {
}

fn to_timestamp(value: ConfigValue) -> Result<Timestamp, Box<dyn std::error::Error + Send + Sync>> {
// TODO: Maybe switch to native TOML date-time type?
// Since toml_edit::Datetime isn't the date-time type used across our code
// base, we accept both string and date-time types.
if let Some(s) = value.as_str() {
Ok(Timestamp::from_datetime(DateTime::parse_from_rfc3339(s)?))
} else if let Some(d) = value.as_datetime() {
// It's easier to re-parse the TOML date-time expression.
let s = d.to_string();
Ok(Timestamp::from_datetime(DateTime::parse_from_rfc3339(&s)?))
} else {
let ty = value.type_name();
Err(format!("invalid type: {ty}, expected a date-time string").into())
Err(format!("invalid type: {ty}, expected a date-time").into())
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tests/test_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ fn stable_op_id_settings() -> UserSettings {
config.add_layer(
ConfigLayer::parse(
ConfigSource::User,
"debug.operation-timestamp = '2001-02-03T04:05:06+07:00'",
"debug.operation-timestamp = 2001-02-03T04:05:06+07:00",
)
.unwrap(),
);
Expand Down

0 comments on commit 6bbaf79

Please sign in to comment.