Skip to content

Fix Duration#toString() for negative ns #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/v1/internal/temporal-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ export function epochDayToDate(epochDay) {
export function durationToIsoString(months, days, seconds, nanoseconds) {
const monthsString = formatNumber(months);
const daysString = formatNumber(days);
const secondsString = formatNumber(seconds);
const nanosecondsString = formatNanoseconds(nanoseconds);
return `P${monthsString}M${daysString}DT${secondsString}${nanosecondsString}S`;
const secondsAndNanosecondsString = formatSecondsAndNanosecondsForDuration(seconds, nanoseconds);
return `P${monthsString}M${daysString}DT${secondsAndNanosecondsString}S`;
}

/**
Expand All @@ -204,7 +203,7 @@ export function timeToIsoString(hour, minute, second, nanosecond) {
const hourString = formatNumber(hour, 2);
const minuteString = formatNumber(minute, 2);
const secondString = formatNumber(second, 2);
const nanosecondString = formatNanoseconds(nanosecond);
const nanosecondString = formatNanosecond(nanosecond);
return `${hourString}:${minuteString}:${secondString}${nanosecondString}`;
}

Expand Down Expand Up @@ -320,11 +319,30 @@ function floorMod(x, y) {
return x.subtract(floorDiv(x, y).multiply(y));
}

/**
* @param {Integer|number|string} seconds the number of seconds to format.
* @param {Integer|number|string} nanoseconds the number of nanoseconds to format.
* @return {string} formatted value.
*/
function formatSecondsAndNanosecondsForDuration(seconds, nanoseconds) {
seconds = int(seconds);
nanoseconds = int(nanoseconds);

const signString = seconds.isNegative() || nanoseconds.isNegative() ? '-' : '';
seconds = seconds.isNegative() ? seconds.negate() : seconds;
nanoseconds = nanoseconds.isNegative() ? nanoseconds.negate() : nanoseconds;

const secondsString = formatNumber(seconds);
const nanosecondsString = formatNanosecond(nanoseconds);

return signString + secondsString + nanosecondsString;
}

/**
* @param {Integer|number|string} value the number of nanoseconds to format.
* @return {string} formatted and possibly left-padded nanoseconds part as string.
*/
function formatNanoseconds(value) {
function formatNanosecond(value) {
value = int(value);
return value.equals(0) ? '' : '.' + formatNumber(value, 9);
}
Expand All @@ -338,7 +356,7 @@ function formatNumber(num, stringLength = undefined) {
num = int(num);
const isNegative = num.isNegative();
if (isNegative) {
num = num.multiply(-1);
num = num.negate();
}
const numString = num.toString();
const paddedNumString = stringLength ? numString.padStart(stringLength, '0') : numString;
Expand Down
43 changes: 43 additions & 0 deletions test/v1/temporal-types.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,33 @@ describe('temporal-types', () => {
expect(zonedDateTime.nanosecond).toEqual(neo4j.int(9346458));
});

it('should format duration to string', done => {
if (neo4jDoesNotSupportTemporalTypes(done)) {
return;
}

testDurationToString([
{duration: duration(0, 0, 1, 0), expectedString: 'P0M0DT1S'},
{duration: duration(0, 0, -1, 0), expectedString: 'P0M0DT-1S'},

{duration: duration(0, 0, 0, 5), expectedString: 'P0M0DT0.000000005S'},
{duration: duration(0, 0, 0, -5), expectedString: 'P0M0DT-0.000000005S'},
{duration: duration(0, 0, 0, 999999999), expectedString: 'P0M0DT0.999999999S'},
{duration: duration(0, 0, 0, -999999999), expectedString: 'P0M0DT-0.999999999S'},

{duration: duration(0, 0, 1, 5), expectedString: 'P0M0DT1.000000005S'},
{duration: duration(0, 0, -1, -5), expectedString: 'P0M0DT-1.000000005S'},
{duration: duration(0, 0, 1, -5), expectedString: 'P0M0DT0.999999995S'},
{duration: duration(0, 0, -1, 5), expectedString: 'P0M0DT-0.999999995S'},
{duration: duration(0, 0, 1, 999999999), expectedString: 'P0M0DT1.999999999S'},
{duration: duration(0, 0, -1, -999999999), expectedString: 'P0M0DT-1.999999999S'},
{duration: duration(0, 0, 1, -999999999), expectedString: 'P0M0DT0.000000001S'},
{duration: duration(0, 0, -1, 999999999), expectedString: 'P0M0DT-0.000000001S'},

{duration: duration(0, 0, -78036, -143000000), expectedString: 'P0M0DT-78036.143000000S'}
], done);
});

function testSendAndReceiveRandomTemporalValues(valueGenerator, done) {
const asyncFunction = (index, callback) => {
const next = () => callback();
Expand Down Expand Up @@ -578,6 +605,22 @@ describe('temporal-types', () => {
});
}

function testDurationToString(values, done) {
const durations = values.map(value => value.duration);
const expectedDurationStrings = values.map(value => value.expectedString);

session.run('UNWIND $durations AS d RETURN d', {durations: durations}).then(result => {
const receivedDurationStrings = result.records
.map(record => record.get(0))
.map(duration => duration.toString());

expect(expectedDurationStrings).toEqual(receivedDurationStrings);
done();
}).catch(error => {
done.fail(error);
});
}

function neo4jDoesNotSupportTemporalTypes(done) {
if (serverVersion.compareTo(VERSION_3_4_0) < 0) {
done();
Expand Down