chiark / gitweb /
some timezones shown
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 16 Oct 2020 23:04:14 +0000 (00:04 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 16 Oct 2020 23:04:14 +0000 (00:04 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
demo/test.table.toml
src/gamestate.rs
src/tz.rs

index 3f657804b356ae5dcbd7920387eeae78c1909843..b67890d8650b6bdc67c5a9b088463370d5a3d0a4 100644 (file)
@@ -2,6 +2,8 @@
 # SPDX-License-Identifier: AGPL-3.0-or-later
 # There is NO WARRANTY.
 
+timezone = "Europe/London"
+
 [[players]]
 nick = "alice"
 access = "FixedToken"
index 7e296629d57271e0a87a2200bc89ea7ff80c2d5b..233496de2150183109d765ff7358e2782cb10c94 100644 (file)
@@ -171,9 +171,7 @@ impl Timestamp {
   }
 
   pub fn render(&self, tz: &Timezone) -> String {
-    let mut s = String::with_capacity(30);
-    tz.format(*self, &mut s).unwrap();
-    s
+    tz.format(*self)
   }
 }
 
index 39f958cf646f246e9c43ed951d9538c83d34990c..863b31557608ff684d945dd463ea633c3cf2ae60 100644 (file)
--- a/src/tz.rs
+++ b/src/tz.rs
@@ -21,10 +21,35 @@ impl Timezone {
   pub fn name(&self) -> &str {
     &self.0.name
   }
-  #[throws(fmt::Error)]
-  pub fn format<W: fmt::Write>(&self, ts: Timestamp, w: &mut W) {
-    write!(w, "TS{:?}(@{:?})", ts, &self)?
-//        let ctz = chrono::offset::Utc;
+
+  // Oh my god this API is awful!
+
+  #[throws(as Option)]
+  fn format_tz<'r, TZ: chrono::TimeZone>(
+    tz: &TZ, ts: Timestamp, fmt: &'r str
+  ) -> chrono::format::DelayedFormat<chrono::format::StrftimeItems<'r>>
+    where <TZ as chrono::TimeZone>::Offset : Display
+  {
+    use chrono::DateTime;
+    let dt = tz.timestamp_opt(ts.0.try_into().ok()?, 0).single()?;
+    DateTime::format(&dt, fmt)
+  }
+
+  pub fn format(&self, ts: Timestamp) -> String {
+    match (||{
+      let fmt = "%Y-%m-%d %H:%M:%S %z";
+      let df = match &self.0.ctz {
+        Some(ctz) => Timezone::format_tz(ctz,                  ts, fmt),
+        None      => Timezone::format_tz(&chrono::offset::Utc, ts, fmt),
+      }
+      .ok_or_else(
+        || format!("timestamp {} out of range!", &ts.0)
+      )?;
+      Ok(format!("{}", df))
+    })() {
+      Ok(s) => s,
+      Err(s) => s,
+    }
   }
 
   pub fn default_todo() -> Self {