File tree 2 files changed +35
-8
lines changed
2 files changed +35
-8
lines changed Original file line number Diff line number Diff line change 5
5
6
6
import Timezone from "./timezone.js" ;
7
7
import Duration from "./duration.js" ;
8
+ import Period from "./period.js" ;
8
9
import design from "./design.js" ;
9
10
import TimezoneService from "./timezone_service.js" ;
10
11
import { strictParseInt , trunc , pad2 } from "./helpers.js" ;
@@ -965,18 +966,22 @@ class Time {
965
966
}
966
967
967
968
/**
968
- * Compares the ICAL.Time instance with another one.
969
+ * Compares the ICAL.Time instance with another one, or a period .
969
970
*
970
- * @param {ICAL.Duration } aOther The instance to compare with
971
- * @return {Number } -1, 0 or 1 for less/equal/greater
971
+ * @param {ICAL.Time|ICAL.Period } aOther The instance to compare with
972
+ * @return {Number } -1, 0 or 1 for less/equal/greater
972
973
*/
973
974
compare ( other ) {
974
- let a = this . toUnixTime ( ) ;
975
- let b = other . toUnixTime ( ) ;
975
+ if ( other instanceof Period ) {
976
+ return - 1 * other . compare ( this ) ;
977
+ } else {
978
+ let a = this . toUnixTime ( ) ;
979
+ let b = other . toUnixTime ( ) ;
976
980
977
- if ( a > b ) return 1 ;
978
- if ( b > a ) return - 1 ;
979
- return 0 ;
981
+ if ( a > b ) return 1 ;
982
+ if ( b > a ) return - 1 ;
983
+ return 0 ;
984
+ }
980
985
}
981
986
982
987
/**
Original file line number Diff line number Diff line change @@ -1606,6 +1606,28 @@ suite('icaltime', function() {
1606
1606
assert . equal ( a . compare ( b ) , 1 ) ;
1607
1607
assert . equal ( b . compare ( a ) , - 1 ) ;
1608
1608
} ) ;
1609
+
1610
+ test ( "compare with period" , function ( ) {
1611
+ let periodbefore = ICAL . Period . fromData ( {
1612
+ start : Time . fromString ( "1970-01-02T03:04:03Z" ) ,
1613
+ end : Time . fromString ( "1970-01-02T03:04:04Z" )
1614
+ } ) ;
1615
+ let periodat = ICAL . Period . fromData ( {
1616
+ start : Time . fromString ( "1970-01-02T03:04:05Z" ) ,
1617
+ end : Time . fromString ( "1970-01-02T03:04:05Z" )
1618
+ } ) ;
1619
+ let periodafter = ICAL . Period . fromData ( {
1620
+ start : Time . fromString ( "1970-01-02T03:04:06Z" ) ,
1621
+ end : Time . fromString ( "1970-01-02T03:04:07Z" )
1622
+ } ) ;
1623
+
1624
+
1625
+ let dt = Time . fromString ( '1970-01-02T03:04:05Z' ) ;
1626
+
1627
+ assert . equal ( dt . compare ( periodbefore ) , 1 ) ;
1628
+ assert . equal ( dt . compare ( periodat ) , 0 ) ;
1629
+ assert . equal ( dt . compare ( periodafter ) , - 1 ) ;
1630
+ } ) ;
1609
1631
} ) ;
1610
1632
1611
1633
test ( 'cache cleared' , function ( ) {
You can’t perform that action at this time.
0 commit comments