diff --git a/game/src/sandbox/mod.rs b/game/src/sandbox/mod.rs index 471d6050df..47e7f12aec 100644 --- a/game/src/sandbox/mod.rs +++ b/game/src/sandbox/mod.rs @@ -869,7 +869,7 @@ fn mouseover_unzoomed_agent_circle(ctx: &mut EventCtx, app: &mut App) { } fn is_daytime(app: &App) -> bool { - let hours = app.primary.sim.time().get_parts().0 % 24; + let hours = app.primary.sim.time().get_hours() % 24; hours >= 6 && hours < 18 } diff --git a/geom/src/time.rs b/geom/src/time.rs index 1c6dd38122..f1de19dddb 100644 --- a/geom/src/time.rs +++ b/geom/src/time.rs @@ -37,7 +37,7 @@ impl Time { } /// (hours, minutes, seconds, centiseconds) - pub fn get_parts(self) -> (usize, usize, usize, usize) { + fn get_parts(self) -> (usize, usize, usize, usize) { let mut remainder = self.0; let hours = (remainder / 3600.0).floor(); remainder -= hours * 3600.0; @@ -54,14 +54,9 @@ impl Time { centis as usize, ) } - /// Rounded up + /// Rounded down. 6:59:00 is hour 6. pub fn get_hours(self) -> usize { - let (hr, min, sec, cs) = self.get_parts(); - if min > 0 || sec > 0 || cs > 0 { - hr + 1 - } else { - hr - } + self.get_parts().0 } pub fn ampm_tostring(self) -> String { @@ -210,4 +205,17 @@ mod tests { Time::parse("07:30:05").unwrap() ); } + + #[test] + fn get_hours() { + assert_eq!((Time::START_OF_DAY + Duration::hours(6)).get_hours(), 6); + assert_eq!( + (Time::START_OF_DAY + Duration::hours(6) + Duration::seconds(1.0)).get_hours(), + 6 + ); + assert_eq!( + (Time::START_OF_DAY + Duration::hours(6) + Duration::minutes(59)).get_hours(), + 6 + ); + } } diff --git a/sim/src/analytics.rs b/sim/src/analytics.rs index 9e057eba13..a582811a7d 100644 --- a/sim/src/analytics.rs +++ b/sim/src/analytics.rs @@ -535,8 +535,10 @@ impl TimeSeriesCount { } } - let hour = time.get_parts().0; - *self.counts.entry((id, agent_type, hour)).or_insert(0) += count; + *self + .counts + .entry((id, agent_type, time.get_hours())) + .or_insert(0) += count; } pub fn total_for(&self, id: X) -> usize { @@ -561,8 +563,7 @@ impl TimeSeriesCount { pub fn total_for_by_time(&self, id: X, now: Time) -> usize { let mut cnt = 0; for agent_type in AgentType::all() { - // TODO Off-by-one? - for hour in 0..now.get_hours() { + for hour in 0..=now.get_hours() { cnt += self .counts .get(&(id.clone(), agent_type, hour)) diff --git a/sim/src/cap.rs b/sim/src/cap.rs index b88b43a0af..14f4442bc7 100644 --- a/sim/src/cap.rs +++ b/sim/src/cap.rs @@ -146,7 +146,7 @@ impl CapSimState { let zone = &mut self.zones[*idx]; if now - zone.hour_started >= Duration::hours(1) { - zone.hour_started = Time::START_OF_DAY + Duration::hours(now.get_parts().0); + zone.hour_started = Time::START_OF_DAY + Duration::hours(now.get_hours()); zone.entered_in_last_hour.clear(); }