Search code examples
rustrust-chrono

No function or associated item named `now` found for struct `chrono::offset::utc::Utc` in the current scope


The method I'm trying to use is documented here.

Here is the code I'm trying to use it in:

use chrono::{DateTime, Utc};

struct Attributes {
    time: Option<DateTime<Utc>>,
}


impl Default for Attributes {
    fn default() -> Self {
        Attributes {
            time: Some(chrono::offset::Utc::now()),
        }
    }
}

And here is the error I get on running cargo build:

error[E0599]: no function or associated item named `now` found for struct `chrono::offset::utc::Utc` in the current scope
   --> src/event/v03/attributes.rs:165:45
    |
165 |             time: Some(chrono::offset::Utc::now()),
    |                                             ^^^ function or associated item not found in `chrono::offset::utc::Utc`

I'm not sure where the issue is, but I'm sure the method exists. Any insight would be much appreciated.


Solution

  • This was quite a silly mistake on my end. I overlooked the fact that I was using chrono with the std feature turned off. This required me disabling all other default features, including the feature clock which facilitates chrono::offset::Utc::now().