Repository: moment/luxon
Pull request #583 (2019-10-01 09:56:28)

Issue #492. Added methods toISODate, toISOTime to Interval

jsf-clabot: [![CLA assistant check](https://cla.js.foundation/pull/badge/not_signed)](https://cla.js.foundation/moment/luxon?pullRequest=583) <br/>Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our [Contributor License Agreement](https://cla.js.foundation/moment/luxon?pullRequest=583) before we can accept your contribution.<br/>
icambron: Looks good!

Patch:
diff --git a/src/interval.js b/src/interval.js
index 0bcf7d60..494d0b86 100644
--- a/src/interval.js
+++ b/src/interval.js
@@ -32,7 +32,7 @@ function validateStartEnd(start, end) {
  * * **Interrogation** To analyze the Interval, use {@link count}, {@link length}, {@link hasSame}, {@link contains}, {@link isAfter}, or {@link isBefore}.
  * * **Transformation** To create other Intervals out of this one, use {@link set}, {@link splitAt}, {@link splitBy}, {@link divideEqually}, {@link merge}, {@link xor}, {@link union}, {@link intersection}, or {@link difference}.
  * * **Comparison** To compare this Interval to another one, use {@link equals}, {@link overlaps}, {@link abutsStart}, {@link abutsEnd}, {@link engulfs}
- * * **Output*** To convert the Interval into other representations, see {@link toString}, {@link toISO}, {@link toFormat}, and {@link toDuration}.
+ * * **Output** To convert the Interval into other representations, see {@link toString}, {@link toISO}, {@link toISODate}, {@link toISOTime}, {@link toFormat}, and {@link toDuration}.
  */
 export default class Interval {
   /**
@@ -521,6 +521,29 @@ export default class Interval {
     return `${this.s.toISO(opts)}/${this.e.toISO(opts)}`;
   }
 
+  /**
+   * Returns an ISO 8601-compliant string representation of date of this Interval.
+   * The time components are ignored.
+   * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals
+   * @return {string}
+   */
+  toISODate() {
+    if (!this.isValid) return INVALID;
+    return `${this.s.toISODate()}/${this.e.toISODate()}`;
+  }
+
+  /**
+   * Returns an ISO 8601-compliant string representation of time of this Interval.
+   * The date components are ignored.
+   * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals
+   * @param {Object} opts - The same options as {@link DateTime.toISO}
+   * @return {string}
+   */
+  toISOTime(opts) {
+    if (!this.isValid) return INVALID;
+    return `${this.s.toISOTime(opts)}/${this.e.toISOTime(opts)}`;
+  }
+
   /**
    * Returns a string representation of this Interval formatted according to the specified format string.
    * @param {string} dateFormat - the format string. This string formats the start and end time. See {@link DateTime.toFormat} for details.


Test patch:
diff --git a/test/interval/format.test.js b/test/interval/format.test.js
index 3335c7cb..ca4658a4 100644
--- a/test/interval/format.test.js
+++ b/test/interval/format.test.js
@@ -29,6 +29,31 @@ test("Interval#toISO accepts ISO options", () =>
 test("Interval#toISO returns an unfriendly string for invalid intervals", () =>
   expect(invalid.toISO()).toBe("Invalid Interval"));
 
+//------
+// .toISODate()
+//------
+
+test("Interval#toISODate returns a simple ISO date interval format", () =>
+  expect(interval.toISODate()).toBe("1982-05-25/1983-10-14"));
+
+test("Interval#toISODate returns an unfriendly string for invalid intervals", () =>
+  expect(invalid.toISODate()).toBe("Invalid Interval"));
+
+//------
+// .toISOTime()
+//------
+
+test("Interval#toISOTime returns a simple ISO time interval format", () =>
+  expect(interval.toISOTime()).toBe("09:00:00.000Z/13:30:00.000Z"));
+
+test("Interval#toISOTime returns an unfriendly string for invalid intervals", () =>
+  expect(invalid.toISOTime()).toBe("Invalid Interval"));
+
+test("Interval#toISOTime accepts ISO options", () => {
+  expect(interval.toISOTime({ suppressSeconds: true })).toBe("09:00Z/13:30Z");
+  expect(interval.toISOTime({ suppressMilliseconds: true })).toBe("09:00:00Z/13:30:00Z");
+});
+
 //------
 // .toFormat()
 //------