Expand description
Macros for easy unit conversion and
Dt construction.
Each macro expands to a call on an equivalent Dt
method.
§Overview
§Unit → attoseconds
Returns total attoseconds as i128.
§Attoseconds → unit
Returns a whole-unit count as i128, or a lossy
Real for the _f forms.
as_fs!as_ps!as_ns!as_us!as_ms!as_sec!as_sec_f!as_mins!as_hours!as_days!as_days_f!as_weeks!
§Instant / duration
Returns a Dt.
dt!from_sec!from_sec_f!from_ms!from_us!from_ns!from_ps!from_fs!from_days_f!from_ymd!from_jd!from_jd_f!from_mjd!from_mjd_f!
§Import paths
All macros can be imported from this module:
use deep_time::macros::{from_ns, ms, sec};These are also available at the crate root via use deep_time::{…}:
The rest are available only under deep_time::macros.
use deep_time::macros::* brings in every macro listed above.
§How they work
- Attosecond storage unit. Forward converters such as
ms!andsec!return total attoseconds asi128. Reverse converters such asas_ms!andas_sec!take total attoseconds and return a count in the named unit. - Truncation toward zero. Integer reverse converters use ordinary
i128division (attos / unit). Any leftover below one whole unit is dropped, and the result moves toward zero—not toward −∞. For example, −0.5 s as whole seconds is0, and −1.5 s is-1. The floating reverse converters (as_sec_f!,as_days_f!) are lossyRealcasts instead. - Signed remainders on constructors. Macros such as
from_sec!andfrom_ns!accept an optional fractional remainder in attoseconds. Both signs of the remainder are valid; the total iswhole × unit + frac(with saturating arithmetic on the underlying method). The same total can often be written with a signed remainder or as a floor-style split with a non-negative remainder. - Scale labels vs conversion. Most
from_*macros only set the returnedDt’sscale/targetfields; they do not convert the attosecond count between time scales.from_ymd!,from_jd!,from_jd_f!,from_mjd!, andfrom_mjd_f!are the exceptions: they build aDton TAI (converting from theon=scale when needed) and settargetfromon=, as documented on each macro. constcontexts. Expansions callconstmethods onDt, so these macros work inconstcontexts where the underlying method does.
§Examples
use deep_time::{Dt, Scale, dt, from_ymd, ms, ns};
use deep_time::macros::{as_sec, from_sec, sec};
// Unit helpers avoid hand-counting zeros in attosecond literals.
assert_eq!(ms!(300), Dt::ms_to_attos(300));
assert_eq!(ns!(1), 1_000_000_000);
// Whole seconds + sub-second remainder (remainder is attoseconds).
let a = from_sec!(1, ms!(300));
assert_eq!(a, dt!(1_300_000_000_000_000_000));
// Reverse conversion truncates toward zero.
assert_eq!(as_sec!(sec!(1) + ms!(900)), 1);
assert_eq!(as_sec!(-ms!(500)), 0);
assert_eq!(as_sec!(-sec!(1) - ms!(500)), -1);
// Calendar construction: builds a Dt, converting civil time to TAI.
assert_eq!(from_ymd!(2000, 1, 1; 12, on=Scale::TAI), Dt::ZERO);See each macro’s own documentation for accepted forms, defaults, and
links to the corresponding Dt methods.
Macros§
- as_days
- Converts total attoseconds (
i128) → whole days (i128). - as_
days_ f - Converts total attoseconds (
i128) → lossy float days (Real). - as_fs
- Converts total attoseconds (
i128) → whole femtoseconds (i128). - as_
hours - Converts total attoseconds (
i128) → whole hours (i128). - as_mins
- Converts total attoseconds (
i128) → whole minutes (i128). - as_ms
- Converts total attoseconds (
i128) → whole milliseconds (i128). - as_ns
- Converts total attoseconds (
i128) → whole nanoseconds (i128). - as_ps
- Converts total attoseconds (
i128) → whole picoseconds (i128). - as_sec
- Converts total attoseconds (
i128) → whole seconds (i128). - as_
sec_ f - Converts total attoseconds (
i128) → lossy float seconds (Real). - as_us
- Converts total attoseconds (
i128) → whole microseconds (i128). - as_
weeks - Converts total attoseconds (
i128) → whole weeks (i128). - days
- Converts whole days (
i128) to total attoseconds (i128). - days_f
- Converts a floating-point day count (
Real) to total attoseconds (i128). - dt
- Builds a
Dtfrom total attoseconds with optional scale labels. - from_
days_ f - Builds a
Dtfrom a floating-point day count with optional scale labels. - from_fs
- Builds a
Dtfrom whole femtoseconds and an optional signed fractional remainder in attoseconds. - from_jd
- Builds a TAI
Dtfrom a Julian Date (whole days plus optional attosecond remainder). - from_
jd_ f - Builds a TAI
Dtfrom a floating-point Julian Date. - from_
mjd - Builds a TAI
Dtfrom a Modified Julian Date (whole days plus optional attosecond remainder). - from_
mjd_ f - Builds a TAI
Dtfrom a floating-point Modified Julian Date. - from_ms
- Builds a
Dtfrom whole milliseconds and an optional signed fractional remainder in attoseconds. - from_ns
- Builds a
Dtfrom whole nanoseconds and an optional signed fractional remainder in attoseconds. - from_ps
- Builds a
Dtfrom whole picoseconds and an optional signed fractional remainder in attoseconds. - from_
sec - Builds a
Dtfrom whole seconds and an optional signed sub-second remainder (attoseconds). - from_
sec_ f - Builds a
Dtfrom a floating-point seconds count with optional scale labels. - from_us
- Builds a
Dtfrom whole microseconds and an optional signed fractional remainder in attoseconds. - from_
ymd - Builds a
Dtfrom a Gregorian calendar date and optional time. - fs
- Converts whole femtoseconds (
i128) to total attoseconds (i128). - hours
- Converts whole hours (
i128) to total attoseconds (i128). - mins
- Converts whole minutes (
i128) to total attoseconds (i128). - ms
- Converts whole milliseconds (
i128) to total attoseconds (i128). - ns
- Converts whole nanoseconds (
i128) to total attoseconds (i128). - ps
- Converts whole picoseconds (
i128) to total attoseconds (i128). - sec
- Converts whole seconds (
i128) to total attoseconds (i128). - sec_f
- Converts a floating-point second count (
Real) to total attoseconds (i128). - us
- Converts whole microseconds (
i128) to total attoseconds (i128). - weeks
- Converts whole weeks (
i128) to total attoseconds (i128).