Day number of the year from timestamps (ic_cdk::time() -> datetime ?)

I’m trying to figure out the day number of the year (1 to 366) of a timestamp that has been generated with the ic_cdk::timer().

I’ve tried to use the Chronos crate but, while it compiles, I cannot create the canister. I’m guessing the library is not supported.

Anyone knows how to transform such a timestamp to a date or to the actual information I’m interested in?

Following does not work.

use chrono::{Datelike, NaiveDateTime};

// timestamp is a date previously generated with time()

pub fn day(timestamp: &u64) -> usize {
     let date = get_naive_date_time(timestamp.clone());
     date.ordinal() as usize

Not working but, first discoveries:

  1. Chrono seems to be deprecated**, instead better to use the time crate
  2. Using time builds and also seems to work at runtime, at least I was able to create a canister which I wasn’t when I used chrono

So now, how to create a date with time from nanoseconds :thinking:

** actually seems that chrono has a new maintainer Reddit - Dive into anything

Shout-out to @dskloet for the help and solution!

Using timer v0.3.28

use time::{Duration, OffsetDateTime};

pub fn day(timestamp: &u64) -> usize {
    let nanoseconds = *timestamp as i64;
    let seconds = nanoseconds / 1_000_000_000;
    let nanos_remainder = nanoseconds % 1_000_000_000;

    let date = OffsetDateTime::from_unix_timestamp(seconds).unwrap() + Duration::nanoseconds(nanos_remainder as i64);

    let ordinal = date.ordinal();

    ordinal as usize