@@ -5,7 +5,7 @@ use crate::{
5
5
iso:: IsoTime ,
6
6
options:: {
7
7
DifferenceOperation , DifferenceSettings , Overflow , ResolvedRoundingOptions ,
8
- RoundingIncrement , RoundingMode , ToStringRoundingOptions , Unit , UnitGroup ,
8
+ RoundingOptions , ToStringRoundingOptions , Unit , UnitGroup ,
9
9
} ,
10
10
parsers:: { parse_time, IxdtfStringBuilder } ,
11
11
DateDuration , TemporalError , TemporalResult ,
@@ -16,6 +16,8 @@ use writeable::Writeable;
16
16
17
17
use super :: { duration:: normalized:: TimeDuration , PlainDateTime } ;
18
18
19
+ // TODO: add a PartialSignedTime
20
+
19
21
/// A `PartialTime` represents partially filled `Time` fields.
20
22
#[ derive( Debug , Default , Clone , Copy , PartialEq ) ]
21
23
pub struct PartialTime {
@@ -173,13 +175,15 @@ impl PartialTime {
173
175
/// ### Rounding times
174
176
///
175
177
/// ```rust
176
- /// use temporal_rs::{PlainTime, options::{Unit, RoundingMode }};
178
+ /// use temporal_rs::{PlainTime, options::{Unit, RoundingOptions }};
177
179
/// use core::str::FromStr;
178
180
///
179
181
/// let time = PlainTime::from_str("14:23:47.789").unwrap();
180
182
///
183
+ /// let mut options = RoundingOptions::default();
184
+ /// options.smallest_unit = Some(Unit::Minute);
181
185
/// // Round to nearest minute
182
- /// let rounded = time.round(Unit::Minute, None, None ).unwrap();
186
+ /// let rounded = time.round(options ).unwrap();
183
187
/// assert_eq!(rounded.hour(), 14);
184
188
/// assert_eq!(rounded.minute(), 24);
185
189
/// assert_eq!(rounded.second(), 0);
@@ -207,12 +211,6 @@ impl PlainTime {
207
211
Self { iso }
208
212
}
209
213
210
- /// Returns true if a valid `Time`.
211
- #[ allow( dead_code) ]
212
- pub ( crate ) fn is_valid ( & self ) -> bool {
213
- self . iso . is_valid ( )
214
- }
215
-
216
214
/// Specification equivalent to `4.5.15 AddTime ( time, timeDuration )`
217
215
///
218
216
/// Spec: <https://tc39.es/proposal-temporal/#sec-temporal-addtime>
@@ -497,49 +495,26 @@ impl PlainTime {
497
495
self . add_to_time ( & duration. negated ( ) )
498
496
}
499
497
500
- #[ inline]
501
498
/// Returns the `Duration` until the provided `Time` from the current `Time`.
502
499
///
503
500
/// NOTE: `until` assumes the provided other time will occur in the future relative to the current.
501
+ #[ inline]
504
502
pub fn until ( & self , other : & Self , settings : DifferenceSettings ) -> TemporalResult < Duration > {
505
503
self . diff_time ( DifferenceOperation :: Until , other, settings)
506
504
}
507
505
508
- #[ inline]
509
506
/// Returns the `Duration` since the provided `Time` from the current `Time`.
510
507
///
511
508
/// NOTE: `since` assumes the provided other time is in the past relative to the current.
509
+ #[ inline]
512
510
pub fn since ( & self , other : & Self , settings : DifferenceSettings ) -> TemporalResult < Duration > {
513
511
self . diff_time ( DifferenceOperation :: Since , other, settings)
514
512
}
515
513
516
514
// TODO (nekevss): optimize and test rounding_increment type (f64 vs. u64).
517
515
/// Rounds the current `Time` according to provided options.
518
- pub fn round (
519
- & self ,
520
- smallest_unit : Unit ,
521
- rounding_increment : Option < f64 > ,
522
- rounding_mode : Option < RoundingMode > ,
523
- ) -> TemporalResult < Self > {
524
- let increment = RoundingIncrement :: try_from ( rounding_increment. unwrap_or ( 1.0 ) ) ?;
525
- let rounding_mode = rounding_mode. unwrap_or ( RoundingMode :: HalfExpand ) ;
526
-
527
- let max = smallest_unit
528
- . to_maximum_rounding_increment ( )
529
- . ok_or_else ( || {
530
- TemporalError :: range ( ) . with_message ( "smallestUnit must be a time value." )
531
- } ) ?;
532
-
533
- // Safety (nekevss): to_rounding_increment returns a value in the range of a u32.
534
- increment. validate ( u64:: from ( max) , false ) ?;
535
-
536
- let resolved = ResolvedRoundingOptions {
537
- largest_unit : Unit :: Auto ,
538
- increment,
539
- smallest_unit,
540
- rounding_mode,
541
- } ;
542
-
516
+ pub fn round ( & self , options : RoundingOptions ) -> TemporalResult < Self > {
517
+ let resolved = ResolvedRoundingOptions :: from_time_options ( options) ?;
543
518
let ( _, result) = self . iso . round ( resolved) ?;
544
519
545
520
Ok ( Self :: new_unchecked ( result) )
@@ -565,8 +540,8 @@ impl PlainTime {
565
540
}
566
541
567
542
impl From < PlainDateTime > for PlainTime {
568
- fn from ( value : PlainDateTime ) -> Self {
569
- PlainTime :: new_unchecked ( value . iso . time )
543
+ fn from ( pdt : PlainDateTime ) -> Self {
544
+ pdt . to_plain_time ( )
570
545
}
571
546
}
572
547
@@ -587,7 +562,7 @@ mod tests {
587
562
use crate :: {
588
563
builtins:: core:: Duration ,
589
564
iso:: IsoTime ,
590
- options:: { DifferenceSettings , Overflow , RoundingIncrement , Unit } ,
565
+ options:: { DifferenceSettings , Overflow , RoundingIncrement , RoundingOptions , Unit } ,
591
566
} ;
592
567
use num_traits:: FromPrimitive ;
593
568
@@ -630,6 +605,14 @@ mod tests {
630
605
)
631
606
}
632
607
608
+ fn options ( unit : Unit , increment : f64 ) -> RoundingOptions {
609
+ RoundingOptions {
610
+ smallest_unit : Some ( unit) ,
611
+ increment : RoundingIncrement :: try_from ( increment) . ok ( ) ,
612
+ ..Default :: default ( )
613
+ }
614
+ }
615
+
633
616
#[ test]
634
617
fn from_str_cast_sanity_test ( ) {
635
618
let max = u32:: MAX ;
@@ -660,50 +643,50 @@ mod tests {
660
643
fn time_round_millisecond ( ) {
661
644
let base = PlainTime :: new_unchecked ( IsoTime :: new_unchecked ( 3 , 34 , 56 , 987 , 654 , 321 ) ) ;
662
645
663
- let result_1 = base. round ( Unit :: Millisecond , Some ( 1.0 ) , None ) . unwrap ( ) ;
646
+ let result_1 = base. round ( options ( Unit :: Millisecond , 1.0 ) ) . unwrap ( ) ;
664
647
assert_time ( result_1, ( 3 , 34 , 56 , 988 , 0 , 0 ) ) ;
665
648
666
- let result_2 = base. round ( Unit :: Millisecond , Some ( 2.0 ) , None ) . unwrap ( ) ;
649
+ let result_2 = base. round ( options ( Unit :: Millisecond , 2.0 ) ) . unwrap ( ) ;
667
650
assert_time ( result_2, ( 3 , 34 , 56 , 988 , 0 , 0 ) ) ;
668
651
669
- let result_3 = base. round ( Unit :: Millisecond , Some ( 4.0 ) , None ) . unwrap ( ) ;
652
+ let result_3 = base. round ( options ( Unit :: Millisecond , 4.0 ) ) . unwrap ( ) ;
670
653
assert_time ( result_3, ( 3 , 34 , 56 , 988 , 0 , 0 ) ) ;
671
654
672
- let result_4 = base. round ( Unit :: Millisecond , Some ( 5.0 ) , None ) . unwrap ( ) ;
655
+ let result_4 = base. round ( options ( Unit :: Millisecond , 5.0 ) ) . unwrap ( ) ;
673
656
assert_time ( result_4, ( 3 , 34 , 56 , 990 , 0 , 0 ) ) ;
674
657
}
675
658
676
659
#[ test]
677
660
fn time_round_microsecond ( ) {
678
661
let base = PlainTime :: new_unchecked ( IsoTime :: new_unchecked ( 3 , 34 , 56 , 987 , 654 , 321 ) ) ;
679
662
680
- let result_1 = base. round ( Unit :: Microsecond , Some ( 1.0 ) , None ) . unwrap ( ) ;
663
+ let result_1 = base. round ( options ( Unit :: Microsecond , 1.0 ) ) . unwrap ( ) ;
681
664
assert_time ( result_1, ( 3 , 34 , 56 , 987 , 654 , 0 ) ) ;
682
665
683
- let result_2 = base. round ( Unit :: Microsecond , Some ( 2.0 ) , None ) . unwrap ( ) ;
666
+ let result_2 = base. round ( options ( Unit :: Microsecond , 2.0 ) ) . unwrap ( ) ;
684
667
assert_time ( result_2, ( 3 , 34 , 56 , 987 , 654 , 0 ) ) ;
685
668
686
- let result_3 = base. round ( Unit :: Microsecond , Some ( 4.0 ) , None ) . unwrap ( ) ;
669
+ let result_3 = base. round ( options ( Unit :: Microsecond , 4.0 ) ) . unwrap ( ) ;
687
670
assert_time ( result_3, ( 3 , 34 , 56 , 987 , 656 , 0 ) ) ;
688
671
689
- let result_4 = base. round ( Unit :: Microsecond , Some ( 5.0 ) , None ) . unwrap ( ) ;
672
+ let result_4 = base. round ( options ( Unit :: Microsecond , 5.0 ) ) . unwrap ( ) ;
690
673
assert_time ( result_4, ( 3 , 34 , 56 , 987 , 655 , 0 ) ) ;
691
674
}
692
675
693
676
#[ test]
694
677
fn time_round_nanoseconds ( ) {
695
678
let base = PlainTime :: new_unchecked ( IsoTime :: new_unchecked ( 3 , 34 , 56 , 987 , 654 , 321 ) ) ;
696
679
697
- let result_1 = base. round ( Unit :: Nanosecond , Some ( 1.0 ) , None ) . unwrap ( ) ;
680
+ let result_1 = base. round ( options ( Unit :: Nanosecond , 1.0 ) ) . unwrap ( ) ;
698
681
assert_time ( result_1, ( 3 , 34 , 56 , 987 , 654 , 321 ) ) ;
699
682
700
- let result_2 = base. round ( Unit :: Nanosecond , Some ( 2.0 ) , None ) . unwrap ( ) ;
683
+ let result_2 = base. round ( options ( Unit :: Nanosecond , 2.0 ) ) . unwrap ( ) ;
701
684
assert_time ( result_2, ( 3 , 34 , 56 , 987 , 654 , 322 ) ) ;
702
685
703
- let result_3 = base. round ( Unit :: Nanosecond , Some ( 4.0 ) , None ) . unwrap ( ) ;
686
+ let result_3 = base. round ( options ( Unit :: Nanosecond , 4.0 ) ) . unwrap ( ) ;
704
687
assert_time ( result_3, ( 3 , 34 , 56 , 987 , 654 , 320 ) ) ;
705
688
706
- let result_4 = base. round ( Unit :: Nanosecond , Some ( 5.0 ) , None ) . unwrap ( ) ;
689
+ let result_4 = base. round ( options ( Unit :: Nanosecond , 5.0 ) ) . unwrap ( ) ;
707
690
assert_time ( result_4, ( 3 , 34 , 56 , 987 , 654 , 320 ) ) ;
708
691
}
709
692
@@ -796,63 +779,63 @@ mod tests {
796
779
PlainTime :: new_with_overflow ( 3 , 34 , 56 , 987 , 654 , 321 , Overflow :: Constrain ) . unwrap ( ) ;
797
780
798
781
assert_eq ! (
799
- time. round( Unit :: Nanosecond , Some ( 1.0 ) , None ) . unwrap( ) ,
782
+ time. round( options ( Unit :: Nanosecond , 1.0 ) ) . unwrap( ) ,
800
783
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 321 , Overflow :: Constrain ) . unwrap( )
801
784
) ;
802
785
assert_eq ! (
803
- time. round( Unit :: Nanosecond , Some ( 2.0 ) , None ) . unwrap( ) ,
786
+ time. round( options ( Unit :: Nanosecond , 2.0 ) ) . unwrap( ) ,
804
787
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 322 , Overflow :: Constrain ) . unwrap( )
805
788
) ;
806
789
assert_eq ! (
807
- time. round( Unit :: Nanosecond , Some ( 4.0 ) , None ) . unwrap( ) ,
790
+ time. round( options ( Unit :: Nanosecond , 4.0 ) ) . unwrap( ) ,
808
791
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 320 , Overflow :: Constrain ) . unwrap( )
809
792
) ;
810
793
assert_eq ! (
811
- time. round( Unit :: Nanosecond , Some ( 5.0 ) , None ) . unwrap( ) ,
794
+ time. round( options ( Unit :: Nanosecond , 5.0 ) ) . unwrap( ) ,
812
795
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 320 , Overflow :: Constrain ) . unwrap( )
813
796
) ;
814
797
assert_eq ! (
815
- time. round( Unit :: Nanosecond , Some ( 8.0 ) , None ) . unwrap( ) ,
798
+ time. round( options ( Unit :: Nanosecond , 8.0 ) ) . unwrap( ) ,
816
799
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 320 , Overflow :: Constrain ) . unwrap( )
817
800
) ;
818
801
assert_eq ! (
819
- time. round( Unit :: Nanosecond , Some ( 10.0 ) , None ) . unwrap( ) ,
802
+ time. round( options ( Unit :: Nanosecond , 10.0 ) ) . unwrap( ) ,
820
803
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 320 , Overflow :: Constrain ) . unwrap( )
821
804
) ;
822
805
assert_eq ! (
823
- time. round( Unit :: Nanosecond , Some ( 20.0 ) , None ) . unwrap( ) ,
806
+ time. round( options ( Unit :: Nanosecond , 20.0 ) ) . unwrap( ) ,
824
807
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 320 , Overflow :: Constrain ) . unwrap( )
825
808
) ;
826
809
assert_eq ! (
827
- time. round( Unit :: Nanosecond , Some ( 25.0 ) , None ) . unwrap( ) ,
810
+ time. round( options ( Unit :: Nanosecond , 25.0 ) ) . unwrap( ) ,
828
811
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 325 , Overflow :: Constrain ) . unwrap( )
829
812
) ;
830
813
assert_eq ! (
831
- time. round( Unit :: Nanosecond , Some ( 40.0 ) , None ) . unwrap( ) ,
814
+ time. round( options ( Unit :: Nanosecond , 40.0 ) ) . unwrap( ) ,
832
815
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 320 , Overflow :: Constrain ) . unwrap( )
833
816
) ;
834
817
assert_eq ! (
835
- time. round( Unit :: Nanosecond , Some ( 50.0 ) , None ) . unwrap( ) ,
818
+ time. round( options ( Unit :: Nanosecond , 50.0 ) ) . unwrap( ) ,
836
819
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 300 , Overflow :: Constrain ) . unwrap( )
837
820
) ;
838
821
assert_eq ! (
839
- time. round( Unit :: Nanosecond , Some ( 100.0 ) , None ) . unwrap( ) ,
822
+ time. round( options ( Unit :: Nanosecond , 100.0 ) ) . unwrap( ) ,
840
823
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 300 , Overflow :: Constrain ) . unwrap( )
841
824
) ;
842
825
assert_eq ! (
843
- time. round( Unit :: Nanosecond , Some ( 125.0 ) , None ) . unwrap( ) ,
826
+ time. round( options ( Unit :: Nanosecond , 125.0 ) ) . unwrap( ) ,
844
827
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 375 , Overflow :: Constrain ) . unwrap( )
845
828
) ;
846
829
assert_eq ! (
847
- time. round( Unit :: Nanosecond , Some ( 200.0 ) , None ) . unwrap( ) ,
830
+ time. round( options ( Unit :: Nanosecond , 200.0 ) ) . unwrap( ) ,
848
831
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 400 , Overflow :: Constrain ) . unwrap( )
849
832
) ;
850
833
assert_eq ! (
851
- time. round( Unit :: Nanosecond , Some ( 250.0 ) , None ) . unwrap( ) ,
834
+ time. round( options ( Unit :: Nanosecond , 250.0 ) ) . unwrap( ) ,
852
835
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 250 , Overflow :: Constrain ) . unwrap( )
853
836
) ;
854
837
assert_eq ! (
855
- time. round( Unit :: Nanosecond , Some ( 500.0 ) , None ) . unwrap( ) ,
838
+ time. round( options ( Unit :: Nanosecond , 500.0 ) ) . unwrap( ) ,
856
839
PlainTime :: new_with_overflow( 3 , 34 , 56 , 987 , 654 , 500 , Overflow :: Constrain ) . unwrap( )
857
840
) ;
858
841
}
0 commit comments