21
21
22
22
#[ derive( Clone ) ]
23
23
pub ( crate ) struct LocalTimeCacher {
24
- stored_key : CacheKey ,
24
+ stored_key : i64 ,
25
25
cache_values : Option < CacheValues > ,
26
26
}
27
27
@@ -31,16 +31,9 @@ pub(crate) struct TimeDate<'a> {
31
31
millisecond : u32 ,
32
32
}
33
33
34
- #[ derive( Clone , Eq , PartialEq ) ]
35
- enum CacheKey {
36
- NonLeap ( i64 ) ,
37
- Leap ( i64 ) ,
38
- }
39
-
40
34
#[ derive( Clone , Eq , PartialEq ) ]
41
35
struct CacheValues {
42
36
local_time : DateTime < Local > ,
43
- is_leap_second : bool ,
44
37
full_second_str : Option < String > ,
45
38
year : Option < i32 > ,
46
39
year_str : Option < String > ,
@@ -74,7 +67,7 @@ impl LocalTimeCacher {
74
67
#[ must_use]
75
68
fn new ( ) -> LocalTimeCacher {
76
69
LocalTimeCacher {
77
- stored_key : CacheKey :: NonLeap ( 0 ) ,
70
+ stored_key : 0 ,
78
71
cache_values : None ,
79
72
}
80
73
}
@@ -96,9 +89,9 @@ impl LocalTimeCacher {
96
89
} ;
97
90
let millisecond = reduced_nanosecond / 1_000_000 ;
98
91
99
- let cache_key = CacheKey :: new ( & utc_time, is_leap_second ) ;
92
+ let cache_key = utc_time. timestamp ( ) ;
100
93
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) ) ;
102
95
self . stored_key = cache_key;
103
96
}
104
97
@@ -163,6 +156,7 @@ impl<'a> TimeDate<'a> {
163
156
hour: u32 ,
164
157
hour12: ( bool , u32 ) ,
165
158
minute: u32 ,
159
+ second: u32 ,
166
160
}
167
161
168
162
impl_cache_fields_str_getter ! {
@@ -175,23 +169,6 @@ impl<'a> TimeDate<'a> {
175
169
timestamp => unix_timestamp_str : "{}" ,
176
170
}
177
171
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
-
195
172
#[ must_use]
196
173
pub ( crate ) fn weekday_name ( & mut self ) -> MultiName < & ' static str > {
197
174
match self . cached . weekday_name {
@@ -320,24 +297,11 @@ impl<'a> TimeDate<'a> {
320
297
}
321
298
}
322
299
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
-
335
300
impl CacheValues {
336
301
#[ must_use]
337
- fn new ( utc_time : DateTime < Utc > , is_leap_second : bool ) -> Self {
302
+ fn new ( utc_time : DateTime < Utc > ) -> Self {
338
303
CacheValues {
339
304
local_time : utc_time. into ( ) ,
340
- is_leap_second,
341
305
full_second_str : None ,
342
306
year : None ,
343
307
year_str : None ,
@@ -408,61 +372,3 @@ impl fmt::Debug for TimeDateLazyLocked<'_> {
408
372
. finish ( )
409
373
}
410
374
}
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