Crate timetraveler

Crate timetraveler 

Source
Expand description

§Timetraveler

A crate for converting between different time representations.

API Documentation

§Usage

use timetraveler::{chrono::AsDateTime, time::AsOffsetDateTime};
use time::{macros::datetime, OffsetDateTime};
use chrono::{DateTime, Utc, FixedOffset};
use chrono_tz::{Tz, Europe::Stockholm};
use rxpect::{expect, expectations::EqualityExpectations};
 
// A time::OffsetDateTime
let dt = datetime!(2024-01-01 12:00:00 +02:00);

// Convert to chrono::DateTime<Stockholm>
let utc: DateTime<Utc> = dt.as_date_time_utc();
expect(utc.to_rfc3339().as_ref()).to_equal("2024-01-01T10:00:00+00:00");

// Convert to chrono::DateTime<Stockholm>
let stockholm: DateTime<Tz> = dt.as_date_time(Stockholm);
expect(stockholm.to_rfc3339().as_ref()).to_equal("2024-01-01T11:00:00+01:00");

// Convert back to time::OffsetDateTime
let roundtrip: OffsetDateTime = stockholm.as_offset_date_time();
expect(roundtrip).to_equal(dt);

§Features

The conversions in this crate are based on features. One feature per supported crate.

Currently, the only features are chrono and time and they are both enabled by default.

Note that both features must be enabled for the conversions to work—i.e. you can’t just enable one of them.

§Contributions

Missing a conversion function for a struct? Or maybe for a whole new crate?

Contributions are always welcome, but open an issue first and let’s discuss before you sink your precious time into it.

If there is already an open issue, go ahead and open that PR!

§License

This project is licensed under both the MIT license and the Apache License (Version 2.0).

Modules§

chrono
time