Skip to content

Commit 5ed160c

Browse files
committed
Drop leap second support as SystemTime doesn't count them
1 parent 1ee896f commit 5ed160c

File tree

1 file changed

+6
-100
lines changed

1 file changed

+6
-100
lines changed

spdlog/src/formatter/local_time_cacher.rs

Lines changed: 6 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ where
2121

2222
#[derive(Clone)]
2323
pub(crate) struct LocalTimeCacher {
24-
stored_key: CacheKey,
24+
stored_key: i64,
2525
cache_values: Option<CacheValues>,
2626
}
2727

@@ -31,16 +31,9 @@ pub(crate) struct TimeDate<'a> {
3131
millisecond: u32,
3232
}
3333

34-
#[derive(Clone, Eq, PartialEq)]
35-
enum CacheKey {
36-
NonLeap(i64),
37-
Leap(i64),
38-
}
39-
4034
#[derive(Clone, Eq, PartialEq)]
4135
struct CacheValues {
4236
local_time: DateTime<Local>,
43-
is_leap_second: bool,
4437
full_second_str: Option<String>,
4538
year: Option<i32>,
4639
year_str: Option<String>,
@@ -74,7 +67,7 @@ impl LocalTimeCacher {
7467
#[must_use]
7568
fn new() -> LocalTimeCacher {
7669
LocalTimeCacher {
77-
stored_key: CacheKey::NonLeap(0),
70+
stored_key: 0,
7871
cache_values: None,
7972
}
8073
}
@@ -96,9 +89,9 @@ impl LocalTimeCacher {
9689
};
9790
let millisecond = reduced_nanosecond / 1_000_000;
9891

99-
let cache_key = CacheKey::new(&utc_time, is_leap_second);
92+
let cache_key = utc_time.timestamp();
10093
if self.cache_values.is_none() || self.stored_key != cache_key {
101-
self.cache_values = Some(CacheValues::new(utc_time, is_leap_second));
94+
self.cache_values = Some(CacheValues::new(utc_time));
10295
self.stored_key = cache_key;
10396
}
10497

@@ -163,6 +156,7 @@ impl<'a> TimeDate<'a> {
163156
hour: u32,
164157
hour12: (bool, u32),
165158
minute: u32,
159+
second: u32,
166160
}
167161

168162
impl_cache_fields_str_getter! {
@@ -175,23 +169,6 @@ impl<'a> TimeDate<'a> {
175169
timestamp => unix_timestamp_str : "{}",
176170
}
177171

178-
#[must_use]
179-
pub(crate) fn second(&mut self) -> u32 {
180-
match self.cached.second {
181-
Some(value) => value,
182-
None => {
183-
let value = if !self.cached.is_leap_second {
184-
self.cached.local_time.second()
185-
} else {
186-
// https://www.itu.int/dms_pubrec/itu-r/rec/tf/R-REC-TF.460-6-200202-I!!PDF-E.pdf
187-
60
188-
};
189-
self.cached.second = Some(value);
190-
value
191-
}
192-
}
193-
}
194-
195172
#[must_use]
196173
pub(crate) fn weekday_name(&mut self) -> MultiName<&'static str> {
197174
match self.cached.weekday_name {
@@ -320,24 +297,11 @@ impl<'a> TimeDate<'a> {
320297
}
321298
}
322299

323-
impl CacheKey {
324-
#[must_use]
325-
fn new(utc_time: &DateTime<Utc>, is_leap_second: bool) -> Self {
326-
let timestamp = utc_time.timestamp();
327-
if !is_leap_second {
328-
Self::NonLeap(timestamp)
329-
} else {
330-
Self::Leap(timestamp)
331-
}
332-
}
333-
}
334-
335300
impl CacheValues {
336301
#[must_use]
337-
fn new(utc_time: DateTime<Utc>, is_leap_second: bool) -> Self {
302+
fn new(utc_time: DateTime<Utc>) -> Self {
338303
CacheValues {
339304
local_time: utc_time.into(),
340-
is_leap_second,
341305
full_second_str: None,
342306
year: None,
343307
year_str: None,
@@ -408,61 +372,3 @@ impl fmt::Debug for TimeDateLazyLocked<'_> {
408372
.finish()
409373
}
410374
}
411-
412-
#[cfg(test)]
413-
mod tests {
414-
use super::*;
415-
416-
#[test]
417-
fn leap_second() {
418-
let (date_2015, date_2022) = (
419-
NaiveDate::from_ymd_opt(2015, 6, 30).unwrap(),
420-
NaiveDate::from_ymd_opt(2022, 6, 30).unwrap(),
421-
);
422-
423-
enum Kind {
424-
NonLeap,
425-
Leap,
426-
}
427-
428-
let datetimes = [
429-
(
430-
Kind::NonLeap,
431-
date_2015.and_hms_nano_opt(23, 59, 59, 100_000_000).unwrap(),
432-
),
433-
(
434-
Kind::Leap,
435-
date_2015
436-
.and_hms_nano_opt(23, 59, 59, 1_000_000_000)
437-
.unwrap(),
438-
),
439-
(
440-
Kind::Leap,
441-
date_2015
442-
.and_hms_nano_opt(23, 59, 59, 1_100_000_000)
443-
.unwrap(),
444-
),
445-
(Kind::NonLeap, date_2022.and_hms_opt(23, 59, 59).unwrap()),
446-
(
447-
Kind::NonLeap,
448-
date_2022.and_hms_nano_opt(23, 59, 59, 100_000_000).unwrap(),
449-
),
450-
];
451-
452-
let mut cacher = LocalTimeCacher::new();
453-
454-
for datetime in datetimes {
455-
let leap = match datetime.0 {
456-
Kind::NonLeap => false,
457-
Kind::Leap => true,
458-
};
459-
let datetime = datetime.1;
460-
461-
println!(" => checking '{datetime}'");
462-
463-
let mut result = cacher.get_inner(datetime.and_local_timezone(Utc).unwrap());
464-
assert_eq!(result.cached.is_leap_second, leap);
465-
assert_eq!(result.second(), if !leap { 59 } else { 60 });
466-
}
467-
}
468-
}

0 commit comments

Comments
 (0)