Seed: wasm-friendly Datetime utility crate

Created on 11 May 2019  路  4Comments  路  Source: seed-rs/seed

I'm considering adding a crate that wraps Chrono, but includes functionality it doesn't include on the wasm-target, like getting the current date/time/dt, via js_sys, and provides a cleaner API. Thoughts?

Caveats:

  • This is more suited to Gloo, but perhaps here is a better place to start, due to Gloo's formal update process, and since it's not-yet released. The API wrapper will be opinionated.

  • While getting the current date/time etc is common, it may not be worth wrapping a non-web crate just for this functionality, and api improvements are out of this project's scope. I'm proposing this anyway because a clean dt api that can get current info is common in web-programming, and this project takes a batteries-included approach: I don't want users to have to hunt around and use binder code.

@Pauan @fitzgen

Most helpful comment

Is there any reason this can't be added to chrono? Have they rejected the idea of having a web submodule which is enabled by a feature?

All 4 comments

Is there any reason this can't be added to chrono? Have they rejected the idea of having a web submodule which is enabled by a feature?

That might be the best approach. Not rejected, but there's a related issue and PR that's been idle for a bit. Worth pursuing this route.

I'm still concerned about the verbosity of the api; ie I don't want users to have to write code like this wrapper:

    /// Convert an iso-format date, eg "2019-01-05" for January 5, 2019" to a Date.
    pub fn from_iso(date: &str) -> Self {
        // Chrono needs time too; and can't parse date-only directly.
        let padded = &(date.to_string() + "T000000");
        Self {
            wrapped: chrono::Utc
                .datetime_from_str(padded, "%Y-%m-%dT%H%M%S")
                .expect(&format!("Can't format date: {}", date))
                .date()
        }
    }

Although this could also be a PR for Chrono.

I would expect a function called from_iso to fully support all of ISO, not just dates. So that seems like an ad-hoc function specialized to a particular app's needs.

Aside from that, is chrono::DateTime::parse_from_rfc3339(date).unwrap_throw().date() too verbose? It seems pretty straightforward to me, personally.

And similarly, a chrono::Date::parse_from_rfc3339(date).unwrap_throw() could be used for date only (though that needs a PR to add it to chrono).

Closing this for now - until I find the scope of limitations of Chrono in WASM, and pursue PRs there.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bgourlie picture bgourlie  路  4Comments

JWorthe picture JWorthe  路  3Comments

jgrund picture jgrund  路  6Comments

MartinKavik picture MartinKavik  路  3Comments

MartinKavik picture MartinKavik  路  4Comments