Skip to main content

noxtls_platform/
lib.rs

1// Copyright (c) 2019-2026, Argenox Technologies LLC
2// All rights reserved.
3//
4// SPDX-License-Identifier: GPL-2.0-only OR LicenseRef-Argenox-Commercial-License
5//
6// This file is part of the NoxTLS Library.
7//
8// This program is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by the
10// Free Software Foundation; version 2 of the License.
11//
12// Alternatively, this file may be used under the terms of a commercial
13// license from Argenox Technologies LLC.
14//
15// See `noxtls/LICENSE` and `noxtls/LICENSE.md` in this repository for full details.
16// CONTACT: info@argenox.com
17
18#![cfg_attr(not(feature = "std"), no_std)]
19#![forbid(unsafe_code)]
20
21//! Portable hooks for wall time, monotonic timers, and entropy suitable for embedded TLS stacks.
22
23/// Millisecond-resolution opaque timestamp used by DTLS flight and retransmit timers.
24///
25/// Values are produced by platform-specific monotonic sources; callers should only compare or
26/// subtract deltas within the same clock domain.
27pub type MonotonicMillis = u64;
28
29/// Reads whole seconds since the Unix epoch using the system clock when `std` is enabled.
30///
31/// # Arguments
32///
33/// This function takes no parameters.
34///
35/// # Returns
36///
37/// Elapsed whole seconds since 1970-01-01 UTC, or `0` when the system clock is unavailable or yields a pre-epoch time.
38///
39/// # Panics
40///
41/// This function does not panic.
42#[cfg(feature = "std")]
43#[must_use]
44pub fn unix_timestamp_secs() -> u64 {
45    use std::time::{SystemTime, UNIX_EPOCH};
46    SystemTime::now()
47        .duration_since(UNIX_EPOCH)
48        .map_or(0, |d| d.as_secs())
49}