Compare commits

...

2 Commits

2 changed files with 10 additions and 5 deletions

View File

@ -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()
@ -278,7 +283,7 @@ mod tests {
#[test]
fn display() {
let mut dt = DateTime::from_timestamp(1706571482);
assert_eq!(dt.display(), "Mon Jan 29 23:38:02 Utc 2024");
assert_eq!(dt.display(), "Mon Jan 29 23:38:02 UTC 2024");
dt.zone = TimeZone::Offset {
sign: crate::zone::Sign::Positive,
hours: 4,

View File

@ -97,7 +97,7 @@ impl TimeZone {
pub fn display(&self) -> String {
match self {
Self::Utc => "Utc".into(),
Self::Utc => "UTC".into(),
_ => format!("{self}"),
}
}