Skip to content

Commit d63536d

Browse files
committed
Add comparison for periods in ICAL.Time::compare
1 parent 4be69a2 commit d63536d

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

lib/ical/time.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import Timezone from "./timezone.js";
77
import Duration from "./duration.js";
8+
import Period from "./period.js";
89
import design from "./design.js";
910
import TimezoneService from "./timezone_service.js";
1011
import { strictParseInt, trunc, pad2 } from "./helpers.js";
@@ -965,18 +966,22 @@ class Time {
965966
}
966967

967968
/**
968-
* Compares the ICAL.Time instance with another one.
969+
* Compares the ICAL.Time instance with another one, or a period.
969970
*
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
972973
*/
973974
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();
976980

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+
}
980985
}
981986

982987
/**

test/time_test.js

+22
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,28 @@ suite('icaltime', function() {
16061606
assert.equal(a.compare(b), 1);
16071607
assert.equal(b.compare(a), -1);
16081608
});
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+
});
16091631
});
16101632

16111633
test('cache cleared', function() {

0 commit comments

Comments
 (0)