Expand description
§Timetraveler
A crate for converting between different time representations.
§Breaking changes in 0.4.0
Since Jiff was introduced it doesn’t make sense for the library to be opinionated on which features should be default —so by default no features are enabled now. You need to enable at least one feature or a compiler error will be raised.
§Features
- chrono: Support for conversion to/from chrono
- chrono-tz: Additional Support for conversion to/from Chrono’s timezones (implies chrono)
- time: Support for conversion to/from time
- jiff: Support for conversion to/from jiff
Enabling only a single feature only enables the traits for converting to that crates structs. To enable the implementation of the conversion traits, you need to enable at least two features.
§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<Utc>
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<FixedOffset>
let offset: DateTime<FixedOffset> = dt.as_date_time_offset();
expect(offset.to_rfc3339().as_ref()).to_equal("2024-01-01T12:00:00+02: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 features are chrono, time, and jiff for support for converting to/from each respective crate.
No features are enabled by default.
Note that at least two features must be enabled for the conversions to work—enabling just one will only give you the traits for converting to representations in that crate.
§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).