[pbs-devel] [PATCH proxmox] proxmox-time: implement epoch_to_rfc3339 for wasm

Dominik Csapak d.csapak at proxmox.com
Mon Aug 28 15:30:21 CEST 2023


we just printed out the UTC version, this implements a localized version

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
noticed that this was missing while fixing the other patch

 proxmox-time/src/wasm.rs | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/proxmox-time/src/wasm.rs b/proxmox-time/src/wasm.rs
index 3f58b99..e379dcb 100644
--- a/proxmox-time/src/wasm.rs
+++ b/proxmox-time/src/wasm.rs
@@ -30,9 +30,30 @@ pub fn epoch_to_rfc3339_utc(epoch: i64) -> Result<String, Error> {
 
 /// Convert Unix epoch into RFC3339 local time with TZ
 pub fn epoch_to_rfc3339(epoch: i64) -> Result<String, Error> {
-    // Note: JS does not provide this, so we need to implement this ourselves.
-    // for now, we simply return UTC instead
-    epoch_to_rfc3339_utc(epoch)
+    let js_date = js_sys::Date::new_0();
+    js_date.set_time((epoch as f64) * 1000.0);
+
+    let y = js_date.get_full_year();
+    let m = js_date.get_month() + 1;
+    let d = js_date.get_date();
+    let h = js_date.get_hours();
+    let min = js_date.get_minutes();
+    let s = js_date.get_seconds();
+
+    let offset = -js_date.get_timezone_offset() as i64;
+
+    let offset = if offset == 0 {
+        "Z".to_string()
+    } else {
+        let offset_hour = (offset / 60).abs();
+        let offset_minute = (offset % 60).abs();
+        let sign = if offset > 0 { "+" } else { "-" };
+        format!("{sign}{offset_hour:0>2}:{offset_minute:0>2}")
+    };
+
+    Ok(format!(
+        "{y:0>4}-{m:0>2}-{d:0>2}T{h:0>2}:{min:0>2}:{s:0>2}{offset}"
+    ))
 }
 
 /// Parse RFC3339 into Unix epoch
-- 
2.39.2





More information about the pbs-devel mailing list