Add chrono integration
This commit is contained in:
parent
9ec0ddc5ca
commit
b47d6419b8
4 changed files with 41 additions and 1 deletions
|
@ -196,4 +196,5 @@
|
|||
|
||||
### Added
|
||||
|
||||
- `zlib-ng` for fast Deflate compression. This is now the default for compression levels 0-9.
|
||||
- `zlib-ng` for fast Deflate compression. This is now the default for compression levels 0-9.
|
||||
- `chrono` to convert zip_next::DateTime to and from chrono::NaiveDateTime
|
|
@ -16,6 +16,7 @@ edition = "2021"
|
|||
aes = { version = "0.8.2", optional = true }
|
||||
byteorder = "1.4.3"
|
||||
bzip2 = { version = "0.4.4", optional = true }
|
||||
chrono = { version = "0.4.25", optional = true }
|
||||
constant_time_eq = { version = "0.2.5", optional = true }
|
||||
crc32fast = "1.3.2"
|
||||
flate2 = { version = "1.0.26", default-features = false, optional = true }
|
||||
|
@ -40,6 +41,7 @@ time = { version = "0.3.21", features = ["formatting", "macros"] }
|
|||
|
||||
[features]
|
||||
aes-crypto = [ "aes", "constant_time_eq", "hmac", "pbkdf2", "sha1" ]
|
||||
chrono = ["chrono/default"]
|
||||
deflate = ["flate2/rust_backend"]
|
||||
deflate-miniz = ["flate2/default"]
|
||||
deflate-zlib = ["flate2/zlib"]
|
||||
|
|
|
@ -54,6 +54,8 @@ The features available are:
|
|||
is the most effective `deflate` implementation available.
|
||||
* `bzip2`: Enables the BZip2 compression algorithm.
|
||||
* `time`: Enables features using the [time](https://github.com/rust-lang-deprecated/time) crate.
|
||||
* `chrono`: Enables converting last-modified `zip_next::DateTime` to and from `chrono::NaiveDateTime` and from
|
||||
`chrono::DateTime`.
|
||||
* `zstd`: Enables the Zstandard compression algorithm.
|
||||
|
||||
By default `aes-crypto`, `deflate`, `deflate-zlib-ng`, `deflate-zopfli`, `bzip2`, `time` and `zstd` are enabled.
|
||||
|
|
35
src/types.rs
35
src/types.rs
|
@ -3,6 +3,8 @@ use path::{Component, Path, PathBuf};
|
|||
use std::path;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[cfg(feature = "chrono")]
|
||||
use chrono::{Datelike, NaiveDate, NaiveDateTime, NaiveTime, Timelike};
|
||||
#[cfg(not(any(
|
||||
all(target_arch = "arm", target_pointer_width = "32"),
|
||||
target_arch = "mips",
|
||||
|
@ -116,6 +118,39 @@ impl arbitrary::Arbitrary<'_> for DateTime {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "chrono")]
|
||||
#[allow(clippy::result_unit_err)]
|
||||
impl<T> TryFrom<T> for DateTime
|
||||
where
|
||||
T: Datelike + Timelike,
|
||||
{
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: T) -> Result<Self, Self::Error> {
|
||||
Ok(DateTime::from_date_and_time(
|
||||
value.year().try_into()?,
|
||||
value.month().try_into()?,
|
||||
value.day().try_into()?,
|
||||
value.hour().try_into()?,
|
||||
value.minute().try_into()?,
|
||||
value.second().try_into()?,
|
||||
)?)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "chrono")]
|
||||
#[allow(clippy::result_unit_err)]
|
||||
impl TryInto<NaiveDateTime> for DateTime {
|
||||
type Error = ();
|
||||
|
||||
fn try_into(self) -> Result<NaiveDateTime, Self::Error> {
|
||||
Ok(NaiveDateTime::new(
|
||||
NaiveDate::from_ymd_opt(self.year.into(), self.month.into(), self.day.into())?,
|
||||
NaiveTime::from_hms_opt(self.hour.into(), self.minute.into(), self.second.into())?,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for DateTime {
|
||||
/// Constructs an 'default' datetime of 1980-01-01 00:00:00
|
||||
fn default() -> DateTime {
|
||||
|
|
Loading…
Add table
Reference in a new issue