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

More support for chrono::DateTime #171

Closed
NickLarsenNZ opened this issue Feb 29, 2024 · 2 comments
Closed

More support for chrono::DateTime #171

NickLarsenNZ opened this issue Feb 29, 2024 · 2 comments

Comments

@NickLarsenNZ
Copy link

NickLarsenNZ commented Feb 29, 2024

I'm trying to generate random times between now and some days ago.

I see the example here:

let start_dt: chrono::DateTime<Utc> = DateTimeBefore(EN, Utc::now()).fake();
println!("{}", start_dt);
let end_dt: chrono::DateTime<Utc> = DateTimeAfter(EN, Utc::now()).fake();
println!("{}", end_dt);
let between: chrono::DateTime<Utc> = DateTimeBetween(EN, start_dt, end_dt).fake();
println!("{}", between);

But that appears to generate random times between random dates. I need to do something more like this:

    let three_days_ago = Utc::now() - Duration::days(3);
    let now = Utc::now();

    let start_time: chrono::DateTime<Utc> = DateTimeBetween(three_days_ago, now).fake();

But I get an error:

note: expected `OffsetDateTime`, found `DateTime<Utc>`
Full error
error[E0308]: arguments to this function are incorrect
   --> src/main.rs:80:45
    |
77  |     let three_days_ago = Utc::now() - Duration::days(3);
    |                          ------------------------------ here the type of `three_days_ago` is inferred to be `chrono::DateTime<Utc>`
...
80  |     let start_time: chrono::DateTime<Utc> = DateTimeBetween(three_days_ago, now).fake();
    |                                             ^^^^^^^^^^^^^^^
    |
note: expected `OffsetDateTime`, found `DateTime<Utc>`
   --> src/main.rs:80:61
    |
80  |     let start_time: chrono::DateTime<Utc> = DateTimeBetween(three_days_ago, now).fake();
    |                                                             ^^^^^^^^^^^^^^
    = note: expected struct `time::offset_date_time::OffsetDateTime`
               found struct `chrono::DateTime<Utc>`
note: expected `OffsetDateTime`, found `DateTime<Utc>`
   --> src/main.rs:80:77
    |
80  |     let start_time: chrono::DateTime<Utc> = DateTimeBetween(three_days_ago, now).fake();
    |                                                                             ^^^
    = note: expected struct `time::offset_date_time::OffsetDateTime`
               found struct `chrono::DateTime<Utc>`
note: function defined here
   --> /home/nick/.cargo/registry/src/index.crates.io-6f17d22bba15001f/fake-2.9.2/src/faker/mod.rs:118:9
    |
118 |         DateTimeBetween(start: time::OffsetDateTime, end: time::OffsetDateTime);
    |         ^^^^^^^^^^^^^^^

error[E0277]: the trait bound `chrono::DateTime<Utc>: Dummy<fake::faker::time::raw::DateTimeBetween<EN>>` is not satisfied
   --> src/main.rs:80:82
    |
80  |     let start_time: chrono::DateTime<Utc> = DateTimeBetween(three_days_ago, now).fake();
    |                                                                                  ^^^^ the trait `Dummy<fake::faker::time::raw::DateTimeBetween<EN>>` is not implemented for `chrono::DateTime<Utc>`
    |
    = help: the following other types implement trait `Dummy<T>`:
              <chrono::DateTime<Utc> as Dummy<fake::faker::chrono::raw::DateTimeBefore<L>>>
              <chrono::DateTime<Utc> as Dummy<fake::faker::chrono::raw::DateTimeAfter<L>>>
              <chrono::DateTime<Utc> as Dummy<fake::faker::chrono::raw::DateTimeBetween<L>>>
    = note: required for `fake::faker::time::raw::DateTimeBetween<EN>` to implement `fake::private::FakeBase<chrono::DateTime<Utc>>`
note: required by a bound in `fake`
   --> /home/nick/.cargo/registry/src/index.crates.io-6f17d22bba15001f/fake-2.9.2/src/lib.rs:202:15
    |
200 |     fn fake<U>(&self) -> U
    |        ---- required by a bound in this associated function
201 |     where
202 |         Self: private::FakeBase<U>,
    |               ^^^^^^^^^^^^^^^^^^^^ required by this bound in `Fake::fake`

Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.

It appears that I must use the OffsetDateTime from the time crate like so:

    let three_days_ago = OffsetDateTime::from(OffsetDateTime::now_utc() - Duration::days(3));
    let now = OffsetDateTime::now_utc();

    let start_time: OffsetDateTime = DateTimeBetween(three_days_ago, now).fake();

Would it be much effort to support chrono::DateTime?

@NickLarsenNZ NickLarsenNZ changed the title More support for chrono DateTime More support for chrono::DateTime Feb 29, 2024
@cksac
Copy link
Owner

cksac commented Mar 1, 2024

It already supported, https://github.com/cksac/fake-rs/blob/master/fake/src/faker/mod.rs#L105.
and below run without error,

use chrono::{Duration, Utc};
use fake::{faker::chrono::en::*, Fake};

fn main() {
    let three_days_ago = Utc::now() - Duration::days(3);
    let now = Utc::now();

    let start_time: chrono::DateTime<Utc> = DateTimeBetween(three_days_ago, now).fake();
    println!("start_time {:?}", start_time);
}

@NickLarsenNZ
Copy link
Author

Oh, I didn't see the extra import needed. I will refactor my code.
Thanks you.

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