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

Converting a timestamp to local time is verbose and unintuitive #200

Closed
johnomalley opened this issue Jan 6, 2018 · 2 comments
Closed

Comments

@johnomalley
Copy link

My (simplified) use case - build a vector of file metadata (name + modified timestamp), then sort by most recent and output a name + formatted timestamp for each one in local time. This is a trivial problem in Java, C#, JavaScript, etc. As far as I can tell this is what I need to do in chrono:

fn format_time(modified: i64) -> String {
    DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(modified, 0), Utc).with_timezone(&Local).format("%Y-%m-%d %H:%M:%S").to_string()
}

This took me a few hours to figure out. Wow! I thought Java APIs were verbose. Maybe there's an easier way but it's not obvious from the docs.

The docs refer to Local.from_timestamp(...) but that does not compile (apologies if I'm missing something as I'm new to Rust).

@quodlibetor
Copy link
Contributor

It is sadly the case that our from_* methods are poorly named and don't include from_* at all. The are all implemented on the TimeZone trait which is part of the chrono prelude.

But for example this works:

extern crate chrono;

use chrono::prelude::*;

fn main() {
    println!("{}", Local.timestamp(90, 0));
}

and outputs: 1970-01-01 00:01:30 +00:00.

In fact, the only places in the chrono docs that I can find that use the extremely verbose construction that you used (on DateTime) immediately gives the equivalent shorter constructor.

I'm going to add an issue to create from_* methods (and deprecate the existing constructors) on timezone, because the current situation is extremely confusing.

@johnomalley
Copy link
Author

Ah much better - thanks for your reply. Closing.

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 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
djc pushed a commit that referenced this issue Sep 11, 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:
- #88
- #200
- #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](#815) for discussion about error handling and panics.

Closes #832
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

No branches or pull requests

2 participants