Skip to content

Commit 15e287b

Browse files
AVeeArjan
and
Arjan
authored
Use NaiveDateTime for internal tz_info methods. (#1658)
This replaces passing a timestamp and year to several internal functions with passing a NaiveDateTime, making the function signature slightly clearer. The values originated from in the a NaiveDateTime in first place, so it basically just postpones the conversion. Co-authored-by: Arjan <arjan@xldata.nl>
1 parent 8317e7c commit 15e287b

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

src/offset/local/tz_info/rule.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::cmp::Ordering;
2-
31
use super::parser::Cursor;
42
use super::timezone::{LocalTimeType, SECONDS_PER_WEEK};
53
use super::{
64
Error, CUMUL_DAY_IN_MONTHS_NORMAL_YEAR, DAYS_PER_WEEK, DAY_IN_MONTHS_NORMAL_YEAR,
75
SECONDS_PER_DAY,
86
};
7+
use crate::{Datelike, NaiveDateTime};
8+
use std::cmp::Ordering;
99

1010
/// Transition rule
1111
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
@@ -81,15 +81,14 @@ impl TransitionRule {
8181
/// Find the local time type associated to the transition rule at the specified Unix time in seconds
8282
pub(super) fn find_local_time_type_from_local(
8383
&self,
84-
local_time: i64,
85-
year: i32,
84+
local_time: NaiveDateTime,
8685
) -> Result<crate::MappedLocalTime<LocalTimeType>, Error> {
8786
match self {
8887
TransitionRule::Fixed(local_time_type) => {
8988
Ok(crate::MappedLocalTime::Single(*local_time_type))
9089
}
9190
TransitionRule::Alternate(alternate_time) => {
92-
alternate_time.find_local_time_type_from_local(local_time, year)
91+
alternate_time.find_local_time_type_from_local(local_time)
9392
}
9493
}
9594
}
@@ -229,13 +228,11 @@ impl AlternateTime {
229228

230229
fn find_local_time_type_from_local(
231230
&self,
232-
local_time: i64,
233-
current_year: i32,
231+
local_time: NaiveDateTime,
234232
) -> Result<crate::MappedLocalTime<LocalTimeType>, Error> {
235-
// Check if the current year is valid for the following computations
236-
if !(i32::MIN + 2..=i32::MAX - 2).contains(&current_year) {
237-
return Err(Error::OutOfRange("out of range date time"));
238-
}
233+
// Year must be between i32::MIN + 2 and i32::MAX - 2, year in NaiveDate is always smaller.
234+
let current_year = local_time.year();
235+
let local_time = local_time.and_utc().timestamp();
239236

240237
let dst_start_transition_start =
241238
self.dst_start.unix_time(current_year, 0) + i64::from(self.dst_start_time);

src/offset/local/tz_info/timezone.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! Types related to a time zone.
22
3+
use super::rule::{AlternateTime, TransitionRule};
4+
use super::{parser, Error, DAYS_PER_WEEK, SECONDS_PER_DAY};
5+
use crate::NaiveDateTime;
36
use std::fs::{self, File};
47
use std::io::{self, Read};
58
use std::path::{Path, PathBuf};
69
use std::{cmp::Ordering, fmt, str};
710

8-
use super::rule::{AlternateTime, TransitionRule};
9-
use super::{parser, Error, DAYS_PER_WEEK, SECONDS_PER_DAY};
10-
1111
#[cfg(target_env = "ohos")]
1212
use crate::offset::local::tz_info::parser::Cursor;
1313

@@ -133,13 +133,11 @@ impl TimeZone {
133133
self.as_ref().find_local_time_type(unix_time)
134134
}
135135

136-
// should we pass NaiveDateTime all the way through to this fn?
137136
pub(crate) fn find_local_time_type_from_local(
138137
&self,
139-
local_time: i64,
140-
year: i32,
138+
local_time: NaiveDateTime,
141139
) -> Result<crate::MappedLocalTime<LocalTimeType>, Error> {
142-
self.as_ref().find_local_time_type_from_local(local_time, year)
140+
self.as_ref().find_local_time_type_from_local(local_time)
143141
}
144142

145143
/// Returns a reference to the time zone
@@ -225,8 +223,7 @@ impl<'a> TimeZoneRef<'a> {
225223

226224
pub(crate) fn find_local_time_type_from_local(
227225
&self,
228-
local_time: i64,
229-
year: i32,
226+
local_time: NaiveDateTime,
230227
) -> Result<crate::MappedLocalTime<LocalTimeType>, Error> {
231228
// #TODO: this is wrong as we need 'local_time_to_local_leap_time ?
232229
// but ... does the local time even include leap seconds ??
@@ -235,10 +232,10 @@ impl<'a> TimeZoneRef<'a> {
235232
// Err(Error::OutOfRange(error)) => return Err(Error::FindLocalTimeType(error)),
236233
// Err(err) => return Err(err),
237234
// };
238-
let local_leap_time = local_time;
235+
let local_leap_time = local_time.and_utc().timestamp();
239236

240237
// if we have at least one transition,
241-
// we must check _all_ of them, incase of any Overlapping (MappedLocalTime::Ambiguous) or Skipping (MappedLocalTime::None) transitions
238+
// we must check _all_ of them, in case of any Overlapping (MappedLocalTime::Ambiguous) or Skipping (MappedLocalTime::None) transitions
242239
let offset_after_last = if !self.transitions.is_empty() {
243240
let mut prev = self.local_time_types[0];
244241

@@ -301,7 +298,7 @@ impl<'a> TimeZoneRef<'a> {
301298
};
302299

303300
if let Some(extra_rule) = self.extra_rule {
304-
match extra_rule.find_local_time_type_from_local(local_time, year) {
301+
match extra_rule.find_local_time_type_from_local(local_time) {
305302
Ok(local_time_type) => Ok(local_time_type),
306303
Err(Error::OutOfRange(error)) => Err(Error::FindLocalTimeType(error)),
307304
err => err,

src/offset/local/unix.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::{cell::RefCell, collections::hash_map, env, fs, hash::Hasher, time::Sys
1212

1313
use super::tz_info::TimeZone;
1414
use super::{FixedOffset, NaiveDateTime};
15-
use crate::{Datelike, MappedLocalTime};
15+
use crate::MappedLocalTime;
1616

1717
pub(super) fn offset_from_utc_datetime(utc: &NaiveDateTime) -> MappedLocalTime<FixedOffset> {
1818
offset(utc, false)
@@ -164,7 +164,7 @@ impl Cache {
164164
// we pass through the year as the year of a local point in time must either be valid in that locale, or
165165
// the entire time was skipped in which case we will return MappedLocalTime::None anyway.
166166
self.zone
167-
.find_local_time_type_from_local(d.and_utc().timestamp(), d.year())
167+
.find_local_time_type_from_local(d)
168168
.expect("unable to select local time type")
169169
.and_then(|o| FixedOffset::east_opt(o.offset()))
170170
}

0 commit comments

Comments
 (0)