dos_date_time/lib.rs
1// SPDX-FileCopyrightText: 2025 Shun Sakai
2//
3// SPDX-License-Identifier: Apache-2.0 OR MIT
4
5//! The `dos-date-time` crate is an [MS-DOS date and time] library.
6//!
7//! The [`DateTime`] is a type that represents MS-DOS date and time, which are
8//! packed 16-bit unsigned integer values that specify the date and time an
9//! MS-DOS file was last written to, and are used as timestamps such as [FAT] or
10//! [ZIP] file format.
11//!
12//! # Examples
13//!
14//! ## Basic usage
15//!
16//! [`DateTime`] can be converted from and to a type which represents time such
17//! as [`time::PrimitiveDateTime`].
18//!
19//! ```
20//! use core::time::Duration;
21//!
22//! use dos_date_time::{
23//! DateTime,
24//! time::{
25//! PrimitiveDateTime,
26//! macros::{date, datetime, time},
27//! },
28//! };
29//!
30//! let dt = DateTime::MIN;
31//! let dt = PrimitiveDateTime::from(dt);
32//! assert_eq!(dt, datetime!(1980-01-01 00:00:00));
33//!
34//! // <https://devblogs.microsoft.com/oldnewthing/20030905-02/?p=42653>.
35//! let dt = dt + Duration::from_secs(722_805_900);
36//! assert_eq!(dt, datetime!(2002-11-26 19:25:00));
37//! let dt = DateTime::try_from(dt).unwrap();
38//! assert_eq!(
39//! (dt.date().to_raw(), dt.time().to_raw()),
40//! (0b0010_1101_0111_1010, 0b1001_1011_0010_0000)
41//! );
42//!
43//! // The largest MS-DOS date and time.
44//! assert_eq!(
45//! DateTime::from_date_time(date!(2107-12-31), time!(23:59:58)),
46//! Ok(DateTime::MAX)
47//! );
48//! ```
49//!
50//! ## Formatting and printing MS-DOS date and time
51//!
52//! The [`Display`](core::fmt::Display) trait for [`DateTime`] is implemented to
53//! show the value in the well-known [RFC 3339 format]. If you need a different
54//! date and time format, convert [`DateTime`] to a type which represents time
55//! such as [`time::PrimitiveDateTime`].
56//!
57//! ```
58//! use dos_date_time::{
59//! DateTime,
60//! time::{PrimitiveDateTime, format_description::well_known::Rfc2822},
61//! };
62//!
63//! let dt = DateTime::MIN;
64//! assert_eq!(format!("{dt}"), "1980-01-01 00:00:00");
65//!
66//! let dt = PrimitiveDateTime::from(dt)
67//! .as_utc()
68//! .format(&Rfc2822)
69//! .unwrap();
70//! assert_eq!(format!("{dt}"), "Tue, 01 Jan 1980 00:00:00 +0000");
71//! ```
72//!
73//! [MS-DOS date and time]: https://learn.microsoft.com/en-us/windows/win32/sysinfo/ms-dos-date-and-time
74//! [FAT]: https://en.wikipedia.org/wiki/File_Allocation_Table
75//! [ZIP]: https://en.wikipedia.org/wiki/ZIP_(file_format)
76//! [RFC 3339 format]: https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
77
78#![doc(html_root_url = "https://docs.rs/dos-date-time/0.2.0/")]
79#![no_std]
80#![cfg_attr(docsrs, feature(doc_cfg))]
81// Lint levels of rustc.
82#![deny(missing_docs)]
83
84#[cfg(test)]
85#[macro_use]
86extern crate alloc;
87#[cfg(feature = "std")]
88extern crate std;
89
90mod dos_date;
91mod dos_date_time;
92mod dos_time;
93pub mod error;
94
95#[cfg(feature = "chrono")]
96pub use chrono;
97#[cfg(feature = "jiff")]
98pub use jiff;
99pub use time;
100
101pub use crate::{dos_date::Date, dos_date_time::DateTime, dos_time::Time};