Skip to content

Commit a558fcf

Browse files
authored
Merge pull request #352 from lutovich/1.6-single-DateTime-type
Represent Cypher DateTime with a single type
2 parents 7147128 + 51b8b91 commit a558fcf

File tree

7 files changed

+136
-217
lines changed

7 files changed

+136
-217
lines changed

src/v1/index.js

+6-26
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,7 @@ import {assertString, isEmptyObjectOrNull} from './internal/util';
3030
import urlUtil from './internal/url-util';
3131
import HttpDriver from './internal/http/http-driver';
3232
import {isPoint, Point} from './spatial-types';
33-
import {
34-
Date,
35-
DateTimeWithZoneId,
36-
DateTimeWithZoneOffset,
37-
Duration,
38-
isDate,
39-
isDateTimeWithZoneId,
40-
isDateTimeWithZoneOffset,
41-
isDuration,
42-
isLocalDateTime,
43-
isLocalTime,
44-
isTime,
45-
LocalDateTime,
46-
LocalTime,
47-
Time
48-
} from './temporal-types';
33+
import {Date, DateTime, Duration, isDate, isDateTime, isDuration, isLocalDateTime, isLocalTime, isTime, LocalDateTime, LocalTime, Time} from './temporal-types';
4934

5035
/**
5136
* @property {function(username: string, password: string, realm: ?string)} basic the function to create a
@@ -225,8 +210,7 @@ const types = {
225210
Record,
226211
Point,
227212
Date,
228-
DateTimeWithZoneId,
229-
DateTimeWithZoneOffset,
213+
DateTime,
230214
Duration,
231215
LocalDateTime,
232216
LocalTime,
@@ -275,8 +259,7 @@ const forExport = {
275259
Point,
276260
isPoint,
277261
Date,
278-
DateTimeWithZoneId,
279-
DateTimeWithZoneOffset,
262+
DateTime,
280263
Duration,
281264
LocalDateTime,
282265
LocalTime,
@@ -286,8 +269,7 @@ const forExport = {
286269
isTime,
287270
isDate,
288271
isLocalDateTime,
289-
isDateTimeWithZoneOffset,
290-
isDateTimeWithZoneId
272+
isDateTime
291273
};
292274

293275
export {
@@ -303,8 +285,7 @@ export {
303285
Point,
304286
isPoint,
305287
Date,
306-
DateTimeWithZoneId,
307-
DateTimeWithZoneOffset,
288+
DateTime,
308289
Duration,
309290
LocalDateTime,
310291
LocalTime,
@@ -314,7 +295,6 @@ export {
314295
isTime,
315296
isDate,
316297
isLocalDateTime,
317-
isDateTimeWithZoneOffset,
318-
isDateTimeWithZoneId
298+
isDateTime
319299
};
320300
export default forExport;

src/v1/internal/packstream-v2.js

+32-33
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,7 @@
1919

2020
import * as v1 from './packstream-v1';
2121
import {isPoint, Point} from '../spatial-types';
22-
import {
23-
Date,
24-
DateTimeWithZoneId,
25-
DateTimeWithZoneOffset,
26-
Duration,
27-
isDate,
28-
isDateTimeWithZoneId,
29-
isDateTimeWithZoneOffset,
30-
isDuration,
31-
isLocalDateTime,
32-
isLocalTime,
33-
isTime,
34-
Time
35-
} from '../temporal-types';
22+
import {Date, DateTime, Duration, isDate, isDateTime, isDuration, isLocalDateTime, isLocalTime, isTime, Time} from '../temporal-types';
3623
import {int, isInt} from '../integer';
3724
import {
3825
dateToEpochDay,
@@ -97,10 +84,8 @@ export class Packer extends v1.Packer {
9784
return () => packDate(obj, this, onError);
9885
} else if (isLocalDateTime(obj)) {
9986
return () => packLocalDateTime(obj, this, onError);
100-
} else if (isDateTimeWithZoneOffset(obj)) {
101-
return () => packDateTimeWithZoneOffset(obj, this, onError);
102-
} else if (isDateTimeWithZoneId(obj)) {
103-
return () => packDateTimeWithZoneId(obj, this, onError);
87+
} else if (isDateTime(obj)) {
88+
return () => packDateTime(obj, this, onError);
10489
} else {
10590
return super.packable(obj, onError);
10691
}
@@ -303,7 +288,7 @@ function unpackLocalTime(unpacker, structSize, buffer, disableLosslessIntegers)
303288
*/
304289
function packTime(value, packer, onError) {
305290
const nanoOfDay = localTimeToNanoOfDay(value.hour, value.minute, value.second, value.nanosecond);
306-
const offsetSeconds = int(value.offsetSeconds);
291+
const offsetSeconds = int(value.timeZoneOffsetSeconds);
307292

308293
const packableStructFields = [
309294
packer.packable(nanoOfDay, onError),
@@ -396,21 +381,35 @@ function unpackLocalDateTime(unpacker, structSize, buffer, disableLosslessIntege
396381
return convertIntegerPropsIfNeeded(result, disableLosslessIntegers);
397382
}
398383

384+
/**
385+
* Pack given date time.
386+
* @param {DateTime} value the date time value to pack.
387+
* @param {Packer} packer the packer to use.
388+
* @param {function} onError the error callback.
389+
*/
390+
function packDateTime(value, packer, onError) {
391+
if (value.timeZoneId) {
392+
packDateTimeWithZoneId(value, packer, onError);
393+
} else {
394+
packDateTimeWithZoneOffset(value, packer, onError);
395+
}
396+
}
397+
399398
/**
400399
* Pack given date time with zone offset.
401-
* @param {DateTimeWithZoneOffset} value the date time value to pack.
400+
* @param {DateTime} value the date time value to pack.
402401
* @param {Packer} packer the packer to use.
403402
* @param {function} onError the error callback.
404403
*/
405404
function packDateTimeWithZoneOffset(value, packer, onError) {
406405
const epochSecond = localDateTimeToEpochSecond(value.year, value.month, value.day, value.hour, value.minute, value.second, value.nanosecond);
407406
const nano = int(value.nanosecond);
408-
const offsetSeconds = int(value.offsetSeconds);
407+
const timeZoneOffsetSeconds = int(value.timeZoneOffsetSeconds);
409408

410409
const packableStructFields = [
411410
packer.packable(epochSecond, onError),
412411
packer.packable(nano, onError),
413-
packer.packable(offsetSeconds, onError)
412+
packer.packable(timeZoneOffsetSeconds, onError)
414413
];
415414
packer.packStruct(DATE_TIME_WITH_ZONE_OFFSET, packableStructFields, onError);
416415
}
@@ -421,36 +420,36 @@ function packDateTimeWithZoneOffset(value, packer, onError) {
421420
* @param {number} structSize the retrieved struct size.
422421
* @param {BaseBuffer} buffer the buffer to unpack from.
423422
* @param {boolean} disableLosslessIntegers if integer properties in the result date-time should be native JS numbers.
424-
* @return {DateTimeWithZoneOffset} the unpacked date time with zone offset value.
423+
* @return {DateTime} the unpacked date time with zone offset value.
425424
*/
426425
function unpackDateTimeWithZoneOffset(unpacker, structSize, buffer, disableLosslessIntegers) {
427426
unpacker._verifyStructSize('DateTimeWithZoneOffset', DATE_TIME_WITH_ZONE_OFFSET_STRUCT_SIZE, structSize);
428427

429428
const epochSecond = unpacker.unpackInteger(buffer);
430429
const nano = unpacker.unpackInteger(buffer);
431-
const offsetSeconds = unpacker.unpackInteger(buffer);
430+
const timeZoneOffsetSeconds = unpacker.unpackInteger(buffer);
432431

433432
const localDateTime = epochSecondAndNanoToLocalDateTime(epochSecond, nano);
434-
const result = new DateTimeWithZoneOffset(localDateTime.year, localDateTime.month, localDateTime.day,
435-
localDateTime.hour, localDateTime.minute, localDateTime.second, localDateTime.nanosecond, offsetSeconds);
433+
const result = new DateTime(localDateTime.year, localDateTime.month, localDateTime.day,
434+
localDateTime.hour, localDateTime.minute, localDateTime.second, localDateTime.nanosecond, timeZoneOffsetSeconds, null);
436435
return convertIntegerPropsIfNeeded(result, disableLosslessIntegers);
437436
}
438437

439438
/**
440439
* Pack given date time with zone id.
441-
* @param {DateTimeWithZoneId} value the date time value to pack.
440+
* @param {DateTime} value the date time value to pack.
442441
* @param {Packer} packer the packer to use.
443442
* @param {function} onError the error callback.
444443
*/
445444
function packDateTimeWithZoneId(value, packer, onError) {
446445
const epochSecond = localDateTimeToEpochSecond(value.year, value.month, value.day, value.hour, value.minute, value.second, value.nanosecond);
447446
const nano = int(value.nanosecond);
448-
const zoneId = value.zoneId;
447+
const timeZoneId = value.timeZoneId;
449448

450449
const packableStructFields = [
451450
packer.packable(epochSecond, onError),
452451
packer.packable(nano, onError),
453-
packer.packable(zoneId, onError)
452+
packer.packable(timeZoneId, onError)
454453
];
455454
packer.packStruct(DATE_TIME_WITH_ZONE_ID, packableStructFields, onError);
456455
}
@@ -461,18 +460,18 @@ function packDateTimeWithZoneId(value, packer, onError) {
461460
* @param {number} structSize the retrieved struct size.
462461
* @param {BaseBuffer} buffer the buffer to unpack from.
463462
* @param {boolean} disableLosslessIntegers if integer properties in the result date-time should be native JS numbers.
464-
* @return {DateTimeWithZoneId} the unpacked date time with zone id value.
463+
* @return {DateTime} the unpacked date time with zone id value.
465464
*/
466465
function unpackDateTimeWithZoneId(unpacker, structSize, buffer, disableLosslessIntegers) {
467466
unpacker._verifyStructSize('DateTimeWithZoneId', DATE_TIME_WITH_ZONE_ID_STRUCT_SIZE, structSize);
468467

469468
const epochSecond = unpacker.unpackInteger(buffer);
470469
const nano = unpacker.unpackInteger(buffer);
471-
const zoneId = unpacker.unpack(buffer);
470+
const timeZoneId = unpacker.unpack(buffer);
472471

473472
const localDateTime = epochSecondAndNanoToLocalDateTime(epochSecond, nano);
474-
const result = new DateTimeWithZoneId(localDateTime.year, localDateTime.month, localDateTime.day,
475-
localDateTime.hour, localDateTime.minute, localDateTime.second, localDateTime.nanosecond, zoneId);
473+
const result = new DateTime(localDateTime.year, localDateTime.month, localDateTime.day,
474+
localDateTime.hour, localDateTime.minute, localDateTime.second, localDateTime.nanosecond, null, timeZoneId);
476475
return convertIntegerPropsIfNeeded(result, disableLosslessIntegers);
477476
}
478477

0 commit comments

Comments
 (0)