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

Fix bug with conversion of Timestamp::Microseconds to chrono::Datetime #134

Merged
merged 2 commits into from
Feb 14, 2024

Conversation

amonin7
Copy link
Contributor

@amonin7 amonin7 commented Feb 13, 2024

Description

Fix bug with the conversion of Timestamp::Microseconds to chrono::Datetime
Fix for issue #133

Checklist

  • Formatted code using cargo fmt --all
  • Linted code using clippy
    • with reqwest feature: cargo clippy --manifest-path influxdb/Cargo.toml --all-targets --no-default-features --features use-serde,derive,reqwest-client -- -D warnings
    • with surf feature: cargo clippy --manifest-path influxdb/Cargo.toml --all-targets --no-default-features --features use-serde,derive,hyper-client -- -D warnings
  • Updated README.md using cargo doc2readme -p influxdb --expand-macros
  • 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

@amonin7
Copy link
Contributor Author

amonin7 commented Feb 13, 2024

FYI

  • checked the code with cargo clippy and had no warnings related to the new code, however, there were warnings related to the previous code. That is why did not checked those marks.

  • did not update the readme, since this is a minor bugfix, but please let me know if I should

@msrd0
Copy link
Collaborator

msrd0 commented Feb 14, 2024

Yeah don't worry about warnings you didn't introduce. Please run rustfmt though.

I was gonna comment that we should probably use the Utc.timestamp_micros etc functions but those somehow return a result whereas timestamp_nanos does not ... chrono is such a mess, they really need to do a breaking change release and fix up their api but for now I guess we just keep calling timestamp_nanos.

@msrd0
Copy link
Collaborator

msrd0 commented Feb 14, 2024

Oh wow we even had a broken test case for micros. I guess you'll need to fix/remove that one.

@amonin7
Copy link
Contributor Author

amonin7 commented Feb 14, 2024

I could see that the timestamp_nanos uses unwrap inside itself, so that is why it returns Datetime instead of Result

    fn timestamp_nanos(&self, nanos: i64) -> DateTime<Self> {
        ...
        self.timestamp_opt(secs, nanos as u32).unwrap()
    }

So since we know, what precision each type has (e.g. Timestamp::milliseconds for sure has ms precision), probably we can call those timestamp_microseconds, etc. with the unwrap after it in place. This is just a suggestion :)

@amonin7
Copy link
Contributor Author

amonin7 commented Feb 14, 2024

Oh wow we even had a broken test case for micros. I guess you'll need to fix/remove that one.

Yeah, I did not remove it, since did not know, if somebody needed it for some reason... I can remove it if you want

@msrd0
Copy link
Collaborator

msrd0 commented Feb 14, 2024

Yeah, I did not remove it, since did not know, if somebody needed it for some reason... I can remove it if you want

Well the test divides 1 by some number greater than 1, so it gets integer'ed to 0. Looks to me like the test was just as broken as the impl. I'd probably just replace the existing test with your new test.

@msrd0
Copy link
Collaborator

msrd0 commented Feb 14, 2024

Also please remove the MICRO_PER_NANO constant, that should be 0.0001 or something like that, not 1000.

Copy link
Collaborator

@msrd0 msrd0 left a comment

Choose a reason for hiding this comment

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

Thanks

@amonin7
Copy link
Contributor Author

amonin7 commented Feb 14, 2024

Also, as I mentioned, cargo clippy warns about using deprecated chronology method:
warning: use of deprecated method chrono::DateTime::<Tz>::timestamp_nanos: use timestamp_nanos_opt() instead
--> influxdb/src/query/mod.rs:94:42

idk, if this is helpful, but you can use

opt with unwrap to fix this clippy warning, like this

    fn from(date_time: DateTime<T>) -> Self {
       Timestamp::Nanoseconds(date_time.timestamp_nanos_opt().unwrap() as u128)
   }

@msrd0 msrd0 merged commit 4a93896 into influxdb-rs:main Feb 14, 2024
19 of 23 checks passed
@amonin7 amonin7 deleted the bugfix_timestamp_micros branch February 14, 2024 09:57
@msrd0
Copy link
Collaborator

msrd0 commented Feb 14, 2024

Influxdb 0.7.2 has been released with this fix.

@amonin7
Copy link
Contributor Author

amonin7 commented Feb 14, 2024

Thanks for letting me know! Will be happy to use the correct impl :)

@amonin7
Copy link
Contributor Author

amonin7 commented Feb 14, 2024

p.s. I've seen that you have also corrected the impl from as I suggested
Happy to help :)

@msrd0
Copy link
Collaborator

msrd0 commented Feb 14, 2024

Yeah I knew that was a thing and easy to fix I'd just been too lazy to do that until now 😅

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 this pull request may close these issues.

2 participants