public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox] time: make RFC3339 format in wasm conform to usual format
@ 2023-08-24 14:11 Dominik Csapak
  2023-08-28 11:30 ` Thomas Lamprecht
  0 siblings, 1 reply; 2+ messages in thread
From: Dominik Csapak @ 2023-08-24 14:11 UTC (permalink / raw)
  To: pbs-devel

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>
---
 proxmox-time/src/wasm.rs | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/proxmox-time/src/wasm.rs b/proxmox-time/src/wasm.rs
index 04cea7d..59c26e2 100644
--- a/proxmox-time/src/wasm.rs
+++ b/proxmox-time/src/wasm.rs
@@ -14,10 +14,19 @@ 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"))?;
+    let len = js_date.len();
+    if len < 24 {
+        bail!("invalid rfc3339 string");
+    }
+
+    // replace .xxxZ with Z
+    js_date.replace_range((len - 5).., "Z");
+
+    Ok(js_date)
 }
 
 /// Convert Unix epoch into RFC3339 local time with TZ
-- 
2.30.2





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

* Re: [pbs-devel] [PATCH proxmox] time: make RFC3339 format in wasm conform to usual format
  2023-08-24 14:11 [pbs-devel] [PATCH proxmox] time: make RFC3339 format in wasm conform to usual format Dominik Csapak
@ 2023-08-28 11:30 ` Thomas Lamprecht
  0 siblings, 0 replies; 2+ messages in thread
From: Thomas Lamprecht @ 2023-08-28 11:30 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Dominik Csapak

Am 24/08/2023 um 16:11 schrieb Dominik Csapak:
> on other targets we print the timestamp without fractional seconds

albeit I quite often like to know the millisecond difference between, e.g.,
log messages, but that's another topic and I definitively agree that it's
good to output the same for both.

> ('.xxxZ'), so we should remove that too on wasm

would be relative simple and good to add a unit test for this, could you please
do so?

> 
> Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
> ---
>  proxmox-time/src/wasm.rs | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/proxmox-time/src/wasm.rs b/proxmox-time/src/wasm.rs
> index 04cea7d..59c26e2 100644
> --- a/proxmox-time/src/wasm.rs
> +++ b/proxmox-time/src/wasm.rs
> @@ -14,10 +14,19 @@ 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"))?;
> +    let len = js_date.len();
> +    if len < 24 {
> +        bail!("invalid rfc3339 string");
> +    }
> +
> +    // replace .xxxZ with Z
> +    js_date.replace_range((len - 5).., "Z");
> +
> +    Ok(js_date)

Hmm, deep in the nitpick territory, but the following (untested) seems slightly
nicer, shows the actual length in the error message when asserting that it got the
minimum length and matches make reasoning often (here: very) slightly easier.

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





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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-24 14:11 [pbs-devel] [PATCH proxmox] time: make RFC3339 format in wasm conform to usual format Dominik Csapak
2023-08-28 11:30 ` Thomas Lamprecht

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