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
25/// # Errors
26///
27/// Returns an error if converting `start` or `end` to `OffsetDateTime` fails.
28pub fn get_date_time_range(start: UnixNanos, end: UnixNanos) -> anyhow::Result<DateTimeRange> {
29    Ok(DateTimeRange::from((
30        OffsetDateTime::from_unix_timestamp_nanos(i128::from(start.as_u64()))?,
31        OffsetDateTime::from_unix_timestamp_nanos(i128::from(end.as_u64()))?,
32    )))
33}
34
35////////////////////////////////////////////////////////////////////////////////
36// Tests
37////////////////////////////////////////////////////////////////////////////////
38#[cfg(test)]
39mod tests {
40    use rstest::*;
41
42    use super::*;
43
44    #[rstest]
45    #[case(
46        UnixNanos::default(),
47        UnixNanos::default(),
48        "DateTimeRange { start: 1970-01-01 0:00:00.0 +00:00:00, end: 1970-01-01 0:00:00.0 +00:00:00 }"
49    )]
50    #[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 }")]
51    fn test_get_date_time_range(
52        #[case] start: UnixNanos,
53        #[case] end: UnixNanos,
54        #[case] range_str: &str,
55    ) {
56        let range = get_date_time_range(start, end).unwrap();
57        assert_eq!(format!("{range:?}"), range_str);
58    }
59}