Skip to content

Commit

Permalink
add convertion from iso to chrono duration
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev authored and tdittr committed Jul 18, 2024
1 parent aa8dca4 commit 7e03b28
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 21 deletions.
1 change: 1 addition & 0 deletions 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
Expand Up @@ -26,7 +26,7 @@ regex = "1.10.4"
chrono = "0.4.38"
url = "2.5.0"
tower-http = { version = "0.5.2" , features = ["trace"]}
iso8601-duration = "0.2.0"
iso8601-duration = { version = "0.2.0", features = ["chrono"] }

[dev-dependencies]
quickcheck = "1.0.3"
Expand Down
8 changes: 4 additions & 4 deletions src/wire/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,15 @@ mod tests {
payload_descriptors: None,
interval_period: Some(IntervalPeriod {
start: "2023-06-15T09:30:00Z".parse().unwrap(),
duration: Some(Duration::hour()),
randomize_start: Some(Duration::hour()),
duration: Some(Duration::PT1H),
randomize_start: Some(Duration::PT1H),
}),
intervals: vec![EventInterval {
id: 0,
interval_period: Some(IntervalPeriod {
start: "2023-06-15T09:30:00Z".parse().unwrap(),
duration: Some(Duration::hour()),
randomize_start: Some(Duration::hour()),
duration: Some(Duration::PT1H),
randomize_start: Some(Duration::PT1H),
}),
payloads: vec![EventValuesMap {
value_type: EventType::Price,
Expand Down
40 changes: 31 additions & 9 deletions src/wire/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,38 @@ impl Serialize for Duration {
}

impl Duration {
pub const fn hour() -> Self {
Self(::iso8601_duration::Duration {
year: 0.0,
month: 0.0,
day: 0.0,
hour: 1.0,
minute: 0.0,
second: 0.0,
})
/// because iso8601 durations can include months and years, they don't independently have a
/// fixed duration. Their real duration (in real units like seconds) can only be determined
/// when a starting time is given.
///
/// NOTE: does not consider leap seconds!
pub fn to_chrono_at_datetime<Tz: chrono::TimeZone>(
&self,
at: chrono::DateTime<Tz>,
) -> chrono::Duration {
self.0.to_chrono_at_datetime(at)
}

/// One (1) hour
pub const PT1H: Self = Self(::iso8601_duration::Duration {
year: 0.0,
month: 0.0,
day: 0.0,
hour: 1.0,
minute: 0.0,
second: 0.0,
});

/// Indicates that an event's intervals continue indefinitely into the future until the event is
/// deleted or modified. This effectively represents an infinite duration.
pub const P999Y: Self = Self(::iso8601_duration::Duration {
year: 9999.0,
month: 0.0,
day: 0.0,
hour: 0.0,
minute: 0.0,
second: 0.0,
});
}

impl std::str::FromStr for Duration {
Expand Down
6 changes: 3 additions & 3 deletions src/wire/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ mod tests {
program_type: Some("PRICING_TARIFF".into()),
country: Some("US".into()),
principal_subdivision: Some("CO".into()),
time_zone_offset: Some(Duration::hour()),
time_zone_offset: Some(Duration::PT1H),
interval_period: Some(IntervalPeriod {
start: "2023-06-15T09:30:00Z".parse().unwrap(),
duration: Some(Duration::hour()),
randomize_start: Some(Duration::hour()),
duration: Some(Duration::PT1H),
randomize_start: Some(Duration::PT1H),
}),
program_descriptions: None,
binding_events: Some(false),
Expand Down
8 changes: 4 additions & 4 deletions src/wire/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,15 @@ mod tests {
resource_name: ResourceName::Private("RESOURCE-999".into()),
interval_period: Some(IntervalPeriod {
start: "2023-06-15T09:30:00Z".parse().unwrap(),
duration: Some(Duration::hour()),
randomize_start: Some(Duration::hour()),
duration: Some(Duration::PT1H),
randomize_start: Some(Duration::PT1H),
}),
intervals: vec![Interval {
id: 0,
interval_period: Some(IntervalPeriod {
start: "2023-06-15T09:30:00Z".parse().unwrap(),
duration: Some(Duration::hour()),
randomize_start: Some(Duration::hour()),
duration: Some(Duration::PT1H),
randomize_start: Some(Duration::PT1H),
}),
payloads: vec![ValuesMap {
value_type: ValueType("PRICE".into()),
Expand Down

0 comments on commit 7e03b28

Please sign in to comment.