From 3594cfafdf5df3f66f797417934a7da4a6456265 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Tue, 30 Jan 2024 00:22:52 -0500 Subject: [PATCH] Fix Weekday calculation to skip using TimeZone when converting DateTime to timestamp, so that the calculated weekday will match local time rather than UTC. --- src/datetime.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/datetime.rs b/src/datetime.rs index c6905c4..afc0ced 100644 --- a/src/datetime.rs +++ b/src/datetime.rs @@ -24,8 +24,7 @@ impl DateTime { self.year.get() } - /// Gets the Unix timestamp corresponding to this `DateTime` - pub fn timestamp(&self) -> i64 { + fn timestamp_naive(&self) -> i64 { let mut seconds: i64 = 0; let mut year = Year::new(1970); if self.year() < 1970 { @@ -50,6 +49,12 @@ impl DateTime { seconds += i64::from(self.hour) * SECONDS_PER_HOUR; seconds += i64::from(self.minute) * SECONDS_PER_MINUTE; seconds += i64::from(self.second); + seconds + } + + /// Gets the Unix timestamp corresponding to this `DateTime` + pub fn timestamp(&self) -> i64 { + let mut seconds = self.timestamp_naive(); seconds += self.zone.as_seconds(); seconds } @@ -141,7 +146,7 @@ impl DateTime { } pub fn weekday(&self) -> Weekday { - let ts = self.timestamp(); + let ts = self.timestamp_naive(); let days = ts / SECONDS_PER_DAY; let rem = days % 7; rem.try_into().unwrap()