Skip to content

Commit

Permalink
Add is_within_last_24_hours function
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Dec 17, 2023
1 parent 8eee50d commit 1f11073
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
30 changes: 28 additions & 2 deletions nautilus_core/core/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use chrono::{
Datelike, NaiveDate, SecondsFormat, Weekday,
};

use crate::time::UnixNanos;

pub const MILLISECONDS_IN_SECOND: u64 = 1_000;
pub const NANOSECONDS_IN_SECOND: u64 = 1_000_000_000;
pub const NANOSECONDS_IN_MILLISECOND: u64 = 1_000_000;
Expand Down Expand Up @@ -91,7 +93,7 @@ pub fn unix_nanos_to_iso8601(timestamp_ns: u64) -> String {
dt.to_rfc3339_opts(SecondsFormat::Nanos, true)
}

pub fn last_weekday_nanos(year: i32, month: u32, day: u32) -> Result<u64> {
pub fn last_weekday_nanos(year: i32, month: u32, day: u32) -> Result<UnixNanos> {
let date = NaiveDate::from_ymd_opt(year, month, day).ok_or_else(|| anyhow!("Invalid date"))?;
let current_weekday = date.weekday().number_from_monday();

Expand All @@ -112,7 +114,17 @@ pub fn last_weekday_nanos(year: i32, month: u32, day: u32) -> Result<u64> {

Ok(unix_timestamp_ns
.timestamp_nanos_opt()
.ok_or_else(|| anyhow!("Failed `timestamp_nanos_opt`"))? as u64)
.ok_or_else(|| anyhow!("Failed `timestamp_nanos_opt`"))? as UnixNanos)
}

pub fn is_within_last_24_hours(timestamp_ns: UnixNanos) -> Result<bool> {
let seconds = timestamp_ns / NANOSECONDS_IN_SECOND;
let nanoseconds = (timestamp_ns % NANOSECONDS_IN_SECOND) as u32;
let timestamp = DateTime::from_timestamp(seconds as i64, nanoseconds)
.ok_or_else(|| anyhow!("Invalid timestamp {timestamp_ns}"))?;
let now = Utc::now();

Ok(now.signed_duration_since(timestamp) <= chrono::Duration::days(1))
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -240,4 +252,18 @@ mod tests {
let result = last_weekday_nanos(9999, 12, 31);
assert!(result.is_err());
}

#[rstest]
fn test_is_within_last_24_hours_when_now() {
let now_ns = Utc::now().timestamp_nanos_opt().unwrap();
assert!(is_within_last_24_hours(now_ns as UnixNanos).unwrap());
}

#[rstest]
fn test_is_within_last_24_hours_when_two_days_ago() {
let past_ns = (Utc::now() - chrono::Duration::days(2))
.timestamp_nanos_opt()
.unwrap();
assert!(!is_within_last_24_hours(past_ns as UnixNanos).unwrap());
}
}
9 changes: 7 additions & 2 deletions nautilus_core/core/src/python/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use pyo3::prelude::*;

use super::to_pyvalue_err;
use crate::datetime::{
last_weekday_nanos, micros_to_nanos, millis_to_nanos, nanos_to_micros, nanos_to_millis,
nanos_to_secs, secs_to_millis, secs_to_nanos, unix_nanos_to_iso8601,
is_within_last_24_hours, last_weekday_nanos, micros_to_nanos, millis_to_nanos, nanos_to_micros,
nanos_to_millis, nanos_to_secs, secs_to_millis, secs_to_nanos, unix_nanos_to_iso8601,
};

#[must_use]
Expand Down Expand Up @@ -73,3 +73,8 @@ pub fn py_unix_nanos_to_iso8601(timestamp_ns: u64) -> String {
pub fn py_last_weekday_nanos(year: i32, month: u32, day: u32) -> PyResult<u64> {
last_weekday_nanos(year, month, day).map_err(to_pyvalue_err)
}

#[pyfunction(name = "is_within_last_24_hours")]
pub fn py_is_within_last_24_hours(timestamp_ns: u64) -> PyResult<bool> {
is_within_last_24_hours(timestamp_ns).map_err(to_pyvalue_err)
}
1 change: 1 addition & 0 deletions nautilus_core/core/src/python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ pub fn core(_: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(datetime::py_nanos_to_micros, m)?)?;
m.add_function(wrap_pyfunction!(datetime::py_unix_nanos_to_iso8601, m)?)?;
m.add_function(wrap_pyfunction!(datetime::py_last_weekday_nanos, m)?)?;
m.add_function(wrap_pyfunction!(datetime::py_is_within_last_24_hours, m)?)?;
Ok(())
}

0 comments on commit 1f11073

Please sign in to comment.