Skip to content

Commit

Permalink
truncate milliseconds DateTime#toISO
Browse files Browse the repository at this point in the history
  • Loading branch information
Ezequiel Gimenez committed Apr 2, 2023
1 parent 615ccf8 commit d1c8e29
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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'
Expand All @@ -1642,6 +1652,7 @@ export default class DateTime {
format = "extended",
suppressSeconds = false,
suppressMilliseconds = false,
truncateMilliseconds = false,
includeOffset = true,
extendedZone = false,
} = {}) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -1699,6 +1718,7 @@ export default class DateTime {
*/
toISOTime({
suppressMilliseconds = false,
truncateMilliseconds = false,
suppressSeconds = false,
includeOffset = true,
includePrefix = false,
Expand All @@ -1717,6 +1737,7 @@ export default class DateTime {
format === "extended",
suppressSeconds,
suppressMilliseconds,
truncateMilliseconds,
includeOffset,
extendedZone
)
Expand Down
39 changes: 39 additions & 0 deletions test/datetime/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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");
});
Expand All @@ -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");
});
Expand Down

0 comments on commit d1c8e29

Please sign in to comment.