Welcome to mirror list, hosted at ThFree Co, Russian Federation.

time.rs « src - github.com/windirstat/ntfs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b358ab42de417c831add309c0c0f113386d13808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2021 Colin Finck <colin@reactos.org>
// SPDX-License-Identifier: MIT OR Apache-2.0

use binread::BinRead;
use derive_more::From;

#[cfg(any(feature = "chrono", feature = "std"))]
use core::convert::TryFrom;

#[cfg(feature = "chrono")]
use {
    crate::error::NtfsError,
    chrono::{DateTime, Datelike, NaiveDate, Timelike, Utc},
};

#[cfg(feature = "std")]
use std::time::{SystemTime, SystemTimeError};

/// Number of days between 0001-01-01 and 1601-01-01.
#[cfg(feature = "chrono")]
const DAYS_FROM_0001_TO_1601: i32 = 584389;

/// Difference in 100-nanosecond intervals between the Windows/NTFS epoch (1601-01-01) and the Unix epoch (1970-01-01).
#[cfg(feature = "std")]
const EPOCH_DIFFERENCE_IN_INTERVALS: i64 = 116_444_736_000_000_000;

/// Number of 100-nanosecond intervals in a second.
#[cfg(any(feature = "chrono", feature = "std"))]
const INTERVALS_PER_SECOND: u64 = 10_000_000;

/// Number of 100-nanosecond intervals in a day.
#[cfg(feature = "chrono")]
const INTERVALS_PER_DAY: u64 = 24 * 60 * 60 * INTERVALS_PER_SECOND;

/// An NTFS timestamp, used for expressing file times.
///
/// NTFS (and the Windows NT line of operating systems) represent time as an unsigned 64-bit integer
/// counting the number of 100-nanosecond intervals since January 1, 1601.
#[derive(BinRead, Clone, Copy, Debug, Eq, From, Ord, PartialEq, PartialOrd)]
pub struct NtfsTime(u64);

impl NtfsTime {
    /// Returns the stored NT timestamp (number of 100-nanosecond intervals since January 1, 1601).
    pub fn nt_timestamp(&self) -> u64 {
        self.0
    }
}

#[cfg(feature = "chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
impl TryFrom<DateTime<Utc>> for NtfsTime {
    type Error = NtfsError;

    fn try_from(dt: DateTime<Utc>) -> Result<Self, Self::Error> {
        // First do the time calculations, which safely fit into a u64.
        let mut intervals = dt.hour() as u64;

        intervals *= 60;
        intervals += dt.minute() as u64;

        intervals *= 60;
        intervals += dt.second() as u64;

        intervals *= INTERVALS_PER_SECOND;
        intervals += dt.nanosecond() as u64 / 100;

        // Now do checked arithmetics for the day calculations, which may
        // exceed the lower bounds (years before 1601) or upper bounds
        // (dates after approximately 28 May 60056).
        let num_days_from_ce = dt.num_days_from_ce();
        let num_days_from_1601 = num_days_from_ce
            .checked_sub(DAYS_FROM_0001_TO_1601)
            .ok_or(NtfsError::InvalidTime)?;
        let intervals_days = INTERVALS_PER_DAY
            .checked_mul(num_days_from_1601 as u64)
            .ok_or(NtfsError::InvalidTime)?;
        intervals = intervals
            .checked_add(intervals_days)
            .ok_or(NtfsError::InvalidTime)?;

        Ok(Self(intervals))
    }
}

#[cfg(feature = "chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
impl From<NtfsTime> for DateTime<Utc> {
    fn from(nt: NtfsTime) -> DateTime<Utc> {
        let mut remainder = nt.nt_timestamp();

        let nano = (remainder % INTERVALS_PER_SECOND) as u32 * 100;
        remainder /= INTERVALS_PER_SECOND;

        let sec = (remainder % 60) as u32;
        remainder /= 60;

        let min = (remainder % 60) as u32;
        remainder /= 60;

        let hour = (remainder % 24) as u32;
        remainder /= 24;

        let num_days_from_1601 = remainder as i32;
        let num_days_from_ce = num_days_from_1601 + DAYS_FROM_0001_TO_1601;

        let ndt =
            NaiveDate::from_num_days_from_ce(num_days_from_ce).and_hms_nano(hour, min, sec, nano);
        DateTime::<Utc>::from_utc(ndt, Utc)
    }
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl TryFrom<SystemTime> for NtfsTime {
    type Error = SystemTimeError;

    fn try_from(st: SystemTime) -> Result<Self, Self::Error> {
        let duration_since_unix_epoch = st.duration_since(SystemTime::UNIX_EPOCH)?;
        let intervals_since_unix_epoch = duration_since_unix_epoch.as_secs() as u64
            * INTERVALS_PER_SECOND
            + duration_since_unix_epoch.subsec_nanos() as u64 / 100;
        let intervals_since_windows_epoch =
            intervals_since_unix_epoch + EPOCH_DIFFERENCE_IN_INTERVALS as u64;
        Ok(Self(intervals_since_windows_epoch))
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;

    #[cfg(feature = "chrono")]
    use chrono::TimeZone;

    pub(crate) const NT_TIMESTAMP_2021_01_01: u64 = 132539328000000000u64;

    #[cfg(feature = "chrono")]
    #[test]
    fn test_chrono() {
        let dt = Utc.ymd(2013, 1, 5).and_hms(18, 15, 00);
        let nt = NtfsTime::try_from(dt).unwrap();
        assert_eq!(*nt, 130018833000000000u64);

        let dt2 = DateTime::<Utc>::from(nt);
        assert_eq!(dt, dt2);

        let dt = Utc.ymd(1601, 1, 1).and_hms(0, 0, 0);
        let nt = NtfsTime::try_from(dt).unwrap();
        assert_eq!(*nt, 0u64);

        let dt = Utc.ymd(1600, 12, 31).and_hms(23, 59, 59);
        assert!(NtfsTime::try_from(dt).is_err());

        let dt = Utc.ymd(60056, 5, 28).and_hms(0, 0, 0);
        assert!(NtfsTime::try_from(dt).is_ok());

        let dt = Utc.ymd(60056, 5, 29).and_hms(0, 0, 0);
        assert!(NtfsTime::try_from(dt).is_err());
    }

    #[cfg(feature = "std")]
    #[test]
    fn test_systemtime() {
        let st = SystemTime::now();
        let nt = NtfsTime::try_from(st).unwrap();
        assert!(nt.nt_timestamp() > NT_TIMESTAMP_2021_01_01);
    }
}