async_zip/date/mod.rs
1// Copyright (c) 2021-2024 Harry [Majored] [hello@majored.pw]
2// MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE)
3
4// https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#446
5// https://learn.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-dosdatetimetovarianttime
6
7/// A date and time stored as per the MS-DOS representation used by ZIP files.
8#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Hash)]
9pub struct ZipDateTime {
10 pub(crate) date: u16,
11 pub(crate) time: u16,
12}
13
14impl ZipDateTime {
15 /// Returns the year of this date & time.
16 pub fn year(&self) -> i32 {
17 (((self.date & 0xFE00) >> 9) + 1980).into()
18 }
19
20 /// Returns the month of this date & time.
21 pub fn month(&self) -> u32 {
22 ((self.date & 0x1E0) >> 5).into()
23 }
24
25 /// Returns the day of this date & time.
26 pub fn day(&self) -> u32 {
27 (self.date & 0x1F).into()
28 }
29
30 /// Returns the hour of this date & time.
31 pub fn hour(&self) -> u32 {
32 ((self.time & 0xF800) >> 11).into()
33 }
34
35 /// Returns the minute of this date & time.
36 pub fn minute(&self) -> u32 {
37 ((self.time & 0x7E0) >> 5).into()
38 }
39
40 /// Returns the second of this date & time.
41 ///
42 /// Note that MS-DOS has a maximum granularity of two seconds.
43 pub fn second(&self) -> u32 {
44 ((self.time & 0x1F) << 1).into()
45 }
46}