nautilus_core/ffi/
datetime.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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
16use std::ffi::c_char;
17
18use crate::{
19    datetime::{unix_nanos_to_iso8601, unix_nanos_to_iso8601_millis},
20    ffi::string::str_to_cstr,
21};
22
23/// Converts a UNIX nanoseconds timestamp to an ISO 8601 (RFC 3339) format C string pointer.
24#[cfg(feature = "ffi")]
25#[unsafe(no_mangle)]
26pub extern "C" fn unix_nanos_to_iso8601_cstr(timestamp_ns: u64) -> *const c_char {
27    str_to_cstr(&unix_nanos_to_iso8601(timestamp_ns.into()))
28}
29
30/// Converts a UNIX nanoseconds timestamp to an ISO 8601 (RFC 3339) format C string pointer
31/// with millisecond precision.
32#[cfg(feature = "ffi")]
33#[unsafe(no_mangle)]
34pub extern "C" fn unix_nanos_to_iso8601_millis_cstr(timestamp_ns: u64) -> *const c_char {
35    str_to_cstr(&unix_nanos_to_iso8601_millis(timestamp_ns.into()))
36}
37/// Converts seconds to nanoseconds (ns).
38#[cfg(feature = "ffi")]
39#[unsafe(no_mangle)]
40pub extern "C" fn secs_to_nanos(secs: f64) -> u64 {
41    crate::datetime::secs_to_nanos(secs)
42}
43
44/// Converts seconds to milliseconds (ms).
45#[cfg(feature = "ffi")]
46#[unsafe(no_mangle)]
47pub extern "C" fn secs_to_millis(secs: f64) -> u64 {
48    crate::datetime::secs_to_millis(secs)
49}
50
51/// Converts milliseconds (ms) to nanoseconds (ns).
52#[cfg(feature = "ffi")]
53#[unsafe(no_mangle)]
54pub extern "C" fn millis_to_nanos(millis: f64) -> u64 {
55    crate::datetime::millis_to_nanos(millis)
56}
57
58/// Converts microseconds (μs) to nanoseconds (ns).
59#[cfg(feature = "ffi")]
60#[unsafe(no_mangle)]
61pub extern "C" fn micros_to_nanos(micros: f64) -> u64 {
62    crate::datetime::micros_to_nanos(micros)
63}
64
65/// Converts nanoseconds (ns) to seconds.
66#[cfg(feature = "ffi")]
67#[unsafe(no_mangle)]
68pub extern "C" fn nanos_to_secs(nanos: u64) -> f64 {
69    crate::datetime::nanos_to_secs(nanos)
70}
71
72/// Converts nanoseconds (ns) to milliseconds (ms).
73#[cfg(feature = "ffi")]
74#[unsafe(no_mangle)]
75pub const extern "C" fn nanos_to_millis(nanos: u64) -> u64 {
76    crate::datetime::nanos_to_millis(nanos)
77}
78
79/// Converts nanoseconds (ns) to microseconds (μs).
80#[cfg(feature = "ffi")]
81#[unsafe(no_mangle)]
82pub const extern "C" fn nanos_to_micros(nanos: u64) -> u64 {
83    crate::datetime::nanos_to_micros(nanos)
84}