Skip to content

Commit a600c83

Browse files
committed
Added unit tests for temporal-util
1 parent 220c827 commit a600c83

File tree

2 files changed

+141
-3
lines changed

2 files changed

+141
-3
lines changed

src/v1/internal/temporal-util.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export function dateToIsoString(year, month, day) {
239239
if (isNegative) {
240240
year = year.multiply(-1);
241241
}
242-
let yearString = year.toString().padStart(4, '0');
242+
let yearString = formatNumber(year, 4);
243243
if (isNegative) {
244244
yearString = '-' + yearString;
245245
}
@@ -318,6 +318,12 @@ function floorMod(x, y) {
318318
* @return {string} formatted and possibly left-padded number as string.
319319
*/
320320
function formatNumber(num, stringLength = undefined) {
321-
const result = int(num).toString();
322-
return stringLength ? result.padStart(stringLength, '0') : result;
321+
num = int(num);
322+
const isNegative = num.isNegative();
323+
if (isNegative) {
324+
num = num.multiply(-1);
325+
}
326+
const numString = num.toString();
327+
const paddedNumString = stringLength ? numString.padStart(stringLength, '0') : numString;
328+
return isNegative ? '-' + paddedNumString : paddedNumString;
323329
}

test/internal/temporal-util.test.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/**
2+
* Copyright (c) 2002-2018 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import {int} from '../../src/v1/integer';
21+
import * as util from '../../src/v1/internal/temporal-util';
22+
import {Date} from '../../src/v1/temporal-types';
23+
import {LocalDateTime, LocalTime} from '../../src/v1';
24+
25+
describe('temporal-util', () => {
26+
27+
it('should convert date to ISO string', () => {
28+
expect(util.dateToIsoString(90, 2, 5)).toEqual('0090-02-05');
29+
expect(util.dateToIsoString(int(1), 1, int(1))).toEqual('0001-01-01');
30+
expect(util.dateToIsoString(-123, int(12), int(23))).toEqual('-0123-12-23');
31+
expect(util.dateToIsoString(int(-999), int(9), int(10))).toEqual('-0999-09-10');
32+
expect(util.dateToIsoString(1999, 12, 19)).toEqual('1999-12-19');
33+
expect(util.dateToIsoString(int(2023), int(8), int(16))).toEqual('2023-08-16');
34+
expect(util.dateToIsoString(12345, 12, 31)).toEqual('12345-12-31');
35+
expect(util.dateToIsoString(int(19191919), int(11), int(30))).toEqual('19191919-11-30');
36+
expect(util.dateToIsoString(-909090, 9, 9)).toEqual('-909090-09-09');
37+
expect(util.dateToIsoString(int(-888999777), int(7), int(26))).toEqual('-888999777-07-26');
38+
});
39+
40+
it('should convert time zone offset to ISO string', () => {
41+
expect(util.timeZoneOffsetToIsoString(0)).toEqual('Z');
42+
expect(util.timeZoneOffsetToIsoString(-0)).toEqual('Z');
43+
expect(util.timeZoneOffsetToIsoString(1)).toEqual('+00:00:01');
44+
expect(util.timeZoneOffsetToIsoString(-1)).toEqual('-00:00:01');
45+
expect(util.timeZoneOffsetToIsoString(20 * 60)).toEqual('+00:20');
46+
expect(util.timeZoneOffsetToIsoString(-12 * 60)).toEqual('-00:12');
47+
expect(util.timeZoneOffsetToIsoString(8 * 60 * 60)).toEqual('+08:00');
48+
expect(util.timeZoneOffsetToIsoString(-13 * 60 * 60)).toEqual('-13:00');
49+
expect(util.timeZoneOffsetToIsoString(8 * 60 * 60 + 59 * 60)).toEqual('+08:59');
50+
expect(util.timeZoneOffsetToIsoString(-12 * 60 * 60 - 31 * 60)).toEqual('-12:31');
51+
expect(util.timeZoneOffsetToIsoString(2 * 60 * 60 + 9 * 60 + 17)).toEqual('+02:09:17');
52+
expect(util.timeZoneOffsetToIsoString(-7 * 60 * 60 - 18 * 60 - 54)).toEqual('-07:18:54');
53+
});
54+
55+
it('should convert time to ISO string', () => {
56+
expect(util.timeToIsoString(8, 9, 1, 0)).toEqual('08:09:01.000000000');
57+
expect(util.timeToIsoString(int(2), int(4), int(6), int(7))).toEqual('02:04:06.000000007');
58+
expect(util.timeToIsoString(22, 19, 7, 999)).toEqual('22:19:07.000000999');
59+
expect(util.timeToIsoString(int(17), int(2), int(59), int(909090))).toEqual('17:02:59.000909090');
60+
expect(util.timeToIsoString(23, 59, 59, 999999991)).toEqual('23:59:59.999999991');
61+
expect(util.timeToIsoString(int(23), int(22), int(21), int(111222333))).toEqual('23:22:21.111222333');
62+
});
63+
64+
it('should convert duration to ISO string', () => {
65+
expect(util.durationToIsoString(0, 0, 0, 0)).toEqual('P0M0DT0.000000000S');
66+
expect(util.durationToIsoString(0, 0, 0, 123)).toEqual('P0M0DT0.000000123S');
67+
expect(util.durationToIsoString(11, 99, 100, 99901)).toEqual('P11M99DT100.000099901S');
68+
expect(util.durationToIsoString(int(3), int(9191), int(17), int(123456789))).toEqual('P3M9191DT17.123456789S');
69+
expect(util.durationToIsoString(-5, 2, -13, 123)).toEqual('P-5M2DT-13.000000123S');
70+
});
71+
72+
it('should convert epoch day to cypher date', () => {
73+
expect(util.epochDayToDate(-719528)).toEqual(date(0, 1, 1));
74+
expect(util.epochDayToDate(-135153)).toEqual(date(1599, 12, 19));
75+
expect(util.epochDayToDate(7905)).toEqual(date(1991, 8, 24));
76+
expect(util.epochDayToDate(int(48210))).toEqual(date(2101, 12, 30));
77+
expect(util.epochDayToDate(int(-4310226))).toEqual(date(-9831, 1, 1));
78+
});
79+
80+
it('should convert cypher date to epoch day', () => {
81+
expect(util.dateToEpochDay(date(-13, 12, 31))).toEqual(int(-723912));
82+
expect(util.dateToEpochDay(date(9, 9, 9))).toEqual(int(-715989));
83+
expect(util.dateToEpochDay(date(2015, 2, 17))).toEqual(int(16483));
84+
expect(util.dateToEpochDay(date(2189, 7, 19))).toEqual(int(80188));
85+
expect(util.dateToEpochDay(date(19999, 9, 28))).toEqual(int(6585227));
86+
});
87+
88+
it('should convert epoch second with nano to cypher local date-time', () => {
89+
expect(util.epochSecondAndNanoToLocalDateTime(653165977, 999)).toEqual(localDateTime(1990, 9, 12, 18, 59, 37, 999));
90+
expect(util.epochSecondAndNanoToLocalDateTime(-62703676801, 12345)).toEqual(localDateTime(-18, 12, 31, 23, 59, 59, 12345));
91+
expect(util.epochSecondAndNanoToLocalDateTime(2678400, int(1))).toEqual(localDateTime(1970, 2, 1, 0, 0, 0, 1));
92+
expect(util.epochSecondAndNanoToLocalDateTime(int(3065493882737), int(1794673))).toEqual(localDateTime(99111, 8, 21, 6, 32, 17, 1794673));
93+
expect(util.epochSecondAndNanoToLocalDateTime(int(-37428234001), 999999111)).toEqual(localDateTime(783, 12, 12, 20, 19, 59, 999999111));
94+
});
95+
96+
it('should convert cypher local date-time to epoch second', () => {
97+
expect(util.localDateTimeToEpochSecond(localDateTime(1990, 9, 12, 18, 59, 37, 999))).toEqual(int(653165977));
98+
expect(util.localDateTimeToEpochSecond(localDateTime(-18, 12, 31, 23, 59, 59, 12345))).toEqual(int(-62703676801));
99+
expect(util.localDateTimeToEpochSecond(localDateTime(1970, 2, 1, 0, 0, 0, 1))).toEqual(int(2678400));
100+
expect(util.localDateTimeToEpochSecond(localDateTime(99111, 8, 21, 6, 32, 17, 1794673))).toEqual(int(3065493882737));
101+
expect(util.localDateTimeToEpochSecond(localDateTime(783, 12, 12, 20, 19, 59, 999999111))).toEqual(int(-37428234001));
102+
});
103+
104+
it('should convert nanosecond of the day to cypher local time', () => {
105+
expect(util.nanoOfDayToLocalTime(68079000012399)).toEqual(localTime(18, 54, 39, 12399));
106+
expect(util.nanoOfDayToLocalTime(0)).toEqual(localTime(0, 0, 0, 0));
107+
expect(util.nanoOfDayToLocalTime(1)).toEqual(localTime(0, 0, 0, 1));
108+
expect(util.nanoOfDayToLocalTime(int(86399999999999))).toEqual(localTime(23, 59, 59, 999999999));
109+
expect(util.nanoOfDayToLocalTime(int(46277000808080))).toEqual(localTime(12, 51, 17, 808080));
110+
});
111+
112+
it('should convert cypher local time to nanosecond of the day', () => {
113+
expect(util.localTimeToNanoOfDay(localTime(18, 54, 39, 12399))).toEqual(int(68079000012399));
114+
expect(util.localTimeToNanoOfDay(localTime(0, 0, 0, 0))).toEqual(int(0));
115+
expect(util.localTimeToNanoOfDay(localTime(0, 0, 0, 1))).toEqual(int(1));
116+
expect(util.localTimeToNanoOfDay(localTime(23, 59, 59, 999999999))).toEqual(int(86399999999999));
117+
expect(util.localTimeToNanoOfDay(localTime(12, 51, 17, 808080))).toEqual(int(46277000808080));
118+
});
119+
120+
});
121+
122+
function date(year, month, day) {
123+
return new Date(int(year), int(month), int(day));
124+
}
125+
126+
function localTime(hour, minute, second, nanosecond) {
127+
return new LocalTime(int(hour), int(minute), int(second), int(nanosecond));
128+
}
129+
130+
function localDateTime(year, month, day, hour, minute, second, nanosecond) {
131+
return new LocalDateTime(date(year, month, day), localTime(hour, minute, second, nanosecond));
132+
}

0 commit comments

Comments
 (0)