Skip to content

Commit 43c4899

Browse files
committed
Omit zero nanoseconds in toString() for temporal types
This changes implementations of `toString()` function for `Duration`, `Time`, `LocalTime`, `LocalDateTime` and `DateTime`. Returned strings still remain valid ISO strings and represent same temporal values.
1 parent 9532461 commit 43c4899

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/v1/internal/temporal-util.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ export function durationToIsoString(months, days, seconds, nanoseconds) {
189189
const monthsString = formatNumber(months);
190190
const daysString = formatNumber(days);
191191
const secondsString = formatNumber(seconds);
192-
const nanosecondsString = formatNumber(nanoseconds, 9);
193-
return `P${monthsString}M${daysString}DT${secondsString}.${nanosecondsString}S`;
192+
const nanosecondsString = formatNanoseconds(nanoseconds);
193+
return `P${monthsString}M${daysString}DT${secondsString}${nanosecondsString}S`;
194194
}
195195

196196
/**
@@ -205,8 +205,8 @@ export function timeToIsoString(hour, minute, second, nanosecond) {
205205
const hourString = formatNumber(hour, 2);
206206
const minuteString = formatNumber(minute, 2);
207207
const secondString = formatNumber(second, 2);
208-
const nanosecondString = formatNumber(nanosecond, 9);
209-
return `${hourString}:${minuteString}:${secondString}.${nanosecondString}`;
208+
const nanosecondString = formatNanoseconds(nanosecond);
209+
return `${hourString}:${minuteString}:${secondString}${nanosecondString}`;
210210
}
211211

212212
/**
@@ -321,6 +321,15 @@ function floorMod(x, y) {
321321
return x.subtract(floorDiv(x, y).multiply(y));
322322
}
323323

324+
/**
325+
* @param {Integer|number|string} value the number of nanoseconds to format.
326+
* @return {string} formatted and possibly left-padded nanoseconds part as string.
327+
*/
328+
function formatNanoseconds(value) {
329+
value = int(value);
330+
return value.equals(0) ? '' : '.' + formatNumber(value, 9);
331+
}
332+
324333
/**
325334
* @param {Integer|number|string} num the number to format.
326335
* @param {number} [stringLength=undefined] the string length to left-pad to.

test/internal/temporal-util.test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ describe('temporal-util', () => {
5252
});
5353

5454
it('should convert time to ISO string', () => {
55-
expect(util.timeToIsoString(8, 9, 1, 0)).toEqual('08:09:01.000000000');
55+
expect(util.timeToIsoString(8, 9, 1, 0)).toEqual('08:09:01');
56+
expect(util.timeToIsoString(1, 23, 45, 600000000)).toEqual('01:23:45.600000000');
5657
expect(util.timeToIsoString(int(2), int(4), int(6), int(7))).toEqual('02:04:06.000000007');
5758
expect(util.timeToIsoString(22, 19, 7, 999)).toEqual('22:19:07.000000999');
5859
expect(util.timeToIsoString(int(17), int(2), int(59), int(909090))).toEqual('17:02:59.000909090');
@@ -61,7 +62,7 @@ describe('temporal-util', () => {
6162
});
6263

6364
it('should convert duration to ISO string', () => {
64-
expect(util.durationToIsoString(0, 0, 0, 0)).toEqual('P0M0DT0.000000000S');
65+
expect(util.durationToIsoString(0, 0, 0, 0)).toEqual('P0M0DT0S');
6566
expect(util.durationToIsoString(0, 0, 0, 123)).toEqual('P0M0DT0.000000123S');
6667
expect(util.durationToIsoString(11, 99, 100, 99901)).toEqual('P11M99DT100.000099901S');
6768
expect(util.durationToIsoString(int(3), int(9191), int(17), int(123456789))).toEqual('P3M9191DT17.123456789S');

test/v1/temporal-types.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -435,20 +435,20 @@ describe('temporal-types', () => {
435435

436436
it('should convert Duration to ISO string', () => {
437437
expect(duration(13, 62, 3, 999111999).toString()).toEqual('P13M62DT3.999111999S');
438-
expect(duration(0, 0, 0, 0).toString()).toEqual('P0M0DT0.000000000S');
438+
expect(duration(0, 0, 0, 0).toString()).toEqual('P0M0DT0S');
439439
expect(duration(-1, -2, 10, 10).toString()).toEqual('P-1M-2DT10.000000010S');
440440
});
441441

442442
it('should convert LocalTime to ISO string', () => {
443443
expect(localTime(12, 19, 39, 111222333).toString()).toEqual('12:19:39.111222333');
444444
expect(localTime(3, 59, 2, 17).toString()).toEqual('03:59:02.000000017');
445-
expect(localTime(0, 0, 0, 0).toString()).toEqual('00:00:00.000000000');
445+
expect(localTime(0, 0, 0, 0).toString()).toEqual('00:00:00');
446446
});
447447

448448
it('should convert Time to ISO string', () => {
449449
expect(time(11, 45, 22, 333222111, 9015).toString()).toEqual('11:45:22.333222111+02:30:15');
450450
expect(time(23, 2, 1, 10, 0).toString()).toEqual('23:02:01.000000010Z');
451-
expect(time(0, 12, 59, 0, -40500).toString()).toEqual('00:12:59.000000000-11:15');
451+
expect(time(0, 12, 59, 0, -40500).toString()).toEqual('00:12:59-11:15');
452452
expect(time(21, 59, 0, 123, -25200).toString()).toEqual('21:59:00.000000123-07:00');
453453
});
454454

0 commit comments

Comments
 (0)