proxmox-time: implement epoch_to_rfc3339 for wasm

we just printed out the UTC version, this implements a localized version

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2023-08-28 15:30:21 +02:00 committed by Dietmar Maurer
parent 151e2cfdfd
commit 299a478f15

View File

@ -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