1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Hour-of-day effect (0..23) using TimeService
use crate::bar_indicators::indicator_value::IndicatorValue;
use crate::types::TimeService;
#[derive(Clone)]
pub struct HourOfDayEffect {
pub value: f64,
}
impl Default for HourOfDayEffect {
fn default() -> Self {
Self::new()
}
}
impl HourOfDayEffect {
pub fn new() -> Self {
Self { value: 0.0 }
}
#[inline]
pub fn reset(&mut self) {
self.value = 0.0;
}
#[inline]
pub fn is_ready(&self) -> bool {
true
}
pub fn value(&self) -> IndicatorValue {
IndicatorValue::Single(self.value)
}
pub fn update_with_timestamp(&mut self, unix_secs: i64) -> f64 {
let h = TimeService::hour_utc(unix_secs) as f64;
self.value = h;
self.value
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hour_of_day_effect_creation() {
let hod = HourOfDayEffect::new();
assert!(hod.is_ready());
assert_eq!(hod.value, 0.0);
}
#[test]
fn test_hour_of_day_effect_update() {
let mut hod = HourOfDayEffect::new();
// 1700000000 is ~2023-11-14 22:13:20 UTC
let value = hod.update_with_timestamp(1700000000);
assert!(value >= 0.0 && value < 24.0, "Hour should be in [0, 24)");
}
#[test]
fn test_hour_of_day_effect_range() {
let mut hod = HourOfDayEffect::new();
for hour in 0..24 {
let ts = 1700000000 + hour * 3600;
let value = hod.update_with_timestamp(ts);
assert!(value >= 0.0 && value < 24.0, "Hour should be in [0, 24)");
}
}
#[test]
fn test_hour_of_day_effect_reset() {
let mut hod = HourOfDayEffect::new();
hod.update_with_timestamp(1700000000);
hod.reset();
assert_eq!(hod.value, 0.0);
}
}