|
| 1 | +#pragma once |
| 2 | +// Copyright 2026 Davide Faconti |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +// The absolute time spine: a chrono vocabulary that sits one lossless step above |
| 6 | +// the int64-ns PJ::Timestamp. Timestamp stays the storage/ABI/wire currency; |
| 7 | +// these types are how layered code names absolute time without re-deriving the |
| 8 | +// epoch or hand-rolling 1e9 conversions. Two concepts, kept un-mixable by the |
| 9 | +// type system: |
| 10 | +// * Timepoint — an absolute instant (sys_time<nanoseconds>, Unix epoch). |
| 11 | +// * Duration — a span (nanoseconds); Timepoint + Timepoint won't compile. |
| 12 | +// |
| 13 | +// fromRaw()/toRaw() are the only sanctioned crossing between the int64 spine and |
| 14 | +// the chrono world: lift a Timestamp into a Timepoint to compute with it, lower |
| 15 | +// it back immediately before a storage/ABI/wire boundary. Display-relative time |
| 16 | +// (the Qwt-axis / PlaybackEngine coordinate) is a separate, app-level vocabulary |
| 17 | +// that builds on this one — it lives in pj_runtime, not here. |
| 18 | + |
| 19 | +#include <chrono> |
| 20 | + |
| 21 | +#include "pj_base/types.hpp" // PJ::Timestamp, PJ::Range |
| 22 | + |
| 23 | +namespace PJ { |
| 24 | + |
| 25 | +/// An ABSOLUTE wall-clock instant, nanosecond precision, Unix epoch. Lossless |
| 26 | +/// mirror of PJ::Timestamp (C++20 guarantees system_clock's epoch == Unix epoch). |
| 27 | +using Timepoint = std::chrono::sys_time<std::chrono::nanoseconds>; |
| 28 | + |
| 29 | +/// A length of time (a span, not a point): retention windows, lifetimes, deltas. |
| 30 | +using Duration = std::chrono::nanoseconds; |
| 31 | + |
| 32 | +/// Lift an int64-ns PJ::Timestamp out of the spine into a Timepoint. |
| 33 | +[[nodiscard]] constexpr Timepoint fromRaw(Timestamp ns) noexcept { |
| 34 | + return Timepoint{Duration{ns}}; |
| 35 | +} |
| 36 | + |
| 37 | +/// Lower a Timepoint back to the int64-ns spine, immediately before crossing a |
| 38 | +/// storage/ABI boundary (DataWriter, the C-ABI trampolines, the codecs). |
| 39 | +[[nodiscard]] constexpr Timestamp toRaw(Timepoint t) noexcept { |
| 40 | + return t.time_since_epoch().count(); |
| 41 | +} |
| 42 | + |
| 43 | +/// Lift an int64 interval into an absolute Timepoint interval (reuses PJ::Range, |
| 44 | +/// never std::pair). |
| 45 | +[[nodiscard]] constexpr Range<Timepoint> fromRawRange(const Range<Timestamp>& r) noexcept { |
| 46 | + return {fromRaw(r.min), fromRaw(r.max)}; |
| 47 | +} |
| 48 | + |
| 49 | +} // namespace PJ |
0 commit comments