Overview
The Unix time conversion API provides portable utilities for working with Unix timestamps (seconds/milliseconds since epoch). All conversions are relative to the Unix epoch: 1970-01-01 00:00:00 UTC. These utilities are designed for:- Parsing file formats storing Unix timestamps
- Logging and telemetry systems
- Binary analysis and forensics
- Cross-platform timestamp handling
Type Aliases
Wall-clock time suitable for Unix epoch conversions
High-resolution monotonic clock for precise measurements (see Stopwatch)
Functions
Creating Time Points from Unix Timestamps
from_unix_seconds()
Creates a time_point from seconds since Unix epoch.
Number of seconds since 1970-01-01 00:00:00 UTC
A time point representing the specified Unix timestamp
from_unix_millis()
Creates a time_point from milliseconds since Unix epoch.
Number of milliseconds since 1970-01-01 00:00:00 UTC
A time point representing the specified Unix timestamp
Converting Time Points to Unix Timestamps
to_unix_seconds()
Converts a time_point to seconds since Unix epoch.
The time point to convert
Number of seconds since Unix epoch (truncated toward zero)
to_unix_millis()
Converts a time_point to milliseconds since Unix epoch.
The time point to convert
Number of milliseconds since Unix epoch (truncated toward zero)
Getting Current Unix Time
unix_seconds_now()
Returns the current time as seconds since Unix epoch.
Current Unix timestamp in seconds
unix_millis_now()
Returns the current time as milliseconds since Unix epoch.
Current Unix timestamp in milliseconds
Usage Examples
Creating Time Points from Timestamps
Converting Time Points to Timestamps
Getting Current Unix Time
Parsing Timestamps from Binary Data
Timestamp Arithmetic
Logging with Timestamps
Round-Trip Conversion
Precision and Truncation
All conversion functions use
std::chrono::duration_cast, which truncates toward zero. When converting from higher precision to lower precision, fractional parts are discarded.Design Characteristics
| Property | Description |
|---|---|
| UTC only | No timezone abstraction or conversion |
| No calendar | No date formatting or parsing |
| Header-only | No compilation or linking required |
| Portable | Works on 32-bit and 64-bit systems |
| constexpr | Conversion functions usable at compile-time |
| ABI-transparent | Direct use of standard library types |
Safety Considerations
Common Patterns
Measuring Duration Between Events
Checking if Timestamp is Recent
Intended Use Cases
- Parsing binary file formats with embedded timestamps
- Network protocol timestamp handling
- Log file timestamp generation and parsing
- Cache expiration tracking
- Binary analysis and forensics
- Cross-platform timestamp serialization
- Systems programming and tooling
Related APIs
- Stopwatch - High-resolution elapsed time measurement
sys_clock- Standard library system clockstd::chrono::time_point- Standard time point representation