From 615ccf8159e888b752184364a7ba3c54f8270bf4 Mon Sep 17 00:00:00 2001 From: Ezequiel Gimenez Date: Sat, 1 Apr 2023 23:41:28 -0300 Subject: [PATCH 1/3] remove duplicate extendedZone param --- src/datetime.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/datetime.js b/src/datetime.js index 902790945..844db673d 100644 --- a/src/datetime.js +++ b/src/datetime.js @@ -1627,11 +1627,11 @@ export default class DateTime { /** * Returns an ISO 8601-compliant string representation of this DateTime * @param {Object} opts - options - * @param {boolean} [opts.suppressMilliseconds=false] - exclude milliseconds from the format if they're 0 + * @param {string} [opts.format='extended'] - choose between the basic and extended format * @param {boolean} [opts.suppressSeconds=false] - exclude seconds from the format if they're 0 + * @param {boolean} [opts.suppressMilliseconds=false] - exclude milliseconds from the format if they're 0 * @param {boolean} [opts.includeOffset=true] - include the offset, such as 'Z' or '-04:00' * @param {boolean} [opts.extendedZone=false] - add the time zone format extension - * @param {string} [opts.format='extended'] - choose between the basic and extended format * @example DateTime.utc(1983, 5, 25).toISO() //=> '1982-05-25T00:00:00.000Z' * @example DateTime.now().toISO() //=> '2017-04-22T20:47:05.335-04:00' * @example DateTime.now().toISO({ includeOffset: false }) //=> '2017-04-22T20:47:05.335' From d1c8e290df6c84cefc91b560eba0224ecf330b5d Mon Sep 17 00:00:00 2001 From: Ezequiel Gimenez Date: Sun, 2 Apr 2023 02:20:21 -0300 Subject: [PATCH 2/3] truncate milliseconds DateTime#toISO --- src/datetime.js | 29 +++++++++++++++++++++++---- test/datetime/format.test.js | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/datetime.js b/src/datetime.js index 844db673d..2443fcccc 100644 --- a/src/datetime.js +++ b/src/datetime.js @@ -217,24 +217,33 @@ function toISOTime( extended, suppressSeconds, suppressMilliseconds, + truncateMilliseconds, includeOffset, extendedZone ) { let c = padStart(o.c.hour); + + const includeMilliseconds = + (o.c.millisecond !== 0 && !truncateMilliseconds) || + ((o.c.second !== 0 || !suppressSeconds) && !suppressMilliseconds && !truncateMilliseconds); + + const includeSeconds = includeMilliseconds || o.c.second !== 0 || !suppressSeconds; + if (extended) { c += ":"; c += padStart(o.c.minute); - if (o.c.millisecond !== 0 || o.c.second !== 0 || !suppressSeconds) { + + if (includeSeconds) { c += ":"; } } else { c += padStart(o.c.minute); } - if (o.c.millisecond !== 0 || o.c.second !== 0 || !suppressSeconds) { + if (includeSeconds) { c += padStart(o.c.second); - if (o.c.millisecond !== 0 || !suppressMilliseconds) { + if (includeMilliseconds) { c += "."; c += padStart(o.c.millisecond, 3); } @@ -1630,6 +1639,7 @@ export default class DateTime { * @param {string} [opts.format='extended'] - choose between the basic and extended format * @param {boolean} [opts.suppressSeconds=false] - exclude seconds from the format if they're 0 * @param {boolean} [opts.suppressMilliseconds=false] - exclude milliseconds from the format if they're 0 + * @param {boolean} [opts.truncateMilliseconds=false] - truncate the milliseconds from the format, irrespective of their value * @param {boolean} [opts.includeOffset=true] - include the offset, such as 'Z' or '-04:00' * @param {boolean} [opts.extendedZone=false] - add the time zone format extension * @example DateTime.utc(1983, 5, 25).toISO() //=> '1982-05-25T00:00:00.000Z' @@ -1642,6 +1652,7 @@ export default class DateTime { format = "extended", suppressSeconds = false, suppressMilliseconds = false, + truncateMilliseconds = false, includeOffset = true, extendedZone = false, } = {}) { @@ -1653,7 +1664,15 @@ export default class DateTime { let c = toISODate(this, ext); c += "T"; - c += toISOTime(this, ext, suppressSeconds, suppressMilliseconds, includeOffset, extendedZone); + c += toISOTime( + this, + ext, + suppressSeconds, + suppressMilliseconds, + truncateMilliseconds, + includeOffset, + extendedZone + ); return c; } @@ -1699,6 +1718,7 @@ export default class DateTime { */ toISOTime({ suppressMilliseconds = false, + truncateMilliseconds = false, suppressSeconds = false, includeOffset = true, includePrefix = false, @@ -1717,6 +1737,7 @@ export default class DateTime { format === "extended", suppressSeconds, suppressMilliseconds, + truncateMilliseconds, includeOffset, extendedZone ) diff --git a/test/datetime/format.test.js b/test/datetime/format.test.js index 66ef19e5b..589c448c2 100644 --- a/test/datetime/format.test.js +++ b/test/datetime/format.test.js @@ -104,6 +104,33 @@ test("DateTime#toISO() suppresses [milli]seconds", () => { ); }); +test("DateTime#toISO() truncates [milli]seconds", () => { + const truncateMilliseconds = { truncateMilliseconds: true }; + expect(dt.toISO(truncateMilliseconds)).toBe("1982-05-25T09:23:54Z"); + expect(dt.set({ millisecond: 0 }).toISO(truncateMilliseconds)).toBe("1982-05-25T09:23:54Z"); + + const noZeroSeconds = { + suppressSeconds: true, + suppressMilliseconds: true, + truncateMilliseconds: true, + }; + expect(dt.set({ seconds: 0 }).toISO(noZeroSeconds)).toBe("1982-05-25T09:23Z"); + expect(dt.set({ millisecond: 0 }).toISO(noZeroSeconds)).toBe("1982-05-25T09:23:54Z"); + expect(dt.set({ seconds: 0, milliseconds: 0 }).toISO(noZeroSeconds)).toBe("1982-05-25T09:23Z"); + + const suppressOnlySeconds = { suppressSeconds: true }; + expect(dt.set({ seconds: 0 }).toISO(suppressOnlySeconds)).toBe("1982-05-25T09:23:00.123Z"); + expect(dt.set({ seconds: 0, milliseconds: 0 }).toISO(suppressOnlySeconds)).toBe( + "1982-05-25T09:23Z" + ); + + const suppressMillisecondsOnly = { suppressMilliseconds: true }; + expect(dt.toISO(suppressMillisecondsOnly)).toBe("1982-05-25T09:23:54.123Z"); + expect(dt.set({ seconds: 0, milliseconds: 0 }).toISO(suppressMillisecondsOnly)).toBe( + "1982-05-25T09:23:00Z" + ); +}); + test("DateTime#toISO() returns null for invalid DateTimes", () => { expect(invalid.toISO()).toBe(null); }); @@ -221,6 +248,10 @@ test("DateTime#toISOTime() won't suppress milliseconds by default", () => { expect(dt.startOf("second").toISOTime()).toBe("09:23:54.000Z"); }); +test("DateTime#toISOTime() won't truncate milliseconds by default", () => { + expect(dt.toISOTime()).toBe("09:23:54.123Z"); +}); + test("DateTime#toISOTime({suppressMilliseconds: true}) won't suppress milliseconds if they're nonzero", () => { expect(dt.toISOTime({ suppressMilliseconds: true })).toBe("09:23:54.123Z"); }); @@ -229,6 +260,14 @@ test("DateTime#toISOTime({suppressMilliseconds: true}) will suppress millisecond expect(dt.set({ millisecond: 0 }).toISOTime({ suppressMilliseconds: true })).toBe("09:23:54Z"); }); +test("DateTime#toISOTime({truncateMilliseconds: true}) will truncate milliseconds if value is not zero", () => { + expect(dt.toISOTime({ truncateMilliseconds: true })).toBe("09:23:54Z"); +}); + +test("DateTime#toISOTime({truncateMilliseconds: true}) will truncate milliseconds if value is zero", () => { + expect(dt.set({ millisecond: 0 }).toISOTime({ truncateMilliseconds: true })).toBe("09:23:54Z"); +}); + test("DateTime#toISOTime({suppressSeconds: true}) won't suppress milliseconds if they're nonzero", () => { expect(dt.toISOTime({ suppressSeconds: true })).toBe("09:23:54.123Z"); }); From fdf6016856a292e0149fe5ddc9661509ca2d635f Mon Sep 17 00:00:00 2001 From: EzeGmnz <40328394+EzeGmnz@users.noreply.github.com> Date: Mon, 1 May 2023 00:29:19 -0300 Subject: [PATCH 3/3] moves added parameter to end of function head --- src/datetime.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/datetime.js b/src/datetime.js index 2443fcccc..f70950b7d 100644 --- a/src/datetime.js +++ b/src/datetime.js @@ -217,9 +217,9 @@ function toISOTime( extended, suppressSeconds, suppressMilliseconds, - truncateMilliseconds, includeOffset, - extendedZone + extendedZone, + truncateMilliseconds, ) { let c = padStart(o.c.hour); @@ -1652,9 +1652,9 @@ export default class DateTime { format = "extended", suppressSeconds = false, suppressMilliseconds = false, - truncateMilliseconds = false, includeOffset = true, extendedZone = false, + truncateMilliseconds = false, } = {}) { if (!this.isValid) { return null; @@ -1669,9 +1669,9 @@ export default class DateTime { ext, suppressSeconds, suppressMilliseconds, - truncateMilliseconds, includeOffset, - extendedZone + extendedZone, + truncateMilliseconds, ); return c; } @@ -1718,12 +1718,12 @@ export default class DateTime { */ toISOTime({ suppressMilliseconds = false, - truncateMilliseconds = false, suppressSeconds = false, includeOffset = true, includePrefix = false, extendedZone = false, format = "extended", + truncateMilliseconds = false, } = {}) { if (!this.isValid) { return null; @@ -1737,9 +1737,9 @@ export default class DateTime { format === "extended", suppressSeconds, suppressMilliseconds, - truncateMilliseconds, includeOffset, - extendedZone + extendedZone, + truncateMilliseconds, ) ); }