Skip to content

Commit

Permalink
fix: weekday calculation bug (#114)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
keyvankhademi authored Apr 16, 2024
1 parent 920df19 commit 00d31d1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pyth-agent"
version = "2.6.0"
version = "2.6.1"
edition = "2021"

[[bin]]
Expand Down
18 changes: 16 additions & 2 deletions src/agent/market_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}
}
}

Expand Down Expand Up @@ -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(())
}
}
Expand Down

0 comments on commit 00d31d1

Please sign in to comment.