exc_binance/types/adaptations/
mod.rs1use std::ops::Bound;
2
3use exc_core::ExchangeError;
4use time::OffsetDateTime;
5
6mod book;
7mod candle;
8mod instrument;
9mod trade;
10mod trading;
11mod utils;
12
13pub(crate) fn from_timestamp(ts: i64) -> Result<OffsetDateTime, ExchangeError> {
14 OffsetDateTime::from_unix_timestamp_nanos((ts as i128) * 1_000_000)
15 .map_err(|err| ExchangeError::Other(anyhow!("parse timestamp error: {err}")))
16}
17
18pub(crate) fn to_timestamp(ts: &OffsetDateTime) -> Result<i64, ExchangeError> {
19 let ts = ts.unix_timestamp_nanos() / 1_000_000;
20 if ts > (i64::MAX as i128) {
21 return Err(ExchangeError::Other(anyhow!("datetime too big: {ts}")));
22 }
23 Ok(ts as i64)
24}
25
26pub(crate) fn start_bound_to_timestamp(
27 start: Bound<&OffsetDateTime>,
28) -> Result<Option<i64>, ExchangeError> {
29 match start {
30 Bound::Included(ts) => Ok(Some(to_timestamp(ts)?)),
31 Bound::Excluded(ts) => Ok(Some(to_timestamp(&(*ts + time::Duration::SECOND))?)),
32 Bound::Unbounded => Ok(None),
33 }
34}
35
36pub(crate) fn end_bound_to_timestamp(
37 end: Bound<&OffsetDateTime>,
38) -> Result<Option<i64>, ExchangeError> {
39 match end {
40 Bound::Included(ts) => Ok(Some(to_timestamp(ts)?)),
41 Bound::Excluded(ts) => Ok(Some(to_timestamp(&(*ts - time::Duration::SECOND))?)),
42 Bound::Unbounded => Ok(None),
43 }
44}