docs/book/src/blockchain-development/time.md
The std::time library provides utilities for handling time durations and timestamps in Sway smart contracts.
Represents a span of time in seconds.
{{#include ../../../../examples/time/src/main.sw:create_durations}}
Time std::time library supports conversion between different time scales such as seconds, minutes, hours, days, and weeks.
{{#include ../../../../examples/time/src/main.sw:convert_durations}}
The std::time supports operations on the Duration type.
{{#include ../../../../examples/time/src/main.sw:duration_operations}}
Represents a UNIX timestamp (seconds since Jan 1, 1970).
There are 3 major ways to create a new timestamp.
{{#include ../../../../examples/time/src/main.sw:create_timestamps}}
Operations on the Time type are supported with conjunction of the Duration type.
{{#include ../../../../examples/time/src/main.sw:time_operations}}
The Fuel VM internally uses TAI64 time. Conversions between UNIX and TAI64 are maintained with the Time type.
{{#include ../../../../examples/time/src/main.sw:tai64_conversion}}
The library uses:
const TAI_64_CONVERTER: u64 = 10 + (1 << 62);
(1 << 62) (0x4000000000000000) marks value as TAI64. 10 accounts for initial TAI-UTC offset in 1970.
Conversion formulas:
UNIX → TAI64: tai64 = unix + TAI_64_CONVERTER
TAI64 → UNIX: unix = tai64 - TAI_64_CONVERTER
| Feature | TAI64 | UNIX |
|---|---|---|
| Epoch | 1970-01-01 00:00:00 TAI | 1970-01-01 00:00:00 UTC |
| Leap Seconds | No leap seconds | Includes leap seconds |
| Stability | Continuous time scale | Discontinuous adjustments |
| Value Range | (1 << 62) + offset (10s) | Seconds since epoch |
Duration for time spans instead of raw secondsTimeError results from duration_since() and elapsed()Time::block() for historical time comparisonsSECOND, HOUR, etc.) for readability