Skip to content

Commit

Permalink
Added Driver::utc_now and Driver::timezone_offset
Browse files Browse the repository at this point in the history
  • Loading branch information
sfisol committed Aug 19, 2024
1 parent 0e9d993 commit 2bbf4d9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
<!-- markdownlint-disable-next-line first-line-h1 -->
## Unreleased

### Added

* `Driver::utc_now` (Gets current UTC timestamp)
* `Driver::timezone_offset` (Gets browsers time zone offset in seconds)

### Changed

* Hush excessive logging when no Content-Type or cookie provided
Expand Down
42 changes: 30 additions & 12 deletions crates/vertigo/src/driver_module/api/api_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl ApiImport {

if result != JsValue::Null {
if let JsValue::Json(value) = result {
return value
return value;
}
log::error!("cookie_get_json -> params decode error -> result={result:?}");
}
Expand Down Expand Up @@ -264,6 +264,10 @@ impl ApiImport {
}

pub fn instant_now(&self) -> InstantType {
self.utc_now() as InstantType
}

pub fn utc_now(&self) -> i64 {
let result = self
.dom_access()
.root("window")
Expand All @@ -272,20 +276,35 @@ impl ApiImport {
.fetch();

match result {
JsValue::I64(time) => {
time as u64 as InstantType
}
JsValue::F64(time) => {
time as u64 as InstantType
}
JsValue::I64(time) => time,
JsValue::F64(time) => time as i64,
_ => {
self.panic_message
.show(format!("api.instant_now -> incorrect result {result:?}"));
0_u64
.show(format!("api.utc_now -> incorrect result {result:?}"));
0_i64
}
}
}

pub fn timezone_offset(&self) -> i32 {
let result = self
.dom_access()
.api()
.call("getTimezoneOffset", vec![])
.fetch();

if let JsValue::I32(result) = result {
// Return in seconds to be compatible with chrono
// Opposite as JS returns the offset backwards
result * -60
} else {
self.panic_message.show(format!(
"api.timezone_offset -> incorrect result {result:?}"
));
0
}
}

pub fn history_back(&self) {
self.dom_access()
.root("window")
Expand Down Expand Up @@ -679,11 +698,10 @@ impl ApiImport {
/// Synthetic command to respond with plain text, not DOM
pub fn plain_response(&self, body: String) {
if self.is_browser() {
return
return;
}

self
.dom_access()
self.dom_access()
.synthetic("plain_response", JsValue::String(body))
.exec();
}
Expand Down
12 changes: 12 additions & 0 deletions crates/vertigo/src/driver_module/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ impl Driver {
Instant::now(self.inner.api.clone())
}

/// Gets current UTC timestamp
pub fn utc_now(&self) -> i64 {
self.inner.api.utc_now()
}

/// Gets browsers time zone offset in seconds
///
/// Compatible with chrono's `FixedOffset::east_opt` method.
pub fn timezone_offset(&self) -> i32 {
self.inner.api.timezone_offset()
}

/// Create new RequestBuilder for GETs (more complex version of [fetch](struct.Driver.html#method.fetch))
#[must_use]
pub fn request_get(&self, url: impl Into<String>) -> RequestBuilder {
Expand Down
4 changes: 4 additions & 0 deletions crates/vertigo/src/driver_module/src_js/api_browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ export class ApiBrowser {
public get_env = (name: string): string | null => {
return document.documentElement.getAttribute(`data-env-${name}`);
}

public getTimezoneOffset = (): number => {
return new Date().getTimezoneOffset()
}
}
2 changes: 1 addition & 1 deletion crates/vertigo/src/driver_module/wasm_run.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/vertigo/src/driver_module/wasm_run.js.map

Large diffs are not rendered by default.

0 comments on commit 2bbf4d9

Please sign in to comment.