Skip to content

Represent Cypher DateTime with a single type #352

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
Apr 12, 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
32 changes: 6 additions & 26 deletions src/v1/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,7 @@ import {assertString, isEmptyObjectOrNull} from './internal/util';
import urlUtil from './internal/url-util';
import HttpDriver from './internal/http/http-driver';
import {isPoint, Point} from './spatial-types';
import {
Date,
DateTimeWithZoneId,
DateTimeWithZoneOffset,
Duration,
isDate,
isDateTimeWithZoneId,
isDateTimeWithZoneOffset,
isDuration,
isLocalDateTime,
isLocalTime,
isTime,
LocalDateTime,
LocalTime,
Time
} from './temporal-types';
import {Date, DateTime, Duration, isDate, isDateTime, isDuration, isLocalDateTime, isLocalTime, isTime, LocalDateTime, LocalTime, Time} from './temporal-types';

/**
* @property {function(username: string, password: string, realm: ?string)} basic the function to create a
Expand Down Expand Up @@ -225,8 +210,7 @@ const types = {
Record,
Point,
Date,
DateTimeWithZoneId,
DateTimeWithZoneOffset,
DateTime,
Duration,
LocalDateTime,
LocalTime,
Expand Down Expand Up @@ -275,8 +259,7 @@ const forExport = {
Point,
isPoint,
Date,
DateTimeWithZoneId,
DateTimeWithZoneOffset,
DateTime,
Duration,
LocalDateTime,
LocalTime,
Expand All @@ -286,8 +269,7 @@ const forExport = {
isTime,
isDate,
isLocalDateTime,
isDateTimeWithZoneOffset,
isDateTimeWithZoneId
isDateTime
};

export {
Expand All @@ -303,8 +285,7 @@ export {
Point,
isPoint,
Date,
DateTimeWithZoneId,
DateTimeWithZoneOffset,
DateTime,
Duration,
LocalDateTime,
LocalTime,
Expand All @@ -314,7 +295,6 @@ export {
isTime,
isDate,
isLocalDateTime,
isDateTimeWithZoneOffset,
isDateTimeWithZoneId
isDateTime
};
export default forExport;
65 changes: 32 additions & 33 deletions src/v1/internal/packstream-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,7 @@

import * as v1 from './packstream-v1';
import {isPoint, Point} from '../spatial-types';
import {
Date,
DateTimeWithZoneId,
DateTimeWithZoneOffset,
Duration,
isDate,
isDateTimeWithZoneId,
isDateTimeWithZoneOffset,
isDuration,
isLocalDateTime,
isLocalTime,
isTime,
Time
} from '../temporal-types';
import {Date, DateTime, Duration, isDate, isDateTime, isDuration, isLocalDateTime, isLocalTime, isTime, Time} from '../temporal-types';
import {int, isInt} from '../integer';
import {
dateToEpochDay,
Expand Down Expand Up @@ -97,10 +84,8 @@ export class Packer extends v1.Packer {
return () => packDate(obj, this, onError);
} else if (isLocalDateTime(obj)) {
return () => packLocalDateTime(obj, this, onError);
} else if (isDateTimeWithZoneOffset(obj)) {
return () => packDateTimeWithZoneOffset(obj, this, onError);
} else if (isDateTimeWithZoneId(obj)) {
return () => packDateTimeWithZoneId(obj, this, onError);
} else if (isDateTime(obj)) {
return () => packDateTime(obj, this, onError);
} else {
return super.packable(obj, onError);
}
Expand Down Expand Up @@ -303,7 +288,7 @@ function unpackLocalTime(unpacker, structSize, buffer, disableLosslessIntegers)
*/
function packTime(value, packer, onError) {
const nanoOfDay = localTimeToNanoOfDay(value.hour, value.minute, value.second, value.nanosecond);
const offsetSeconds = int(value.offsetSeconds);
const offsetSeconds = int(value.timeZoneOffsetSeconds);

const packableStructFields = [
packer.packable(nanoOfDay, onError),
Expand Down Expand Up @@ -396,21 +381,35 @@ function unpackLocalDateTime(unpacker, structSize, buffer, disableLosslessIntege
return convertIntegerPropsIfNeeded(result, disableLosslessIntegers);
}

/**
* Pack given date time.
* @param {DateTime} value the date time value to pack.
* @param {Packer} packer the packer to use.
* @param {function} onError the error callback.
*/
function packDateTime(value, packer, onError) {
if (value.timeZoneId) {
packDateTimeWithZoneId(value, packer, onError);
} else {
packDateTimeWithZoneOffset(value, packer, onError);
}
}

/**
* Pack given date time with zone offset.
* @param {DateTimeWithZoneOffset} value the date time value to pack.
* @param {DateTime} value the date time value to pack.
* @param {Packer} packer the packer to use.
* @param {function} onError the error callback.
*/
function packDateTimeWithZoneOffset(value, packer, onError) {
const epochSecond = localDateTimeToEpochSecond(value.year, value.month, value.day, value.hour, value.minute, value.second, value.nanosecond);
const nano = int(value.nanosecond);
const offsetSeconds = int(value.offsetSeconds);
const timeZoneOffsetSeconds = int(value.timeZoneOffsetSeconds);

const packableStructFields = [
packer.packable(epochSecond, onError),
packer.packable(nano, onError),
packer.packable(offsetSeconds, onError)
packer.packable(timeZoneOffsetSeconds, onError)
];
packer.packStruct(DATE_TIME_WITH_ZONE_OFFSET, packableStructFields, onError);
}
Expand All @@ -421,36 +420,36 @@ function packDateTimeWithZoneOffset(value, packer, onError) {
* @param {number} structSize the retrieved struct size.
* @param {BaseBuffer} buffer the buffer to unpack from.
* @param {boolean} disableLosslessIntegers if integer properties in the result date-time should be native JS numbers.
* @return {DateTimeWithZoneOffset} the unpacked date time with zone offset value.
* @return {DateTime} the unpacked date time with zone offset value.
*/
function unpackDateTimeWithZoneOffset(unpacker, structSize, buffer, disableLosslessIntegers) {
unpacker._verifyStructSize('DateTimeWithZoneOffset', DATE_TIME_WITH_ZONE_OFFSET_STRUCT_SIZE, structSize);

const epochSecond = unpacker.unpackInteger(buffer);
const nano = unpacker.unpackInteger(buffer);
const offsetSeconds = unpacker.unpackInteger(buffer);
const timeZoneOffsetSeconds = unpacker.unpackInteger(buffer);

const localDateTime = epochSecondAndNanoToLocalDateTime(epochSecond, nano);
const result = new DateTimeWithZoneOffset(localDateTime.year, localDateTime.month, localDateTime.day,
localDateTime.hour, localDateTime.minute, localDateTime.second, localDateTime.nanosecond, offsetSeconds);
const result = new DateTime(localDateTime.year, localDateTime.month, localDateTime.day,
localDateTime.hour, localDateTime.minute, localDateTime.second, localDateTime.nanosecond, timeZoneOffsetSeconds, null);
return convertIntegerPropsIfNeeded(result, disableLosslessIntegers);
}

/**
* Pack given date time with zone id.
* @param {DateTimeWithZoneId} value the date time value to pack.
* @param {DateTime} value the date time value to pack.
* @param {Packer} packer the packer to use.
* @param {function} onError the error callback.
*/
function packDateTimeWithZoneId(value, packer, onError) {
const epochSecond = localDateTimeToEpochSecond(value.year, value.month, value.day, value.hour, value.minute, value.second, value.nanosecond);
const nano = int(value.nanosecond);
const zoneId = value.zoneId;
const timeZoneId = value.timeZoneId;

const packableStructFields = [
packer.packable(epochSecond, onError),
packer.packable(nano, onError),
packer.packable(zoneId, onError)
packer.packable(timeZoneId, onError)
];
packer.packStruct(DATE_TIME_WITH_ZONE_ID, packableStructFields, onError);
}
Expand All @@ -461,18 +460,18 @@ function packDateTimeWithZoneId(value, packer, onError) {
* @param {number} structSize the retrieved struct size.
* @param {BaseBuffer} buffer the buffer to unpack from.
* @param {boolean} disableLosslessIntegers if integer properties in the result date-time should be native JS numbers.
* @return {DateTimeWithZoneId} the unpacked date time with zone id value.
* @return {DateTime} the unpacked date time with zone id value.
*/
function unpackDateTimeWithZoneId(unpacker, structSize, buffer, disableLosslessIntegers) {
unpacker._verifyStructSize('DateTimeWithZoneId', DATE_TIME_WITH_ZONE_ID_STRUCT_SIZE, structSize);

const epochSecond = unpacker.unpackInteger(buffer);
const nano = unpacker.unpackInteger(buffer);
const zoneId = unpacker.unpack(buffer);
const timeZoneId = unpacker.unpack(buffer);

const localDateTime = epochSecondAndNanoToLocalDateTime(epochSecond, nano);
const result = new DateTimeWithZoneId(localDateTime.year, localDateTime.month, localDateTime.day,
localDateTime.hour, localDateTime.minute, localDateTime.second, localDateTime.nanosecond, zoneId);
const result = new DateTime(localDateTime.year, localDateTime.month, localDateTime.day,
localDateTime.hour, localDateTime.minute, localDateTime.second, localDateTime.nanosecond, null, timeZoneId);
return convertIntegerPropsIfNeeded(result, disableLosslessIntegers);
}

Expand Down
Loading