From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox v2] time: make RFC3339 format in wasm conform to usual format
Date: Mon, 28 Aug 2023 15:29:32 +0200 [thread overview]
Message-ID: <20230828132932.123123-1-d.csapak@proxmox.com> (raw)
on other targets we print the timestamp without fractional seconds
('.xxxZ'), so we should remove that too on wasm
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
changes from v1:
* adapt thomas suggestions (thanks) to make code shorter and the error better
it's currently not possible to run cargo test in wasm32-unknown-unknown
so any test we write here cannot be executed (would have to be done with
another crate like 'wasm-bindgen-test' which requires a browser
environment to run the tests)
proxmox-time/src/wasm.rs | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/proxmox-time/src/wasm.rs b/proxmox-time/src/wasm.rs
index 04cea7d..3f58b99 100644
--- a/proxmox-time/src/wasm.rs
+++ b/proxmox-time/src/wasm.rs
@@ -14,10 +14,18 @@ pub fn epoch_f64() -> f64 {
pub fn epoch_to_rfc3339_utc(epoch: i64) -> Result<String, Error> {
let js_date = js_sys::Date::new_0();
js_date.set_time((epoch as f64) * 1000.0);
- js_date
+ let mut js_date = js_date
.to_iso_string()
.as_string()
- .ok_or_else(|| format_err!("to_iso_string did not return a string"))
+ .ok_or_else(|| format_err!("to_iso_string did not return a string"))?;
+
+ match js_date.len() {
+ len if len < 24 => bail!("invalid length {len} for rfc3339 string"),
+ len => {
+ js_date.replace_range((len - 5).., "Z"); // replace .xxxZ with Z
+ Ok(js_date)
+ }
+ }
}
/// Convert Unix epoch into RFC3339 local time with TZ
--
2.39.2
next reply other threads:[~2023-08-28 13:29 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-28 13:29 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=20230828132932.123123-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox