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

Utc.timestamp method is unintuitive #832

Open
kornelski opened this issue Oct 2, 2022 · 7 comments
Open

Utc.timestamp method is unintuitive #832

kornelski opened this issue Oct 2, 2022 · 7 comments

Comments

@kornelski
Copy link

I've been looking for a way to get DateTime<Utc> from a unix timestamp, and fudged this monstrosity:

DateTime::from_utc(NaiveDateTime::from_timestamp(timestamp as _, 0), Utc)

only to discover later that Utc.timestamp() exists too. But this method was hard to find:

  • It's called timestamp, not from_timestamp like the other conversion methods.
  • It's an instance method, not a static method Utc::from_timestamp like most constructors in Rust.
  • It's naming suggests it returns a timestamp (like a getter), but it returns DateTime.
@esheppa
Copy link
Collaborator

esheppa commented Oct 2, 2022

Thanks for raising this @kornelski. Some of the solutions I've suggested in #831 probably apply here as well, but its worth going through a few of these points to explain why the current API works the way it does:

It's called timestamp, not from_timestamp like the other conversion methods.

This is likely because all these methods take self by reference, and it requires silencing a clippy lint to prefix the method with from_ - however making these all consistent does seem to be an improvement.

It's an instance method, not a static method Utc::from_timestamp like most constructors in Rust.

While this may seem superfluous when using Utc, any TimeZone implementer that maintains internal state, eg FixedOffset, requires the reference to self to get at that state

It's naming suggests it returns a timestamp (like a getter), but it returns DateTime.

Yes it does seem that this could be better named - I'm open to any suggestions you may have, some thoughts are:

  • (as an inherent method on impl DateTime<Utc>) - fn from_timestamp(secs: i64, nsecs: u32) -> DateTime<Utc>
  • rename TimeZone method: fn create_from_timestamp(&self, secs: i64, nsecs: u32) -> DateTime<Self>
  • rename TimeZone method: fn datetime_from_timestamp(&self, secs: i64, nsecs: u32) -> DateTime<Self>
  • rename TimeZone method: fn from_timestamp(&self, secs: i64, nsecs: u32) -> DateTime<Self> (has the disadvantage of being against the convention of taking self by value when prefixed with from)

My overall thinking here is that there is a decent usability improvement by making the traits as minimal as possible and moving as much as possible to inherent methods on the types like DateTime, Utc, etc.

@kornelski
Copy link
Author

kornelski commented Oct 2, 2022

Thank you for your response.

Utc.datetime_from_timestamp would be nice. The datetime there is to signify it doesn't create Utc, and it would also allow date_from_timestamp.

But having these methods on Utc singleton is odd. When I need a DateTime, I look for DateTime::from* methods, which is why I've stumbled into DateTime::from_utc in the first place. So DateTime::from_timestamp would be ideal.

@djc
Copy link
Contributor

djc commented Oct 3, 2022

I think it makes sense to add DateTime::from_timestamp() for this, and ignore any clippy complaints that come up.

@demurgos
Copy link
Contributor

demurgos commented Mar 22, 2023

I definitely agree with this issue. When searching for a conversion function, I scanned the documentation for new and from_* functions. It's easy to overlook the timestamp function. The suggestion by @djc to use from_timestamp would be very helpful.

When searching for exising issues in this repo, I also found that this issue was already raised a couple of times before:

@djc
Copy link
Contributor

djc commented Mar 23, 2023

@demurgos want to send a PR?

demurgos added a commit to demurgos/chrono that referenced this issue Sep 9, 2023
This commit adds the new constructor `from_timestamp_opt` to build a `DateTime<Utc>` from a UNIX timestamp.

Figuring out how to convert a timestamp into a `DateTime<Utc>` was a common issue:
- chronotope#88
- chronotope#200
- chronotope#832

This commit should make `DateTime<Utc>` creation more discoverable and intuitive.

This commit respects the current convention of using the `_opt` suffix for fallible functions. As panicking variants on invalid input are deprecated, no panicking variant is provided. See [this issue](chronotope#815) for discussion about error handling and panics.

Closes chronotope#832
demurgos added a commit to demurgos/chrono that referenced this issue Sep 9, 2023
This commit adds the new constructor `from_timestamp_opt` to build a `DateTime<Utc>` from a UNIX timestamp.

Figuring out how to convert a timestamp into a `DateTime<Utc>` was a common issue:
- chronotope#88
- chronotope#200
- chronotope#832

This commit should make `DateTime<Utc>` creation more discoverable and intuitive.

This commit respects the current convention of using the `_opt` suffix for fallible functions. As panicking variants on invalid input are deprecated, no panicking variant is provided. See [this issue](chronotope#815) for discussion about error handling and panics.

Closes chronotope#832
@demurgos
Copy link
Contributor

demurgos commented Sep 9, 2023

Sure, here it is: #1278 #1279

demurgos added a commit to demurgos/chrono that referenced this issue Sep 9, 2023
This commit adds the new constructor `from_timestamp_opt` to build a `DateTime<Utc>` from a UNIX timestamp.

Figuring out how to convert a timestamp into a `DateTime<Utc>` was a common issue:
- chronotope#88
- chronotope#200
- chronotope#832

This commit should make `DateTime<Utc>` creation more discoverable and intuitive.

This commit respects the current convention of using the `_opt` suffix for fallible functions. As panicking variants on invalid input are deprecated, no panicking variant is provided. See [this issue](chronotope#815) for discussion about error handling and panics.

Closes chronotope#832
demurgos added a commit to demurgos/chrono that referenced this issue Sep 9, 2023
This commit adds the new constructor `from_timestamp_opt` to build a `DateTime<Utc>` from a UNIX timestamp.

Figuring out how to convert a timestamp into a `DateTime<Utc>` was a common issue:
- chronotope#88
- chronotope#200
- chronotope#832

This commit should make `DateTime<Utc>` creation more discoverable and intuitive.

This commit respects the current convention of using the `_opt` suffix for fallible functions. As panicking variants on invalid input are deprecated, no panicking variant is provided. See [this issue](chronotope#815) for discussion about error handling and panics.

Closes chronotope#832
demurgos added a commit to demurgos/chrono that referenced this issue Sep 9, 2023
This commit adds the new constructor `from_timestamp_opt` to build a `DateTime<Utc>` from a UNIX timestamp.

Figuring out how to convert a timestamp into a `DateTime<Utc>` was a common issue:
- chronotope#88
- chronotope#200
- chronotope#832

This commit should make `DateTime<Utc>` creation more discoverable and intuitive.

This commit respects the current convention of using the `_opt` suffix for fallible functions. As panicking variants on invalid input are deprecated, no panicking variant is provided. See [this issue](chronotope#815) for discussion about error handling and panics.

Closes chronotope#832
demurgos added a commit to demurgos/chrono that referenced this issue Sep 10, 2023
This commit adds the new constructor `from_timestamp_opt` to build a `DateTime<Utc>` from a UNIX timestamp.

Figuring out how to convert a timestamp into a `DateTime<Utc>` was a common issue:
- chronotope#88
- chronotope#200
- chronotope#832

This commit should make `DateTime<Utc>` creation more discoverable and intuitive.

This commit respects the current convention of using the `_opt` suffix for fallible functions. As panicking variants on invalid input are deprecated, no panicking variant is provided. See [this issue](chronotope#815) for discussion about error handling and panics.

Closes chronotope#832
demurgos added a commit to demurgos/chrono that referenced this issue Sep 10, 2023
This commit adds the new constructor `from_timestamp_opt` to build a `DateTime<Utc>` from a UNIX timestamp.

Figuring out how to convert a timestamp into a `DateTime<Utc>` was a common issue:
- chronotope#88
- chronotope#200
- chronotope#832

This commit should make `DateTime<Utc>` creation more discoverable and intuitive.

This commit respects the current convention of using the `_opt` suffix for fallible functions. As panicking variants on invalid input are deprecated, no panicking variant is provided. See [this issue](chronotope#815) for discussion about error handling and panics.

Closes chronotope#832
demurgos added a commit to demurgos/chrono that referenced this issue Sep 10, 2023
This commit adds the new constructor `from_timestamp` to build a `DateTime<Utc>` from a UNIX timestamp.

Figuring out how to convert a timestamp into a `DateTime<Utc>` was a common issue:
- chronotope#88
- chronotope#200
- chronotope#832

This commit should make `DateTime<Utc>` creation more discoverable and intuitive.

This commit respects the current convention of preferring fallible functions. It avoids however the `_opt` suffix as there is no panicking variant. See [this issue](chronotope#815) for discussion about error handling and panics.

Closes chronotope#832
demurgos added a commit to demurgos/chrono that referenced this issue Sep 10, 2023
This commit adds the new constructor `from_timestamp` to build a `DateTime<Utc>` from a UNIX timestamp.

Figuring out how to convert a timestamp into a `DateTime<Utc>` was a common issue:
- chronotope#88
- chronotope#200
- chronotope#832

This commit should make `DateTime<Utc>` creation more discoverable and intuitive.

This commit respects the current convention of preferring fallible functions. It avoids however the `_opt` suffix as there is no panicking variant. See [this issue](chronotope#815) for discussion about error handling and panics.

Closes chronotope#832
@pitdicker
Copy link
Collaborator

#1279 added DateTime::<Utc>::from_timestamp, which partly fixes this issue.

I would like to keep this open to track renaming TimeZone::timestamp.

@pitdicker pitdicker reopened this Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants