Skip to main content

nt_time/file_time/
fmt.rs

1// SPDX-FileCopyrightText: 2023 Shun Sakai
2//
3// SPDX-License-Identifier: Apache-2.0 OR MIT
4
5//! Utilities for formatting and printing [`FileTime`].
6
7use core::fmt;
8
9use super::FileTime;
10
11impl fmt::Display for FileTime {
12    /// Shows the underlying [`u64`] value of this `FileTime`.
13    ///
14    /// # Examples
15    ///
16    /// ```
17    /// # use nt_time::FileTime;
18    /// #
19    /// assert_eq!(format!("{}", FileTime::NT_TIME_EPOCH), "0");
20    /// assert_eq!(format!("{}", FileTime::UNIX_EPOCH), "116444736000000000");
21    /// assert_eq!(format!("{}", FileTime::SIGNED_MAX), "9223372036854775807");
22    /// assert_eq!(format!("{}", FileTime::MAX), "18446744073709551615");
23    /// ```
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        u64::from(*self).fmt(f)
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    #[cfg(feature = "std")]
32    use proptest::prop_assert_eq;
33    #[cfg(feature = "std")]
34    use test_strategy::proptest;
35
36    use super::*;
37
38    #[test]
39    fn debug() {
40        assert_eq!(format!("{:?}", FileTime::NT_TIME_EPOCH), "FileTime(0)");
41        assert_eq!(
42            format!("{:?}", FileTime::UNIX_EPOCH),
43            "FileTime(116444736000000000)"
44        );
45        assert_eq!(
46            format!("{:?}", FileTime::SIGNED_MAX),
47            "FileTime(9223372036854775807)"
48        );
49        assert_eq!(
50            format!("{:?}", FileTime::MAX),
51            "FileTime(18446744073709551615)"
52        );
53    }
54
55    #[test]
56    fn display() {
57        assert_eq!(format!("{}", FileTime::NT_TIME_EPOCH), "0");
58        assert_eq!(format!("{}", FileTime::UNIX_EPOCH), "116444736000000000");
59        assert_eq!(format!("{}", FileTime::SIGNED_MAX), "9223372036854775807");
60        assert_eq!(format!("{}", FileTime::MAX), "18446744073709551615");
61    }
62
63    #[cfg(feature = "std")]
64    #[proptest]
65    fn display_roundtrip(ft: FileTime) {
66        prop_assert_eq!(format!("{ft}"), format!("{}", ft.to_raw()));
67    }
68}