public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox] proxmox-time: implement epoch_to_rfc3339 for wasm
@ 2023-08-28 13:30 Dominik Csapak
  2023-08-30  7:21 ` [pbs-devel] applied: " Dietmar Maurer
  0 siblings, 1 reply; 2+ messages in thread
From: Dominik Csapak @ 2023-08-28 13:30 UTC (permalink / raw)
  To: pbs-devel

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

Signed-off-by: Dominik Csapak <d.csapak@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




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-08-30  7:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-28 13:30 [pbs-devel] [PATCH proxmox] proxmox-time: implement epoch_to_rfc3339 for wasm Dominik Csapak
2023-08-30  7:21 ` [pbs-devel] applied: " Dietmar Maurer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal