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
//! NanoTime is a lightweight, high performance Rust library for nanosecond-precision timestamps.
//! It offers support for timestamp generation, arithmetics, comparsion and casting to various string
//! representations, in either local or UTC timezones.
//!
//! NanoTime is optimized for performance - it has no external dependencies, and relies
//! on (g)libc to resolve date information from timestamps. As a result, it has also a limited
//! scope; if you happen to need features such as date/time management, timezone conversion or
//! calendar operations, you're likely better off using [Chrono](https://docs.rs/chrono/latest/chrono/) or similar.
//!
//! # Features
//!
//! NanoTime introduces the [`Timestamp`] struct, which encapsulates a timestamp as nanoseconds
//! since Unix epoch (1970-01-01 00:00 UTC). [`Timestamp`]s can be instantiated by...
//!
//! * Current time: [`Timestamp::now()`]
//! * A standard [`std::time::SystemTime`]: [`Timestamp::from_system_time()`]
//! * A UTC date & time: [`Timestamp::from_utc_date()`]
//! * From a timestamp in milliseconds/nanoseconds: [`Timestamp::new()`], [`Timestamp::from_secs()`], [`Timestamp::from_millis()`], [`Timestamp::from_nanos()`].
//! * From a [`TimestampParts`] struct: [`Timestamp::from_parts()`]
//!
//! [`Timestamp`]s can efficiently be converted into a text representation, with multiple formats supported,
//! and split into date + time [`TimestampParts`].
//!
//! [`Timestamp`] supports comparison, and a limited set of aritmetic operations (difference, for example), which
//! return a [`std::time::Duration`]; this type is re-exported in the module's namespace for convenience.
//!
//! Finally, NanoTime supports a number of convenience tools such as [`sleep()`].
//!
//! # Usage examples
//!
//! ## Initialization
//!
//! ```rust
//! use ntime::Timestamp;
//!
//! let now = Timestamp::now();
//! dbg!(now);
//! ```
//! ```text
//! Timestamp { seconds: 1774369621, nanoseconds: 732000558 }
//! ```
//!
//! ## String conversion and casting
//!
//! ```rust
//! use ntime::{Format, Timestamp};
//!
//! let now = Timestamp::now();
//! println!("nanos since epoch: {}", now.as_nanos());
//! println!("to_string: {}", now.to_string());
//! println!("HTTP/1.1 (UTC): {}", now.as_string(&Format::UtcRFC7231));
//! ```
//! ```text
//! nanos since epoch: 1774369621732000558
//! to_string: 2026-03-24 17:21:01 +0100
//! HTTP/1.1 (UTC): Tue, 24 Mar 2026 16:27:01 UTC
//! ```
//!
//! ## Timestamp Arithmetics
//!
//! ```rust
//! use ntime::Timestamp;
//!
//! let a = Timestamp::from_utc_date(2026, 03, 24, 17, 44, 48, 123, 456).expect("invalid parameters");
//! let b = Timestamp::from_utc_date(2026, 03, 24, 17, 25, 30, 789, 012).expect("invalid parameters");
//! dbg!(a - b);
//! ```
//! ```text
//! a - b = 1157.334000444s
//! ```
//!
//! ## Parts Decomposition
//!
//! ```rust
//! use ntime::Timestamp;
//!
//! let now = Timestamp::now();
//! let now_parts = now.as_utc_parts();
//!
//! println!(
//! "today is {day_name} the {month_day}th of {month_name} {year}, \
//! day {year_day} of the year in UTC",
//! day_name = now_parts.day_name(),
//! month_day = now_parts.month_day,
//! month_name = now_parts.month_name(),
//! year = now_parts.year,
//! year_day = now_parts.year_day
//! );
//! ```
//! ```text
//! today is Mon the 6th of Apr 2026, day 96 of the year in UTC
//! ```
//!
//! ## Sleeping
//!
//! ```rust
//! use ntime;
//!
//! // let's be lazy
//! ntime::sleep_millis(1500);
//! ```
//!
//! # Limitations and Caveats
//!
//! * As noted, NanoTime is not well suited for applications requiring calendar operations, and/or flexible timezone management.
//! * Windows support is currently partial, lacking string conversion support for local timezones.
//!
// Public exported symbols.
pub use Format;
pub use TimestampParts;
pub use *;
pub use Duration;
pub use Timestamp;