diff --git a/proxmox-time/src/wasm.rs b/proxmox-time/src/wasm.rs index 74e2060e..4f268dce 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 { /// Convert Unix epoch into RFC3339 local time with TZ pub fn epoch_to_rfc3339(epoch: i64) -> Result { - // 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