summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2022-01-17 17:06:24 -0500
committerBotahamec <botahamec@outlook.com>2022-01-17 17:06:24 -0500
commite078092f40ac768aff3d6525be22157c122d1af1 (patch)
tree95707d855074b81b99d1d2e1d61d66f62e315fab /src
parentd4c16d0a6967666574eba5ab919b0d1e60f0558d (diff)
Added a reverse conversion to the timezone trait
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs1
-rw-r--r--src/timezone.rs19
2 files changed, 17 insertions, 3 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 12b7225..44e3ad0 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -14,5 +14,6 @@ pub use date::Date;
pub use datetime::NaiveDateTime;
pub use month::Month;
pub use time::Time;
+pub use timezone::TimeZone;
pub use weekday::Weekday;
pub use year::Year;
diff --git a/src/timezone.rs b/src/timezone.rs
index 8401631..5284892 100644
--- a/src/timezone.rs
+++ b/src/timezone.rs
@@ -3,9 +3,14 @@ use core::convert::Infallible;
/// A type that can be used to represent a TimeZone
pub trait TimeZone {
+ /// The error to return in case of a failure to convert the local time to UTC
type Err;
- fn utc_offset(&self, date_time: NaiveDateTime) -> Result<UtcOffset, Self::Err>;
+ /// Given the time in the UTC timezone, determine the UtcOffset
+ fn utc_offset(&self, date_time: NaiveDateTime) -> UtcOffset;
+
+ /// Given the local date and time, figure out the offset from UTC
+ fn offset_from_local_time(&self, date_time: NaiveDateTime) -> Result<UtcOffset, Self::Err>;
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
@@ -15,7 +20,11 @@ pub struct Utc;
impl TimeZone for Utc {
type Err = Infallible;
- fn utc_offset(&self, _: NaiveDateTime) -> Result<UtcOffset, Self::Err> {
+ fn utc_offset(&self, _: NaiveDateTime) -> UtcOffset {
+ UtcOffset::UTC
+ }
+
+ fn offset_from_local_time(&self, _: NaiveDateTime) -> Result<UtcOffset, Self::Err> {
Ok(UtcOffset::UTC)
}
}
@@ -74,7 +83,11 @@ impl UtcOffset {
impl TimeZone for UtcOffset {
type Err = Infallible;
- fn utc_offset(&self, _: NaiveDateTime) -> Result<UtcOffset, Self::Err> {
+ fn utc_offset(&self, _: NaiveDateTime) -> UtcOffset {
+ *self
+ }
+
+ fn offset_from_local_time(&self, _: NaiveDateTime) -> Result<UtcOffset, Self::Err> {
Ok(*self)
}
}