nt_time/lib.rs
1// SPDX-FileCopyrightText: 2023 Shun Sakai
2//
3// SPDX-License-Identifier: Apache-2.0 OR MIT
4
5//! The `nt-time` crate is a [Windows file time] library.
6//!
7//! The [`FileTime`] is a type that represents the file time, which is a 64-bit
8//! unsigned integer value that represents the number of 100-nanosecond
9//! intervals that have elapsed since "1601-01-01 00:00:00 UTC", and is used as
10//! timestamps such as [NTFS] or [7z]. Windows uses a file time to record when
11//! an application creates, accesses, or writes to a file.
12//!
13//! # Examples
14//!
15//! ## Basic usage
16//!
17//! [`FileTime`] can be converted from and to a type which represents time such
18//! as [`time::UtcDateTime`]. Addition and subtraction are also supported.
19//!
20//! ```
21//! use core::time::Duration;
22//!
23//! use nt_time::{
24//! FileTime,
25//! time::{UtcDateTime, macros::utc_datetime},
26//! };
27//!
28//! let ft = FileTime::NT_TIME_EPOCH;
29//! assert_eq!(
30//! UtcDateTime::try_from(ft),
31//! Ok(utc_datetime!(1601-01-01 00:00:00))
32//! );
33//!
34//! let ft = ft + Duration::from_secs(11_644_473_600);
35//! assert_eq!(UtcDateTime::try_from(ft), Ok(UtcDateTime::UNIX_EPOCH));
36//! assert_eq!(ft.to_raw(), 116_444_736_000_000_000);
37//!
38//! // The practical largest file time.
39//! assert_eq!(FileTime::try_from(i64::MAX), Ok(FileTime::SIGNED_MAX));
40//! // The theoretical largest file time.
41//! assert_eq!(FileTime::new(u64::MAX), FileTime::MAX);
42//! ```
43//!
44//! ## Conversion from and to other system times
45//!
46//! [`FileTime`] can be converted from and to other system times such as [Unix
47//! time] or [MS-DOS date and time].
48//!
49//! ### Unix time
50//!
51//! ```
52//! use nt_time::{
53//! FileTime,
54//! time::{UtcDateTime, macros::utc_datetime},
55//! };
56//!
57//! // `1970-01-01 00:00:00 UTC`.
58//! let dt = UtcDateTime::UNIX_EPOCH;
59//! assert_eq!(dt, utc_datetime!(1970-01-01 00:00:00));
60//!
61//! // Convert to a `FileTime`.
62//! let ft = FileTime::from_unix_time_secs(dt.unix_timestamp()).unwrap();
63//! assert_eq!(ft, FileTime::UNIX_EPOCH);
64//!
65//! // Back to Unix time.
66//! let ut = ft.to_unix_time_secs();
67//! assert_eq!(ut, 0);
68//! ```
69//!
70//! ### MS-DOS date and time
71//!
72//! ```
73//! # #[cfg(feature = "dos-date-time")]
74//! # {
75//! use nt_time::{FileTime, dos_date_time::DateTime};
76//!
77//! // `1980-01-01 00:00:00`.
78//! let dt = DateTime::MIN;
79//! assert_eq!(dt.to_string(), "1980-01-01 00:00:00");
80//!
81//! // Convert to a `FileTime`.
82//! let ft = FileTime::from(dt);
83//! assert_eq!(ft, FileTime::new(119_600_064_000_000_000));
84//!
85//! // Back to MS-DOS date and time.
86//! let dt = DateTime::try_from(ft).unwrap();
87//! assert_eq!(
88//! (dt.date().to_raw(), dt.time().to_raw()),
89//! (0b0000_0000_0010_0001, u16::MIN)
90//! );
91//! # }
92//! ```
93//!
94//! ## Formatting and printing the file time
95//!
96//! The formatting traits for [`FileTime`] are implemented to show the
97//! underlying [`u64`] value. If you need a human-readable date and time,
98//! convert [`FileTime`] to a type which represents time such as
99//! [`time::UtcDateTime`].
100//!
101//! ```
102//! use nt_time::{FileTime, time::UtcDateTime};
103//!
104//! let ft = FileTime::NT_TIME_EPOCH;
105//! assert_eq!(format!("{ft}"), "0");
106//!
107//! let dt = UtcDateTime::try_from(ft).unwrap();
108//! assert_eq!(format!("{dt}"), "1601-01-01 0:00:00.0 +00");
109//! ```
110//!
111//! [Windows file time]: https://learn.microsoft.com/en-us/windows/win32/sysinfo/file-times
112//! [NTFS]: https://en.wikipedia.org/wiki/NTFS
113//! [7z]: https://www.7-zip.org/7z.html
114//! [Unix time]: https://en.wikipedia.org/wiki/Unix_time
115//! [MS-DOS date and time]: https://learn.microsoft.com/en-us/windows/win32/sysinfo/ms-dos-date-and-time
116
117#![doc(html_root_url = "https://docs.rs/nt-time/0.13.0/")]
118#![no_std]
119#![cfg_attr(docsrs, feature(doc_cfg))]
120// Lint levels of rustc.
121#![deny(missing_docs)]
122
123#[cfg(test)]
124#[macro_use]
125extern crate alloc;
126#[cfg(feature = "std")]
127extern crate std;
128
129pub mod error;
130mod file_time;
131#[cfg(feature = "serde")]
132pub mod serde_with;
133
134#[cfg(feature = "chrono")]
135pub use chrono;
136#[cfg(feature = "dos-date-time")]
137pub use dos_date_time;
138#[cfg(feature = "jiff")]
139pub use jiff;
140#[cfg(feature = "rand")]
141pub use rand;
142#[cfg(feature = "serde")]
143pub use serde;
144pub use time;
145
146pub use crate::file_time::FileTime;