Skip to main content

nautilus_core/ffi/
datetime.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Thin FFI wrappers around the date/time conversion utilities in `nautilus-core`.
17//!
18//! The Rust implementation already lives in `crate::datetime`; this module simply exposes the
19//! conversions to C (and, by extension, to Python via Cython) while keeping the behaviour and the
20//! documentation in one place.  Each exported function forwards directly to its Rust counterpart
21//! and therefore inherits the same semantics and safety guarantees.
22
23use std::ffi::c_char;
24
25use crate::{
26    datetime::{unix_nanos_to_iso8601, unix_nanos_to_iso8601_millis},
27    ffi::{abort_on_panic, string::str_to_cstr},
28};
29
30/// Converts a UNIX nanoseconds timestamp to an ISO 8601 (RFC 3339) format C string pointer.
31#[cfg(feature = "ffi")]
32#[unsafe(no_mangle)]
33pub extern "C" fn unix_nanos_to_iso8601_cstr(timestamp_ns: u64) -> *const c_char {
34    abort_on_panic(|| str_to_cstr(&unix_nanos_to_iso8601(timestamp_ns.into())))
35}
36
37/// Converts a UNIX nanoseconds timestamp to an ISO 8601 (RFC 3339) format C string pointer
38/// with millisecond precision.
39#[cfg(feature = "ffi")]
40#[unsafe(no_mangle)]
41pub extern "C" fn unix_nanos_to_iso8601_millis_cstr(timestamp_ns: u64) -> *const c_char {
42    abort_on_panic(|| str_to_cstr(&unix_nanos_to_iso8601_millis(timestamp_ns.into())))
43}
44
45/// Converts seconds to nanoseconds (ns).
46#[cfg(feature = "ffi")]
47#[unsafe(no_mangle)]
48pub extern "C" fn secs_to_nanos(secs: f64) -> u64 {
49    abort_on_panic(|| crate::datetime::secs_to_nanos_unchecked(secs))
50}
51
52/// Converts seconds to milliseconds (ms).
53///
54/// # Panics
55///
56/// Panics if [`crate::datetime::secs_to_millis`] returns an error for `secs`.
57/// The panic is caught by [`abort_on_panic`] and converted into a process abort
58/// across the FFI boundary.
59#[cfg(feature = "ffi")]
60#[unsafe(no_mangle)]
61pub extern "C" fn secs_to_millis(secs: f64) -> u64 {
62    abort_on_panic(|| {
63        crate::datetime::secs_to_millis(secs).expect("secs_to_millis: invalid or overflowing input")
64    })
65}
66
67/// Converts milliseconds (ms) to nanoseconds (ns).
68#[cfg(feature = "ffi")]
69#[unsafe(no_mangle)]
70pub extern "C" fn millis_to_nanos(millis: f64) -> u64 {
71    abort_on_panic(|| crate::datetime::millis_to_nanos_unchecked(millis))
72}
73
74/// Converts microseconds (μs) to nanoseconds (ns).
75#[cfg(feature = "ffi")]
76#[unsafe(no_mangle)]
77pub extern "C" fn micros_to_nanos(micros: f64) -> u64 {
78    abort_on_panic(|| crate::datetime::micros_to_nanos_unchecked(micros))
79}
80
81/// Converts nanoseconds (ns) to seconds.
82#[cfg(feature = "ffi")]
83#[unsafe(no_mangle)]
84pub extern "C" fn nanos_to_secs(nanos: u64) -> f64 {
85    abort_on_panic(|| crate::datetime::nanos_to_secs(nanos))
86}
87
88/// Converts nanoseconds (ns) to milliseconds (ms).
89#[cfg(feature = "ffi")]
90#[unsafe(no_mangle)]
91pub extern "C" fn nanos_to_millis(nanos: u64) -> u64 {
92    abort_on_panic(|| crate::datetime::nanos_to_millis(nanos))
93}
94
95/// Converts nanoseconds (ns) to microseconds (μs).
96#[cfg(feature = "ffi")]
97#[unsafe(no_mangle)]
98pub extern "C" fn nanos_to_micros(nanos: u64) -> u64 {
99    abort_on_panic(|| crate::datetime::nanos_to_micros(nanos))
100}