Skip to main content

nt_time/file_time/
consts.rs

1// SPDX-FileCopyrightText: 2023 Shun Sakai
2//
3// SPDX-License-Identifier: Apache-2.0 OR MIT
4
5//! Constants for [`FileTime`].
6
7use super::{FILE_TIMES_PER_SEC, FileTime};
8
9impl FileTime {
10    /// The [NT time epoch].
11    ///
12    /// This is defined as "1601-01-01 00:00:00 UTC", which was the first year
13    /// of the 400-year Gregorian calendar cycle at the time Windows NT was
14    /// being designed.
15    ///
16    /// # Examples
17    ///
18    /// ```
19    /// # use nt_time::{FileTime, time::macros::utc_datetime};
20    /// #
21    /// assert_eq!(FileTime::NT_TIME_EPOCH, utc_datetime!(1601-01-01 00:00:00));
22    /// ```
23    ///
24    /// [NT time epoch]: https://en.wikipedia.org/wiki/Epoch_(computing)
25    pub const NT_TIME_EPOCH: Self = Self::new(u64::MIN);
26
27    /// The [Unix epoch].
28    ///
29    /// This is defined as "1970-01-01 00:00:00 UTC", which is 134,774 days
30    /// after [`FileTime::NT_TIME_EPOCH`].
31    ///
32    /// # Examples
33    ///
34    /// ```
35    /// # use nt_time::{FileTime, time::UtcDateTime};
36    /// #
37    /// assert_eq!(FileTime::UNIX_EPOCH, UtcDateTime::UNIX_EPOCH);
38    /// ```
39    ///
40    /// [Unix epoch]: https://en.wikipedia.org/wiki/Unix_time
41    pub const UNIX_EPOCH: Self = Self::new(134_774 * 86400 * FILE_TIMES_PER_SEC);
42
43    /// The largest file time accepted by the [`FileTimeToSystemTime`] function
44    /// of the [Win32 API].
45    ///
46    /// This is "+30828-09-14 02:48:05.477580700 UTC".
47    ///
48    /// # Examples
49    ///
50    /// ```
51    /// # #[cfg(feature = "large-dates")]
52    /// # {
53    /// # use nt_time::{FileTime, time::macros::utc_datetime};
54    /// #
55    /// assert_eq!(
56    ///     FileTime::SIGNED_MAX,
57    ///     utc_datetime!(+30828-09-14 02:48:05.477_580_700)
58    /// );
59    /// # }
60    /// ```
61    ///
62    /// [`FileTimeToSystemTime`]: https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-filetimetosystemtime
63    /// [Win32 API]: https://learn.microsoft.com/en-us/windows/win32/
64    pub const SIGNED_MAX: Self = Self::new(i64::MAX as u64);
65
66    /// The largest value that can be represented by the file time.
67    ///
68    /// This is "+60056-05-28 05:36:10.955161500 UTC", which is the theoretical
69    /// largest value that the [`FILETIME`] structure of the [Win32 API] can
70    /// represent.
71    ///
72    /// # Examples
73    ///
74    /// ```
75    /// # #[cfg(feature = "large-dates")]
76    /// # {
77    /// # use nt_time::{FileTime, time::macros::utc_datetime};
78    /// #
79    /// assert_eq!(
80    ///     FileTime::MAX,
81    ///     utc_datetime!(+60056-05-28 05:36:10.955_161_500)
82    /// );
83    /// # }
84    /// ```
85    ///
86    /// [`FILETIME`]: https://learn.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime
87    /// [Win32 API]: https://learn.microsoft.com/en-us/windows/win32/
88    pub const MAX: Self = Self::new(u64::MAX);
89}
90
91#[cfg(test)]
92mod tests {
93    use time::{UtcDateTime, macros::utc_datetime};
94
95    use super::*;
96
97    #[test]
98    fn nt_time_epoch() {
99        assert_eq!(FileTime::NT_TIME_EPOCH, utc_datetime!(1601-01-01 00:00:00));
100    }
101
102    #[test]
103    fn unix_epoch() {
104        assert_eq!(FileTime::UNIX_EPOCH, UtcDateTime::UNIX_EPOCH);
105    }
106
107    #[cfg(feature = "large-dates")]
108    #[test]
109    fn signed_max() {
110        assert_eq!(
111            FileTime::SIGNED_MAX,
112            utc_datetime!(+30828-09-14 02:48:05.477_580_700)
113        );
114    }
115
116    #[cfg(feature = "large-dates")]
117    #[test]
118    fn max() {
119        assert_eq!(
120            FileTime::MAX,
121            utc_datetime!(+60056-05-28 05:36:10.955_161_500)
122        );
123    }
124}