Does SystemTime work for browser?

I'm trying to create a filesystem API that under the hood uses std::{fs, path, env}. Futurely, for browser support, it'll use IndexedDB for special app: and app-storage: URLs.

I've to provide a way to get the creation_date() and modification_date() of my own File object:

#[derive(Clone, Eq, PartialEq)]
pub struct File {
    m_path: String,
    m_scheme: FileScheme, // file:, app:, app-storage:
}

I see that std::fs::Metadata (Metadata in std::fs - Rust) provides created and modified fields, but they return SystemTime. Can SystemTime be constructed when using IndexedDB (no plans yet) instead of std::{fs, env, path} and instead of Metadata? Is there a way to convert SystemTime to a zoned-date-time date?

The datetime crate provides ZonedDateTime. How would I translate a SystemTime into that?

If you compile to wasm32-unknown-unknown, the standard library assumes nothing about the system it runs on.

On WebAssembly, the SystemTime::now() constructor looks like this:

However, if you have some other means of knowing the current time (i.e. Date.now() from JavaScript), you can use SystemTime::EPOCH and SystemTime::checked_add() to create your timestamp using math.

I'd suggest creating your own version of std::fs::Metadata with the relevant fields. That way you can construct it yourself and the timestamps aren't required to come from the OS.

1 Like

You mean something like SystemTime::UNIX_EPOCH.checked_add(indexeddb_retrieved_creation_duration)? So it seems like I can use SystemTime then.

Yep. That's what I was thinking.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.