Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove chrono from the RTC API #3185

Closed
robertbastian opened this issue Feb 27, 2025 · 11 comments
Closed

Remove chrono from the RTC API #3185

robertbastian opened this issue Feb 27, 2025 · 11 comments

Comments

@robertbastian
Copy link
Contributor

Motivations

The Rtc::set_current_time and Rtc::current_time methods use chrono::NaiveDateTime. This is not a timestamp type, it is a civil time type, so it exposes concepts like "day" in a place where these concepts should not exist. chrono uses DateTime<Utc> as its timestamp type, but even that is flawed because you can still ask it which day it is (you get the response for London).

Unfortunately Rust lacks a standard library timestamp type, but I don't think esp-hal should pull in chrono just to use a single (flawed) type. I'm trying to migrate my chrono usage to jiff at the moment (which does have a good Timestamp type), but I have to keep chrono around to use the RTC.

Solution

Use an i64 of nanoseconds since the UNIX epoch instead of NaiveDateTime, so that users can choose their own datetime library.

Alternatives

  • Use jiff::Timestamp

  • Use chrono::DateTime<Utc>, which is chrono's kinda-timestamp time (it allows converting into UNIX seconds).

Additional context

@robertbastian robertbastian added the status:needs-attention This should be prioritized label Feb 27, 2025
@MabezDev
Copy link
Member

MabezDev commented Mar 3, 2025

This is not a timestamp type

I disagree here, a type can be a timestamp, and also understand "civil" time concepts. chrono::DateTime<Utc> itself, is just a chrono::NaiveDateTime with an extra field to store the time zone offset. Imo the NaiveDateTime makes sense here, and do note that it can be converted to i64 UNIX Epoch too: https://docs.rs/chrono/latest/chrono/struct.NaiveDateTime.html#method.timestamp which you can use to convert the the type of your choosing.

What are you actually trying to do? It sounds more like you want to compare Instant's to get Duration's, in which case we have the time module for this.

@MabezDev MabezDev removed the status:needs-attention This should be prioritized label Mar 3, 2025
@robertbastian
Copy link
Contributor Author

I disagree here, a type can be a timestamp, and also understand "civil" time concepts.

Yes, that's chrono's DateTime type. All call sites of this need to immediately call .and_utc() to be able to do anything with it. So why not return DateTime<Utc> directly?

Imo the NaiveDateTime makes sense here, and do note that it can be converted to i64 UNIX Epoch too

This method is deprecated for the exact reasons I'm lining out here.

@MabezDev
Copy link
Member

MabezDev commented Mar 3, 2025

Ah sorry I missed the deprecation notice. In which case, I think we'd accept a PR changing the type to chrono::DateTime<Utc>. jiff looks interesting (and simpler), but I'd rather not depend on more <1.0 public dependencies.

@robertbastian
Copy link
Contributor Author

I think ideally this would just return an i64, and not be opinionated about which datetime library to use.

@robertbastian
Copy link
Contributor Author

Also, jiff is aiming for a 1.0 release this summer, which is probably before this component stabilises.

@MabezDev
Copy link
Member

MabezDev commented Mar 3, 2025

I think ideally this would just return an i64, and not be opinionated about which datetime library to use.

I'm fine with adding a _raw API that just returns a i64, but I'd like there to still be a way to get a time date type.

Also, jiff is aiming for a 1.0 release this summer, which is probably before this component stabilises.

Do you have any source for that? I just took a closer look at jiff, and it's maintainer, which gives me a bit more hope for the library. I think if we have a source on a planned jiff 1.0 release, I'd be happy to try it out in esp-hal. I agree that it's likely going to be a while before we stabilize Rtc, so it seems like a good place to try it out.

@MabezDev
Copy link
Member

MabezDev commented Mar 3, 2025

Thanks! I think we'd accept a PR to use jiff instead. Would you mind opening one?

@BurntSushi
Copy link

As the author of Jiff, and looking at #3200, I'm with @robertbastian here that just returning a Unix timestamp as a i64 to nanosecond precision (assuming you don't care about timestamps outside of 1677-09-21T00:12:43.145224192Z to 2262-04-11T23:47:16.854775807Z) is probably the right call here. The API surface is so tiny that bringing in a public dependency for it seems like it's probably not worth it. You can always suggest using Jiff and even provide a doc example doing so by depending on Jiff via a dev-dependency.

@robertbastian
Copy link
Contributor Author

Probably should be u64 actually, as that's what the RTC registers store, and we don't need to support setting the time to before the UNIX epoch.

@JurajSadel
Copy link
Contributor

I believe this is resolved by #3200, closing.

@github-project-automation github-project-automation bot moved this from Todo to Done in esp-rs Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

4 participants