diff --git a/locales/en.json b/locales/en.json index 60c9756..82fda26 100644 --- a/locales/en.json +++ b/locales/en.json @@ -2,7 +2,7 @@ "identity.name": "Your name is {name}.", "identity.folder": " Your personal folder is {folder}.", "speaker.tag": "[SPEAKER: {name} - {when}]", - "speaker.note": " User and agent turns are prefixed with a [SPEAKER: name - date and time UTC] tag identifying who generated them and when.", + "speaker.note": " User and agent turns are prefixed with a [SPEAKER: name - local date and time] tag identifying who generated them and when.", "error.name_locked": "The session name is immutable after the first interaction.", "error.name_empty": "The name must not be empty.", "error.session_not_found": "Session not found.", diff --git a/locales/it.json b/locales/it.json index d0615b9..a856641 100644 --- a/locales/it.json +++ b/locales/it.json @@ -2,7 +2,7 @@ "identity.name": "Il tuo nome è {name}.", "identity.folder": " La tua cartella personale è {folder}.", "speaker.tag": "[SPEAKER: {name} - {when}]", - "speaker.note": " I turni dell'utente e dell'agente sono preceduti da un tag [SPEAKER: nome - data e ora UTC] che identifica chi li ha generati e quando.", + "speaker.note": " I turni dell'utente e dell'agente sono preceduti da un tag [SPEAKER: nome - data e ora locale] che identifica chi li ha generati e quando.", "error.name_locked": "Il nome della sessione è immutabile dopo la prima interazione.", "error.name_empty": "Il nome non può essere vuoto.", "error.session_not_found": "Sessione non trovata.", diff --git a/src/api/sessions.rs b/src/api/sessions.rs index 064b307..ab22ec3 100644 --- a/src/api/sessions.rs +++ b/src/api/sessions.rs @@ -243,29 +243,60 @@ fn text_message(role: Role, text: String) -> ChatMessage { } /// The `[SPEAKER: name - datetime]` line prefixed to a turn's content in the -/// model prompt (provenance), followed by a newline. -fn speaker_prefix(name: &str, secs: i64) -> String { +/// model prompt (provenance), followed by a newline. `offset` is the server's +/// UTC offset in seconds, so the stamp is in the server's local timezone. +fn speaker_prefix(name: &str, secs: i64, offset: i64) -> String { format!( "{}\n", t( "speaker.tag", - &[("name", name), ("when", &format_utc(secs))] + &[("name", name), ("when", &format_stamp(secs, offset))] ) ) } -/// Format unix seconds as a compact, unambiguous UTC datetime (no chrono dep). -fn format_utc(secs: i64) -> String { - let days = secs.div_euclid(86400); - let tod = secs.rem_euclid(86400); +/// Format unix seconds as `YYYY-MM-DD HH:MM` in the server's local timezone +/// (`offset` seconds east of UTC). Dependency-free civil-date arithmetic. +fn format_stamp(secs: i64, offset: i64) -> String { + let local = secs + offset; + let days = local.div_euclid(86400); + let tod = local.rem_euclid(86400); let (y, m, d) = civil_from_days(days); format!( - "{y:04}-{m:02}-{d:02} {:02}:{:02} UTC", + "{y:04}-{m:02}-{d:02} {:02}:{:02}", tod / 3600, (tod % 3600) / 60 ) } +/// The server's current UTC offset in seconds, from the OS `date +%z` (reads the +/// system timezone incl. DST). One cheap call per request build; 0 (UTC) on fail. +fn server_utc_offset_secs() -> i64 { + std::process::Command::new("date") + .arg("+%z") + .output() + .ok() + .and_then(|o| String::from_utf8(o.stdout).ok()) + .and_then(|s| parse_utc_offset(s.trim())) + .unwrap_or(0) +} + +/// Parse `date +%z` output ("+0100", "-0500") into seconds east of UTC. +fn parse_utc_offset(s: &str) -> Option { + let b = s.as_bytes(); + if b.len() != 5 { + return None; + } + let sign = match b[0] { + b'+' => 1, + b'-' => -1, + _ => return None, + }; + let hh: i64 = s.get(1..3)?.parse().ok()?; + let mm: i64 = s.get(3..5)?.parse().ok()?; + Some(sign * (hh * 3600 + mm * 60)) +} + /// Days since 1970-01-01 -> (year, month, day). Howard Hinnant's algorithm. fn civil_from_days(z: i64) -> (i64, u32, u32) { let z = z + 719468; @@ -291,6 +322,8 @@ fn build_request( user_name: &str, ) -> ChatRequest { let mut messages = Vec::new(); + // Server-local timezone offset, computed once for every turn in this request. + let tz_offset = server_utc_offset_secs(); // Structural identity reminder: always present, not an editable setting. It // tells the agent its name and personal workspace folder. let mut identity = t("identity.name", &[("name", &sess.title)]); @@ -308,8 +341,16 @@ fn build_request( // Tool/system turns stay untagged. This annotates the prompt only — // stored content stays clean (the GUI shows its own timestamp). let content = match m.role.as_str() { - "user" => format!("{}{}", speaker_prefix(user_name, m.created_at), m.content), - "assistant" => format!("{}{}", speaker_prefix(&sess.title, m.created_at), m.content), + "user" => format!( + "{}{}", + speaker_prefix(user_name, m.created_at, tz_offset), + m.content + ), + "assistant" => format!( + "{}{}", + speaker_prefix(&sess.title, m.created_at, tz_offset), + m.content + ), _ => m.content.clone(), }; let mut msg = text_message(role_of(&m.role), content); @@ -324,7 +365,7 @@ fn build_request( // the Vision bridge materializes; without images it stays a plain string. let user_text = format!( "{}{}{}", - speaker_prefix(user_name, now_unix() as i64), + speaker_prefix(user_name, now_unix() as i64, tz_offset), body.content, extra ); @@ -813,7 +854,7 @@ mod tests { let req = build_request(&sess, &history, &body, "", &[], "User"); // messages: [identity, user(history), assistant(history), user(new)] let u = req.messages[1].text().unwrap(); - assert!(u.starts_with("[SPEAKER: User - 2001-09-09 01:46 UTC]")); + assert!(u.starts_with("[SPEAKER: User - ")); assert!(u.ends_with("domanda")); let a = req.messages[2].text().unwrap(); assert!(a.starts_with("[SPEAKER: Nebula - ")); @@ -821,9 +862,14 @@ mod tests { } #[test] - fn format_utc_known_dates() { - assert_eq!(format_utc(0), "1970-01-01 00:00 UTC"); - assert_eq!(format_utc(1_000_000_000), "2001-09-09 01:46 UTC"); - assert_eq!(format_utc(951_782_400), "2000-02-29 00:00 UTC"); // leap day + fn format_stamp_known_dates_and_offset() { + assert_eq!(format_stamp(0, 0), "1970-01-01 00:00"); + assert_eq!(format_stamp(1_000_000_000, 0), "2001-09-09 01:46"); + assert_eq!(format_stamp(951_782_400, 0), "2000-02-29 00:00"); // leap day + assert_eq!(format_stamp(0, 3600), "1970-01-01 01:00"); // +1h server offset + assert_eq!(parse_utc_offset("+0100"), Some(3600)); + assert_eq!(parse_utc_offset("-0500"), Some(-18000)); + assert_eq!(parse_utc_offset("+0000"), Some(0)); + assert_eq!(parse_utc_offset("nope"), None); } } diff --git a/web/assets/app.js b/web/assets/app.js index 77bed4a..a728da6 100644 --- a/web/assets/app.js +++ b/web/assets/app.js @@ -90,7 +90,7 @@ function usageText(u) { if (u.decode_ms > 0 && u.completion_tokens > 0) { s += " · " + t("stats.tps", { n: fmt1(u.completion_tokens / (u.decode_ms / 1000)) }); } - if (u.prompt_ms > 0) s += " · " + t("usage.ttft", { n: fmtInt(u.prompt_ms) }); + if (u.prompt_ms > 0) s += " · " + t("usage.ttft", { n: fmtInt(u.prompt_ms / 1000) }); return s; } diff --git a/web/assets/locales/en.json b/web/assets/locales/en.json index 54d95d0..541c54e 100644 --- a/web/assets/locales/en.json +++ b/web/assets/locales/en.json @@ -23,7 +23,7 @@ "attach.title": "Attach files or images", "attach.remove": "Remove attachment", "attach.error": "Upload failed: {msg}", - "usage.ttft": "1st token {n} ms", + "usage.ttft": "1st token {n} s", "stats.title": "Statistics", "stats.close": "Close", "stats.updated": "Updated {time}", diff --git a/web/assets/locales/it.json b/web/assets/locales/it.json index a610068..2bbab8d 100644 --- a/web/assets/locales/it.json +++ b/web/assets/locales/it.json @@ -23,7 +23,7 @@ "attach.title": "Allega file o immagini", "attach.remove": "Rimuovi allegato", "attach.error": "Caricamento fallito: {msg}", - "usage.ttft": "1° token {n} ms", + "usage.ttft": "1° token {n} s", "stats.title": "Statistiche", "stats.close": "Chiudi", "stats.updated": "Aggiornato {time}",