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

Chrono types for Timestamp #41

Merged
merged 14 commits into from
Oct 31, 2019

Conversation

durangatan
Copy link

@durangatan durangatan commented Oct 27, 2019

Description

Fixes #35.

  • Each branch of the Into implementation casts everything to a DateTime<Utc> with at least millisecond precision. I figure the caller can cast the value to a timezone if needed.
  • I'm not sure if I'm representing Timestamps the preferred way in UTC-land, because I'm not super familiar with how influxdb works. For example, Timestamp::Hours(3) probably doesn't mean three hours from the epoch, but for now I kept it consistent.
  • The From<DateTime> implementation creates a Timestamp::Milliseconds from a given date. It will work for any DateTime object that implements TimeZone.
  • these implementations are behind the chrono_timestamps compilation feature.

I hope this is sort of what you're looking for; let me know if you want me to take it in a different direction or implement more chrono converters.

Checklist

  • Formatted code using cargo fmt --all
  • Linted code using clippy cargo clippy --all-targets --all-features -- -D warnings
  • Updated README.md using cargo readme > README.md
  • Reviewed the diff. Did you leave any print statements or unnecessary comments?
  • Any unfinished work that warrants a separate issue captured in an issue with a TODO code comment

src/query/mod.rs Outdated Show resolved Hide resolved
@Empty2k12 Empty2k12 requested a review from msrd0 October 28, 2019 10:35
msrd0
msrd0 previously requested changes Oct 28, 2019
Cargo.toml Outdated
chrono = { version = "0.4.9", optional = true }

[dev-dependencies]
chrono = { version = "0.4.9" }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you always enable the chrono feature in test by all those any(test, feature = "chrono_timestamp")? We test with cargo test --all-features so I guess it's fine to only enable the test when the feature is enabled

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
chrono = { version = "0.4.9" }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@durangatan this one and we're ready to merge ;)

src/query/mod.rs Outdated Show resolved Hide resolved
src/query/mod.rs Outdated Show resolved Hide resolved
@@ -157,7 +201,14 @@ pub enum QueryType {

#[cfg(test)]
mod tests {
extern crate chrono;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be feature-gated (see above)

Copy link
Collaborator

@Empty2k12 Empty2k12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR! 🎉

For example, Timestamp::Hours(3) probably doesn't mean three hours from the epoch, but for now I kept it consistent.

Sadly, this is exactly the way it is. Time in InfluxDb is UTC and an Unix nanosecond timestamp by default. We can set other precisions (which is done using the Timestamp enum).

The From implementation creates a Timestamp::Milliseconds from a given date. It will work for any DateTime object that implements TimeZone

It should probably use Timestamp::Nanoseconds by default, as that's the preferred time resolution from InfluxDb

It would probably also be nice to document the feature gate somewhere, so potential users can find this easily.

src/query/mod.rs Outdated Show resolved Hide resolved
src/query/mod.rs Outdated Show resolved Hide resolved
@Empty2k12 Empty2k12 added this to the Release 0.0.6 milestone Oct 28, 2019
@Empty2k12 Empty2k12 changed the title Chrono timestamps Feature-gates Chrono types Oct 28, 2019
@Empty2k12 Empty2k12 changed the title Feature-gates Chrono types Feature-gated Chrono types Oct 28, 2019
@msrd0
Copy link
Collaborator

msrd0 commented Oct 28, 2019

Btw, right now you only implement DateTime<Utc> and I guess this makes sense not to have arbitrary Timezones but have the user convert them first. But would it make sense to support NaiveDateTime if the user explicitly don't care about timezones?

@durangatan
Copy link
Author

durangatan commented Oct 28, 2019

I chose UTC because it is a "true unix timestamp".
from the docs for NaiveDateTime.timestamp_nanos:


Note that this does not account for the timezone! The true "UNIX timestamp" would count seconds since the midnight UTC on the epoch.

lmk if I should change the implementation to NaiveDateTime

@msrd0
Copy link
Collaborator

msrd0 commented Oct 28, 2019

I see your point, but for some user who doesn't care about the timezone and interprets the Utc time as if it was their local one, it might make sense to also support NaiveDateTime. @Empty2k12 what do you think?

@durangatan
Copy link
Author

Based on the above comments in the thread, I think it's a safe assumption that a user will expect an influxDB timestamp to represent a UTC datetime, not a timezoned time.
"Time in InfluxDb is UTC and an Unix nanosecond timestamp by default. We can set other precisions (which is done using the Timestamp enum)."

@Empty2k12
Copy link
Collaborator

I would say that supporting UTC only is reasonable, as InfluxDb makes that assumption for us and we should strive to behave similar to it.

@durangatan
Copy link
Author

not sure how to fix the last bit of the build; looks like kcov is failing but I can't tell which command specifically fails

@Empty2k12
Copy link
Collaborator

Empty2k12 commented Oct 29, 2019

From the logs, it looks like cargo-travis is not being run with --all-features. I think this argument is missing in these two lines: https://github.com/Empty2k12/influxdb-rust/blob/98820c7a1d369cce6ea23a7489fb544e92b179b9/.travis.yml#L54-L55

Since I'm planning on removing coverage in #23 since it's broken and gives inaccurate results I'd be fine with you also removing the cargo-travis step and all references to code coverage (lib.rs badge and Cargo.toml badge)

Empty2k12
Empty2k12 previously approved these changes Oct 31, 2019
Copy link
Collaborator

@Empty2k12 Empty2k12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Thanks for contributing this!

Since @msrd0 requested the feature, I'll have him have the last words.

Cargo.toml Outdated Show resolved Hide resolved
src/query/mod.rs Outdated Show resolved Hide resolved
src/query/mod.rs Outdated Show resolved Hide resolved
@Empty2k12 Empty2k12 dismissed their stale review October 31, 2019 14:00

Some small suggestions have been noticed.

Joe Duran and others added 4 commits October 31, 2019 09:09
Co-Authored-By: Gero Gerke <hello@gerogerke.de>
Co-Authored-By: Gero Gerke <hello@gerogerke.de>
Co-Authored-By: Gero Gerke <hello@gerogerke.de>
Copy link
Collaborator

@Empty2k12 Empty2k12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for this contribution. 🎉
This will be helpful to everyone working with Chrono in their projects.

@Empty2k12 Empty2k12 changed the title Feature-gated Chrono types Chrono types for Timestamp Oct 31, 2019
@Empty2k12 Empty2k12 merged commit 04d1fc8 into influxdb-rs:master Oct 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create Timestamp from chrono type
3 participants