⏱ nanotime
A minimal, zero-dependency time utility crate for Rust CLI applications.
Part of the nano crate family — minimal, zero-dependency building blocks for CLI apps in Rust:
- nanocolor — terminal colors and styles
- nanospinner — terminal spinners
- nanoprogress — progress bars
- nanolog — minimal logger
- nanotime — time utilities
Local and UTC time retrieval, nanosecond-precision timestamps, human-readable formatting, relative time strings, and lightweight elapsed duration measurement — no heavy crates, no transitive dependencies.
Motivation
Most Rust time crates (like chrono or time) are feature-rich but pull in dependencies or offer far more than you need. If all you want is the current time and a simple way to measure durations, those crates are overkill.
nanotime solves this by providing the essentials and nothing more:
- Zero external dependencies (only
std+ raw FFI) - Nanosecond-precision timestamps with millisecond and microsecond accessors
- Local time via platform FFI (
clock_gettimeon Unix,GetLocalTimeon Windows) - UTC time via
SystemTime+ manual calendar arithmetic - Elapsed duration measurement via
std::time::Instant - Human-readable formatting out of the box
- Validated constructor — no invalid dates
- Implements
Ord,Hash,Eq,Clone,Copy,Debug
Comparison
nanotime is intentionally minimal. If you need timezones, parsing, arithmetic, or no_std, use chrono or time — they're great crates.
nanotime is for when you just want to know what time it is and how long something took.
| Feature | nanotime |
chrono |
time |
|---|---|---|---|
| Zero dependencies | ✓ | ✗ | ✗ |
| Current local time | ✓ | ✓ | ✓ |
| Current UTC time | ✓ | ✓ | ✓ |
| Nanosecond precision | ✓ | ✓ | ✓ |
| Elapsed measurement | ✓ | ✗ | ✗ |
| Relative time ("3s ago") | ✓ | ✓ | ✓ |
| Human-readable display | ✓ | ✓ | ✓ |
| Timezone conversion | ✗ | ✓ | ✓ |
| Date/time parsing | ✗ | ✓ | ✓ |
| Date/time arithmetic | ✗ | ✓ | ✓ |
no_std support |
✗ | ✗ | ✓ |
| Single file | ✓ | ✗ | ✗ |
Quick Start
[]
= "0.1.0"
use ;
Usage
Get the current time
use NanoTime;
let local = now; // local time via platform FFI
let utc = now_utc; // UTC via SystemTime
println!; // "14:32:07.042"
println!; // "2026-02-22"
println!; // "2026-02-22 14:32:07.042"
Create timestamps
use NanoTime;
// Validated constructor (returns None for invalid dates)
let t = new.unwrap;
// From epoch values
let from_secs = from_epoch;
let from_ms = from_epoch_ms;
let from_us = from_epoch_us;
let from_ns = from_epoch_nanos;
// Invalid dates return None
assert!; // not a leap year
assert!; // invalid month
Access fields
use NanoTime;
let now = now;
println!;
println!;
// Sub-second accessors (derived from nanosecond field)
println!; // 0–999,999,999
println!; // 0–999,999
println!; // 0–999
Formatting
use NanoTime;
let t = new.unwrap;
// Default formatting includes milliseconds
println!; // "14:30:05.123"
println!; // "2026-02-22"
println!; // "2026-02-22 14:30:05.123"
// Flexible precision with datetime_fmt (0–9 fractional digits)
println!; // "2026-02-22 14:30:05"
println!; // "2026-02-22 14:30:05.123"
println!; // "2026-02-22 14:30:05.123456"
println!; // "2026-02-22 14:30:05.123456789"
Epoch conversions
use NanoTime;
let t = new.unwrap;
// Convert to epoch at any granularity
let secs = t.to_epoch_secs; // u64
let ms = t.to_epoch_ms; // u64
let us = t.to_epoch_us; // u128
let nanos = t.to_epoch_nanos; // u128
Time differences
use NanoTime;
let a = from_epoch;
let b = from_epoch;
// At any granularity
println!; // 100
println!; // 100000
println!; // 100000000
println!; // 100000000000
Relative time
use NanoTime;
let past = new.unwrap;
println!; // e.g. "419d ago"
let a = new.unwrap;
let b = new.unwrap;
println!; // "5m ago"
println!; // "in 5m"
Buckets: just now, Xs, Xm, Xh, Xd — with "ago" or "in" prefix/suffix for direction.
Measure elapsed time
use Elapsed;
let timer = start;
// ... do some work ...
println!; // "42ms" or "1.23s"
println!; // "0.042"
println!; // "42"
The Display impl automatically picks the right unit:
- Under 1 second:
Xms(e.g.450ms) - 1 second or more:
X.XXs(e.g.1.23s)
API Reference
NanoTime
Constructors
| Method | Returns | Description |
|---|---|---|
NanoTime::new(year, month, day, hour, minute, second, nanosecond) |
Option<NanoTime> |
Validated constructor. Returns None for invalid dates. |
NanoTime::now() |
NanoTime |
Current local time via platform FFI |
NanoTime::now_utc() |
NanoTime |
Current UTC time via SystemTime |
NanoTime::from_epoch(secs) |
NanoTime |
From Unix epoch seconds |
NanoTime::from_epoch_ms(ms) |
NanoTime |
From Unix epoch milliseconds |
NanoTime::from_epoch_us(us) |
NanoTime |
From Unix epoch microseconds |
NanoTime::from_epoch_nanos(nanos) |
NanoTime |
From Unix epoch nanoseconds |
Getters
| Method | Returns | Description |
|---|---|---|
.year() |
u16 |
Calendar year |
.month() |
u8 |
Month (1–12) |
.day() |
u8 |
Day of month (1–31) |
.hour() |
u8 |
Hour (0–23) |
.minute() |
u8 |
Minute (0–59) |
.second() |
u8 |
Second (0–59) |
.nanosecond() |
u32 |
Nanosecond (0–999,999,999) |
.millisecond() |
u16 |
Derived: nanosecond / 1,000,000 |
.microsecond() |
u32 |
Derived: nanosecond / 1,000 |
Formatting
| Method | Returns | Example |
|---|---|---|
Display (format!) |
— | 14:30:05.123 |
.date() |
String |
2026-02-22 |
.datetime() |
String |
2026-02-22 14:30:05.123 |
.datetime_fmt(precision) |
String |
2026-02-22 14:30:05.123456 (precision=6) |
Epoch Conversions
| Method | Returns | Description |
|---|---|---|
.to_epoch_secs() |
u64 |
Unix epoch seconds |
.to_epoch_ms() |
u64 |
Unix epoch milliseconds |
.to_epoch_us() |
u128 |
Unix epoch microseconds |
.to_epoch_nanos() |
u128 |
Unix epoch nanoseconds |
Differences
| Method | Returns | Description |
|---|---|---|
.diff_secs(&other) |
i64 |
Signed difference in seconds |
.diff_ms(&other) |
i64 |
Signed difference in milliseconds |
.diff_us(&other) |
i128 |
Signed difference in microseconds |
.diff_nanos(&other) |
i128 |
Signed difference in nanoseconds |
Relative Time
| Method | Returns | Description |
|---|---|---|
.relative_to(&other) |
String |
e.g. "3s ago", "in 2h" |
.ago() |
String |
Relative to now (UTC) |
Elapsed
| Method | Returns | Description |
|---|---|---|
Elapsed::start() |
Elapsed |
Capture current instant |
.elapsed_secs() |
f64 |
Elapsed seconds |
.elapsed_ms() |
u128 |
Elapsed milliseconds |
.elapsed_us() |
u128 |
Elapsed microseconds |
.elapsed_nanos() |
u128 |
Elapsed nanoseconds |
Display |
— | Xms or X.XXs |
Contributing
Contributions are welcome. To get started:
- Fork the repository
- Create a feature branch (
git checkout -b my-feature) - Make your changes
- Run the tests:
cargo test - Submit a pull request
Please keep changes minimal and focused. This crate's goal is to stay small and dependency-free.
License
This project is licensed under the MIT License.