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

Feature request: get absolute duration #810

Closed
alextes opened this issue Sep 2, 2022 · 5 comments
Closed

Feature request: get absolute duration #810

alextes opened this issue Sep 2, 2022 · 5 comments

Comments

@alextes
Copy link
Contributor

alextes commented Sep 2, 2022

I have a stream of timestamped values, I have a function that tries to retrieve one of those values 24 hours back in time, where the value needs to be within 10 minutes, on either side, of now - hours_24. SQL has a great way to get me the row closest to a given point in time, but now I wanted to check on the rust side whether the closest value is actually within our time limit.

The code ends up looking like this:

let h24_ago = Utc::now() - Duration::hours(24);
let secs_between = (h24_ago - eth_price.timestamp).num_seconds().abs();
let is_fresh_enough = Duration::seconds(secs_between) <= age_limit;

I was already quite happy to find negative Duration is perfectly acceptable but I still need to jump through some hoops because it is nice for age_limit to be a Duration rather than a number of seconds or something.

Now that I write this I'm thinking maybe I should just go through the trouble of writing the WHERE in SQL but either way I'll leave this here, perhaps others need it too, and after a couple of 👍 we could consider implementing ((:

@alextes
Copy link
Contributor Author

alextes commented Sep 2, 2022

Mya, as sqlx has no clue how to put a Duration into SQL it ends up being more lines for me I think because now I need to bind the age_limit into the query. Still, its neater to have it in there I think.

@esheppa
Copy link
Collaborator

esheppa commented Sep 3, 2022

This is likely to be implemented in #714 - see chrono::TimeDelta::abs.

@pitdicker
Copy link
Collaborator

The Duration type of chrono has an abs method since #418.
But you get the Duration from time 0.1 which doesn't have abs if the oldtime features is enabled.

@alextes
Copy link
Contributor Author

alextes commented Jun 30, 2023

@pitdicker can you elaborate? It is still not clear to me how I can get an absolute duration using the current release of chrono.

Here's a code sample that doesn't compile:

use chrono; // 0.4.26

struct EthPrice {
    usd: u32,
    timestamp: chrono::DateTime<chrono::Utc>
}

fn main() {
    let eth_price = EthPrice { usd: 2000, timestamp: chrono::Utc::now() };
    let age_limit = chrono::Duration::minutes(10);
    let h24_ago = chrono::Utc::now() - chrono::Duration::hours(24);
    let secs_between = (h24_ago - eth_price.timestamp).abs();
    let is_fresh_enough = chrono::Duration::seconds(secs_between) <= age_limit;
    dbg!(is_fresh_enough);
}

error:

error[[E0599]](https://doc.rust-lang.org/stable/error_codes/E0599.html): no method named `abs` found for struct `chrono::Duration` in the current scope
  --> src/main.rs:12:56
   |
12 |     let secs_between = (h24_ago - eth_price.timestamp).abs();
   |                                                        ^^^ method not found in `Duration`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (bin "playground") due to previous error

Perhaps at the very least we can link an issue tracking when "you get the Duration from time 0.1" is no longer the case and this feature is unlocked? If it isn't already and I am simply misunderstanding how to use it.

@pitdicker
Copy link
Collaborator

It is available if you use chrono with:

[dependencies]
chrono = { version = "0.4.26", default-features = false, features = ["clock", "std"] }

This is unfortunate, hopefully it 'just works' in the next release when we remove the time 0.1 dependency. See #1095.

alextes added a commit to ultrasoundmoney/eth-analysis-rs that referenced this issue Jul 1, 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

No branches or pull requests

3 participants