nautilus_databento/
common.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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//! Common functions to support Databento adapter operations.
17
18use databento::historical::DateTimeRange;
19use nautilus_core::UnixNanos;
20use time::OffsetDateTime;
21
22pub const DATABENTO: &str = "DATABENTO";
23pub const ALL_SYMBOLS: &str = "ALL_SYMBOLS";
24
25pub fn get_date_time_range(start: UnixNanos, end: UnixNanos) -> anyhow::Result<DateTimeRange> {
26    Ok(DateTimeRange::from((
27        OffsetDateTime::from_unix_timestamp_nanos(i128::from(start.as_u64()))?,
28        OffsetDateTime::from_unix_timestamp_nanos(i128::from(end.as_u64()))?,
29    )))
30}
31
32////////////////////////////////////////////////////////////////////////////////
33// Tests
34////////////////////////////////////////////////////////////////////////////////
35#[cfg(test)]
36mod tests {
37    use rstest::*;
38
39    use super::*;
40
41    #[rstest]
42    #[case(
43        UnixNanos::default(),
44        UnixNanos::default(),
45        "DateTimeRange { start: 1970-01-01 0:00:00.0 +00:00:00, end: 1970-01-01 0:00:00.0 +00:00:00 }"
46    )]
47    #[case(UnixNanos::default(), 1_000_000_000.into(), "DateTimeRange { start: 1970-01-01 0:00:00.0 +00:00:00, end: 1970-01-01 0:00:01.0 +00:00:00 }")]
48    fn test_get_date_time_range(
49        #[case] start: UnixNanos,
50        #[case] end: UnixNanos,
51        #[case] range_str: &str,
52    ) {
53        let range = get_date_time_range(start, end).unwrap();
54        assert_eq!(format!("{range:?}"), range_str);
55    }
56}