Elffuss BraidHub

utopia-ia/elffuss-braid public

Signatures in local time

New changesets were signed with an offset of zero, so every time read
as UTC. braid_repo::local_now takes the offset of the local time zone
from jiff, which finds it from TZ (a POSIX rule or a zone name) or the
system's setting without calling into the C library, and falls back to
+0000 when it cannot. Commits, change create, the rebased changesets of
land and update, and the merges of git sync all sign with it.

jiff was already in the dependency graph through gix-date; braid asks
for std, tz-system and tzdb-zoneinfo only, so nothing is bundled and no
new crate comes in. Its licence, Unlicense or MIT, was allowed already.

log still shows minutes in the signature's own offset; JSON keeps the
seconds and the offset. The test runs braid under a few TZ values,
including one that cannot be found.
Changesetc03d76685fb33bcdc652e8b553500edf4696228b9524c668dccfe7557dbbfa6b
AuthorKikoCis <KikoCis@users.noreply.github.com> · 2026-10-15 23:38 +02:00
CommitterKikoCis <KikoCis@users.noreply.github.com> · 2026-10-15 23:38 +02:00
Parentsab37fe03cdad

Browse the files at this changeset

8 files changed

modified Cargo.lock +1 −0
@@ -530,6 +530,7 @@
530530 "elffuss-braid-format",
531531 "elffuss-braid-git",
532532 "elffuss-braid-store",
533+ "jiff",
533534 "tempfile",
534535 "thiserror",
535536 ]
modified crates/cli/tests/cli.rs +50 −0
@@ -247,6 +247,56 @@
247247 assert!(err.contains("is a blob, not a changeset"), "{err}");
248248 }
249249
250+#[test]
251+fn signatures_carry_the_local_offset() {
252+ let dir = tempfile::tempdir().unwrap();
253+ assert!(
254+ braid()
255+ .arg("init")
256+ .arg(dir.path())
257+ .output()
258+ .unwrap()
259+ .status
260+ .success()
261+ );
262+ let file = dir.path().join("a.txt");
263+ std::fs::write(&file, b"a\n").unwrap();
264+ let set = format!("a.txt=@{}", file.display());
265+ // POSIX rules, UTC, and a zone that cannot be found, which reads as UTC.
266+ for (zone, minutes, shown) in [
267+ ("<+0530>-05:30", 330, "+0530"),
268+ ("<-03>3", -180, "-0300"),
269+ ("UTC0", 0, "+0000"),
270+ ("Nowhere/Nothing", 0, "+0000"),
271+ ] {
272+ let made = run_json(
273+ braid_in(dir.path())
274+ .env("TZ", zone)
275+ .env("BRAID_AUTHOR", "Ada <ada@example.org>")
276+ .args(["change", "create", "--base", "none", "--set", &set])
277+ .args(["-m", zone]),
278+ );
279+ let id = made["change"].as_str().unwrap();
280+ // JSON keeps the seconds and the offset.
281+ let show = run_json(braid_in(dir.path()).args(["show", id]));
282+ for who in ["author", "committer"] {
283+ assert_eq!(show[who]["offset_minutes"], minutes, "{zone}");
284+ assert!(show[who]["seconds"].as_i64().unwrap() > 1_700_000_000);
285+ let time = show[who]["time"].as_str().unwrap();
286+ assert!(time.ends_with(shown), "{zone}: {time}");
287+ }
288+ // The log shows minutes, in the signature's own offset.
289+ let log = braid_in(dir.path())
290+ .args(["log", id, "-n", "1"])
291+ .output()
292+ .unwrap();
293+ let text = String::from_utf8(log.stdout).unwrap();
294+ let time = text.split(" ").nth(1).unwrap();
295+ assert_eq!(time.len(), "2026-10-15 21:41 +0530".len(), "{text}");
296+ assert!(time.ends_with(shown), "{zone}: {text}");
297+ }
298+}
299+
250300 #[test]
251301 fn commands_outside_a_repository_fail_clearly() {
252302 let dir = tempfile::tempdir().unwrap();
modified crates/repo/Cargo.toml +7 −0
@@ -18,6 +18,13 @@
1818 braid-git.workspace = true
1919 braid-engine.workspace = true
2020 blake3.workspace = true
21+# The local time zone, read without calling into the C library. Only the
22+# system's own time zone database: nothing bundled.
23+jiff = { version = "0.2.38", default-features = false, features = [
24+ "std",
25+ "tz-system",
26+ "tzdb-zoneinfo",
27+] }
2128 thiserror = "2"
2229
2330 [dev-dependencies]
added crates/repo/src/clock.rs +50 −0
@@ -0,0 +1,50 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// Copyright (c) 2026 UtopiaIA
3+
4+//! The time a changeset is made, in the local time zone.
5+//!
6+//! The time zone is the system's, as [jiff](https://docs.rs/jiff) finds it
7+//! without calling into the C library: the `TZ` environment variable, a
8+//! POSIX rule or a name in the system's time zone database, or else the
9+//! system setting (on Unix, `/etc/localtime`). When it cannot be determined,
10+//! the offset is zero.
11+
12+use braid_format::Signature;
13+use jiff::Timestamp;
14+use jiff::tz::TimeZone;
15+
16+/// The current time: seconds since the Unix epoch, and the offset of the
17+/// local time zone from UTC at that instant, in minutes.
18+pub fn local_now() -> (i64, i64) {
19+ let now = Timestamp::now();
20+ let offset =
21+ TimeZone::try_system().map_or(0, |zone| i64::from(zone.to_offset(now).seconds()) / 60);
22+ (now.as_second(), offset)
23+}
24+
25+/// A signature by `name` and `email`, made now, in local time.
26+pub fn signature_now(name: &[u8], email: &[u8]) -> Signature {
27+ let (seconds, offset_minutes) = local_now();
28+ Signature {
29+ name: name.to_vec(),
30+ email: email.to_vec(),
31+ seconds,
32+ offset_minutes,
33+ }
34+}
35+
36+#[cfg(test)]
37+mod tests {
38+ use super::*;
39+
40+ #[test]
41+ fn now_has_a_real_offset() {
42+ let (seconds, offset) = local_now();
43+ assert!(seconds > 1_700_000_000);
44+ // Every time zone in use is within a day of UTC.
45+ assert!(offset.abs() < 24 * 60, "{offset}");
46+ let signature = signature_now(b"Ada", b"ada@example.org");
47+ assert_eq!(signature.name, b"Ada");
48+ assert!(signature.seconds >= seconds);
49+ }
50+}
modified crates/repo/src/lib.rs +2 −0
@@ -20,6 +20,7 @@
2020
2121 #![forbid(unsafe_code)]
2222
23+mod clock;
2324 mod config;
2425 mod git;
2526 mod link;
@@ -35,6 +36,7 @@
3536 use braid_store::{Options, Store};
3637
3738 pub use braid_engine::{Conflict, ConflictHunk, ConflictKind, HunkSide};
39+pub use clock::{local_now, signature_now};
3840 pub use config::{CONFIG_FILE, Config};
3941 pub use git::{
4042 GIT_EXPORT_AGENT, GitImport, MAP_REF, REMOTE_PREFIX, braid_ref_for_git, git_ref_for_braid,
modified crates/repo/src/sync.rs +1 −10
@@ -8,7 +8,6 @@
88 use std::ffi::OsString;
99 use std::fs::{self, File, OpenOptions};
1010 use std::path::{Path, PathBuf};
11-use std::time::{SystemTime, UNIX_EPOCH};
1211
1312 use braid_engine::{Conflict, MergeOutcome, is_ancestor, merge};
1413 use braid_format::{Kind, MemoryObjects, ObjectId, Objects, Signature};
@@ -267,15 +266,7 @@
267266 }
268267 None => format!("{}@example.org", email_name(agent)),
269268 };
270- let seconds = SystemTime::now()
271- .duration_since(UNIX_EPOCH)
272- .map_or(0, |elapsed| i64::try_from(elapsed.as_secs()).unwrap_or(0));
273- Ok(Signature {
274- name: agent.as_bytes().to_vec(),
275- email: email.into_bytes(),
276- seconds,
277- offset_minutes: 0,
278- })
269+ Ok(crate::signature_now(agent.as_bytes(), email.as_bytes()))
279270 }
280271 }
281272
modified crates/wc/src/identity.rs +7 −15
@@ -5,10 +5,9 @@
55 //! `name` and `email` of the repository's `.braid/config` (see
66 //! [`braid_repo::Config`]).
77 //!
8-//! Signatures record the current time with an offset of zero: braid does
9-//! not work out the local time zone yet, so times read as UTC.
10-
11-use std::time::{SystemTime, UNIX_EPOCH};
8+//! Signatures record the current time with the offset of the local time
9+//! zone, or of UTC when it cannot be determined (see
10+//! [`braid_repo::local_now`]).
1211
1312 use braid_format::Signature;
1413 use braid_repo::Repo;
@@ -85,17 +84,9 @@
8584 }
8685 }
8786
88- /// A signature by this identity, now, with an offset of zero.
87+ /// A signature by this identity, now, in local time.
8988 pub fn now(&self) -> Signature {
90- let seconds = SystemTime::now()
91- .duration_since(UNIX_EPOCH)
92- .map_or(0, |since| since.as_secs() as i64);
93- Signature {
94- name: self.name.as_bytes().to_vec(),
95- email: self.email.as_bytes().to_vec(),
96- seconds,
97- offset_minutes: 0,
98- }
89+ braid_repo::signature_now(self.name.as_bytes(), self.email.as_bytes())
9990 }
10091 }
10192
@@ -118,8 +109,9 @@
118109 assert!(Identity::parse(bad).is_err(), "{bad:?}");
119110 }
120111 let signature = ada.now();
121- assert_eq!(signature.offset_minutes, 0);
112+ assert_eq!(signature.email, b"ada@example.org");
122113 assert!(signature.seconds > 1_700_000_000);
114+ assert!(signature.offset_minutes.abs() < 24 * 60);
123115 }
124116
125117 #[test]
modified docs/design.md +4 −2
@@ -145,8 +145,10 @@
145145 create` takes the base, the new contents and the deletions, and can require
146146 what the base holds at each path first, so that an agent never overwrites
147147 an edit it did not see.
148-- Signatures record the time in UTC for now: the local time zone offset is not
149- computed yet.
148+- Signatures record the time with the offset of the local time zone, which
149+ [jiff](https://github.com/BurntSushi/jiff) reads from `TZ` or the system's
150+ setting without calling into the C library; when it cannot be determined,
151+ the offset is zero.
150152
151153 ## Git bridge
152154