all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox] proxmox-time: implement epoch_to_rfc3339 for wasm
Date: Mon, 28 Aug 2023 15:30:21 +0200	[thread overview]
Message-ID: <20230828133021.123959-1-d.csapak@proxmox.com> (raw)

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




             reply	other threads:[~2023-08-28 13:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-28 13:30 Dominik Csapak [this message]
2023-08-30  7:21 ` [pbs-devel] applied: " Dietmar Maurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230828133021.123959-1-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal