From 00d31d16678ed2bac2421abb82146bda45d30c4c Mon Sep 17 00:00:00 2001 From: Keyvan Khademi Date: Tue, 16 Apr 2024 11:48:59 -0700 Subject: [PATCH] fix: weekday calculation bug (#114) * fix: fix weekday calculation bug * chore: increase pyth agent version * feat: add test to check sunday * feat: silently ignore weekly_schedule out of bound error * feat: add test for monday --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/agent/market_schedule.rs | 18 ++++++++++++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cd1f188..16421fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3270,7 +3270,7 @@ dependencies = [ [[package]] name = "pyth-agent" -version = "2.6.0" +version = "2.6.1" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index 8c28965..fc99bf4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pyth-agent" -version = "2.6.0" +version = "2.6.1" edition = "2021" [[bin]] diff --git a/src/agent/market_schedule.rs b/src/agent/market_schedule.rs index e55c799..f786e1b 100644 --- a/src/agent/market_schedule.rs +++ b/src/agent/market_schedule.rs @@ -96,7 +96,7 @@ impl MarketSchedule { let month = when_local.date_naive().month0() + 1; let day = when_local.date_naive().day0() + 1; let time = when_local.time(); - let weekday = when_local.weekday().number_from_monday().to_usize(); + let weekday0 = when_local.weekday().number_from_monday().to_usize() - 1; for holiday in &self.holidays { // Check if the day matches @@ -105,7 +105,12 @@ impl MarketSchedule { } } - self.weekly_schedule[weekday].can_publish_at(time) + let day_schedule = self.weekly_schedule.get(weekday0); + + match day_schedule { + Some(day_schedule) => day_schedule.can_publish_at(time), + None => false, + } } } @@ -462,6 +467,15 @@ mod tests { // Date 2400 range assert!(market_schedule .can_publish_at(&NaiveDateTime::parse_from_str("2023-12-31 23:59", format)?.and_utc())); + + // Sunday + assert!(market_schedule + .can_publish_at(&NaiveDateTime::parse_from_str("2024-04-14 12:00", format)?.and_utc())); + + // Monday + assert!(market_schedule + .can_publish_at(&NaiveDateTime::parse_from_str("2024-04-15 12:00", format)?.and_utc())); + Ok(()) } }