From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id C04E69CE9
 for <pbs-devel@lists.proxmox.com>; Mon, 28 Aug 2023 15:30:52 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id A1E861DE29
 for <pbs-devel@lists.proxmox.com>; Mon, 28 Aug 2023 15:30:22 +0200 (CEST)
Received: from bookworm-dev.localdomain (unknown [94.136.29.99])
 by firstgate.proxmox.com (Proxmox) with ESMTP
 for <pbs-devel@lists.proxmox.com>; Mon, 28 Aug 2023 15:30:21 +0200 (CEST)
Received: by bookworm-dev.localdomain (Postfix, from userid 1000)
 id 20ED06027B; Mon, 28 Aug 2023 15:30:21 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Mon, 28 Aug 2023 15:30:21 +0200
Message-Id: <20230828133021.123959-1-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.39.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.386 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RDNS_NONE 0.793 Delivered to internal network by a host with no rDNS
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pbs-devel] [PATCH proxmox] proxmox-time: implement
 epoch_to_rfc3339 for wasm
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Mon, 28 Aug 2023 13:30:52 -0000

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