| 1 | // Copyright (c) 2021-2024 Harry [Majored] [[email protected]] |
| 2 | // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE) |
| 3 | |
| 4 | pub mod builder; |
| 5 | |
| 6 | #[cfg(feature = "chrono")] |
| 7 | use chrono::{DateTime, Datelike, LocalResult, TimeZone, Timelike, Utc}; |
| 8 | |
| 9 | use self::builder::ZipDateTimeBuilder; |
| 10 | |
| 11 | // https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#446 |
| 12 | // https://learn.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-dosdatetimetovarianttime |
| 13 | |
| 14 | /// A date and time stored as per the MS-DOS representation used by ZIP files. |
| 15 | #[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Hash)] |
| 16 | pub struct ZipDateTime { |
| 17 | pub(crate) date: u16, |
| 18 | pub(crate) time: u16, |
| 19 | } |
| 20 | |
| 21 | impl ZipDateTime { |
| 22 | /// Returns the year of this date & time. |
| 23 | pub fn year(&self) -> i32 { |
| 24 | (((self.date & 0xFE00) >> 9) + 1980).into() |
| 25 | } |
| 26 | |
| 27 | /// Returns the month of this date & time. |
| 28 | pub fn month(&self) -> u32 { |
| 29 | ((self.date & 0x1E0) >> 5).into() |
| 30 | } |
| 31 | |
| 32 | /// Returns the day of this date & time. |
| 33 | pub fn day(&self) -> u32 { |
| 34 | (self.date & 0x1F).into() |
| 35 | } |
| 36 | |
| 37 | /// Returns the hour of this date & time. |
| 38 | pub fn hour(&self) -> u32 { |
| 39 | ((self.time & 0xF800) >> 11).into() |
| 40 | } |
| 41 | |
| 42 | /// Returns the minute of this date & time. |
| 43 | pub fn minute(&self) -> u32 { |
| 44 | ((self.time & 0x7E0) >> 5).into() |
| 45 | } |
| 46 | |
| 47 | /// Returns the second of this date & time. |
| 48 | /// |
| 49 | /// Note that MS-DOS has a maximum granularity of two seconds. |
| 50 | pub fn second(&self) -> u32 { |
| 51 | ((self.time & 0x1F) << 1).into() |
| 52 | } |
| 53 | |
| 54 | /// Constructs chrono's [`DateTime`] representation of this date & time. |
| 55 | /// |
| 56 | /// Note that this requires the `chrono` feature. |
| 57 | #[cfg(feature = "chrono")] |
| 58 | pub fn as_chrono(&self) -> LocalResult<DateTime<Utc>> { |
| 59 | self.into() |
| 60 | } |
| 61 | |
| 62 | /// Constructs this date & time from chrono's [`DateTime`] representation. |
| 63 | /// |
| 64 | /// Note that this requires the `chrono` feature. |
| 65 | #[cfg(feature = "chrono")] |
| 66 | pub fn from_chrono(dt: &DateTime<Utc>) -> Self { |
| 67 | dt.into() |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | impl From<ZipDateTimeBuilder> for ZipDateTime { |
| 72 | fn from(builder: ZipDateTimeBuilder) -> Self { |
| 73 | builder.0 |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | #[cfg(feature = "chrono")] |
| 78 | impl From<&DateTime<Utc>> for ZipDateTime { |
| 79 | fn from(value: &DateTime<Utc>) -> Self { |
| 80 | let mut builder = ZipDateTimeBuilder::new(); |
| 81 | |
| 82 | builder = builder.year(value.date_naive().year()); |
| 83 | builder = builder.month(value.date_naive().month()); |
| 84 | builder = builder.day(value.date_naive().day()); |
| 85 | builder = builder.hour(value.time().hour()); |
| 86 | builder = builder.minute(value.time().minute()); |
| 87 | builder = builder.second(value.time().second()); |
| 88 | |
| 89 | builder.build() |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[cfg(feature = "chrono")] |
| 94 | impl From<&ZipDateTime> for LocalResult<DateTime<Utc>> { |
| 95 | fn from(value: &ZipDateTime) -> Self { |
| 96 | Utc.with_ymd_and_hms(value.year(), value.month(), value.day(), value.hour(), value.minute(), value.second()) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #[cfg(feature = "chrono")] |
| 101 | impl From<DateTime<Utc>> for ZipDateTime { |
| 102 | fn from(value: DateTime<Utc>) -> Self { |
| 103 | (&value).into() |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | #[cfg(feature = "chrono")] |
| 108 | impl From<ZipDateTime> for LocalResult<DateTime<Utc>> { |
| 109 | fn from(value: ZipDateTime) -> Self { |
| 110 | (&value).into() |
| 111 | } |
| 112 | } |