Skip to content

Commit e91e891

Browse files
committed
Remove circular dependencies
1 parent 50a3424 commit e91e891

File tree

5 files changed

+255
-171
lines changed

5 files changed

+255
-171
lines changed

src/internal/packstream-v2.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ import {
3333
import { int, isInt } from '../integer'
3434
import {
3535
dateToEpochDay,
36+
localDateTimeToEpochSecond,
37+
localTimeToNanoOfDay
38+
} from './temporal-util'
39+
40+
import {
3641
epochDayToDate,
3742
epochSecondAndNanoToLocalDateTime,
38-
localDateTimeToEpochSecond,
39-
localTimeToNanoOfDay,
4043
nanoOfDayToLocalTime
41-
} from './temporal-util'
44+
} from './temporal-factory'
4245

4346
const POINT_2D = 0x58
4447
const POINT_2D_STRUCT_SIZE = 3

src/internal/temporal-factory.js

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* Copyright (c) 2002-2020 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.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+
import { Date, LocalDateTime, LocalTime } from '../temporal-types'
20+
import { int } from '../integer'
21+
import {
22+
DAYS_0000_TO_1970,
23+
DAYS_PER_400_YEAR_CYCLE,
24+
NANOS_PER_HOUR,
25+
NANOS_PER_MINUTE,
26+
NANOS_PER_SECOND,
27+
SECONDS_PER_DAY,
28+
floorDiv,
29+
floorMod
30+
} from './temporal-util'
31+
32+
/**
33+
* Converts given epoch day to a local date.
34+
* @param {Integer|number|string} epochDay the epoch day to convert.
35+
* @return {Date} the date representing the epoch day in years, months and days.
36+
*/
37+
export function epochDayToDate (epochDay) {
38+
epochDay = int(epochDay)
39+
40+
let zeroDay = epochDay.add(DAYS_0000_TO_1970).subtract(60)
41+
let adjust = int(0)
42+
if (zeroDay.lessThan(0)) {
43+
const adjustCycles = zeroDay
44+
.add(1)
45+
.div(DAYS_PER_400_YEAR_CYCLE)
46+
.subtract(1)
47+
adjust = adjustCycles.multiply(400)
48+
zeroDay = zeroDay.add(adjustCycles.multiply(-DAYS_PER_400_YEAR_CYCLE))
49+
}
50+
let year = zeroDay
51+
.multiply(400)
52+
.add(591)
53+
.div(DAYS_PER_400_YEAR_CYCLE)
54+
let dayOfYearEst = zeroDay.subtract(
55+
year
56+
.multiply(365)
57+
.add(year.div(4))
58+
.subtract(year.div(100))
59+
.add(year.div(400))
60+
)
61+
if (dayOfYearEst.lessThan(0)) {
62+
year = year.subtract(1)
63+
dayOfYearEst = zeroDay.subtract(
64+
year
65+
.multiply(365)
66+
.add(year.div(4))
67+
.subtract(year.div(100))
68+
.add(year.div(400))
69+
)
70+
}
71+
year = year.add(adjust)
72+
const marchDayOfYear = dayOfYearEst
73+
74+
const marchMonth = marchDayOfYear
75+
.multiply(5)
76+
.add(2)
77+
.div(153)
78+
const month = marchMonth
79+
.add(2)
80+
.modulo(12)
81+
.add(1)
82+
const day = marchDayOfYear
83+
.subtract(
84+
marchMonth
85+
.multiply(306)
86+
.add(5)
87+
.div(10)
88+
)
89+
.add(1)
90+
year = year.add(marchMonth.div(10))
91+
92+
return new Date(year, month, day)
93+
}
94+
95+
/**
96+
* Converts nanoseconds of the day into local time.
97+
* @param {Integer|number|string} nanoOfDay the nanoseconds of the day to convert.
98+
* @return {LocalTime} the local time representing given nanoseconds of the day.
99+
*/
100+
export function nanoOfDayToLocalTime (nanoOfDay) {
101+
nanoOfDay = int(nanoOfDay)
102+
103+
const hour = nanoOfDay.div(NANOS_PER_HOUR)
104+
nanoOfDay = nanoOfDay.subtract(hour.multiply(NANOS_PER_HOUR))
105+
106+
const minute = nanoOfDay.div(NANOS_PER_MINUTE)
107+
nanoOfDay = nanoOfDay.subtract(minute.multiply(NANOS_PER_MINUTE))
108+
109+
const second = nanoOfDay.div(NANOS_PER_SECOND)
110+
const nanosecond = nanoOfDay.subtract(second.multiply(NANOS_PER_SECOND))
111+
112+
return new LocalTime(hour, minute, second, nanosecond)
113+
}
114+
115+
/**
116+
* Converts given epoch second and nanosecond adjustment into a local date time object.
117+
* @param {Integer|number|string} epochSecond the epoch second to use.
118+
* @param {Integer|number|string} nano the nanosecond to use.
119+
* @return {LocalDateTime} the local date time representing given epoch second and nano.
120+
*/
121+
export function epochSecondAndNanoToLocalDateTime (epochSecond, nano) {
122+
const epochDay = floorDiv(epochSecond, SECONDS_PER_DAY)
123+
const secondsOfDay = floorMod(epochSecond, SECONDS_PER_DAY)
124+
const nanoOfDay = secondsOfDay.multiply(NANOS_PER_SECOND).add(nano)
125+
126+
const localDate = epochDayToDate(epochDay)
127+
const localTime = nanoOfDayToLocalTime(nanoOfDay)
128+
return new LocalDateTime(
129+
localDate.year,
130+
localDate.month,
131+
localDate.day,
132+
localTime.hour,
133+
localTime.minute,
134+
localTime.second,
135+
localTime.nanosecond
136+
)
137+
}

src/internal/temporal-util.js

Lines changed: 20 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919

2020
import { int, isInt } from '../integer'
21-
import { Date, LocalDateTime, LocalTime } from '../temporal-types'
2221
import { assertNumberOrInteger } from './util'
2322
import { newError } from '../error'
2423

@@ -57,24 +56,24 @@ class ValueRange {
5756
}
5857
}
5958

60-
const YEAR_RANGE = new ValueRange(-999999999, 999999999)
61-
const MONTH_OF_YEAR_RANGE = new ValueRange(1, 12)
62-
const DAY_OF_MONTH_RANGE = new ValueRange(1, 31)
63-
const HOUR_OF_DAY_RANGE = new ValueRange(0, 23)
64-
const MINUTE_OF_HOUR_RANGE = new ValueRange(0, 59)
65-
const SECOND_OF_MINUTE_RANGE = new ValueRange(0, 59)
66-
const NANOSECOND_OF_SECOND_RANGE = new ValueRange(0, 999999999)
67-
68-
const MINUTES_PER_HOUR = 60
69-
const SECONDS_PER_MINUTE = 60
70-
const SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR
71-
const NANOS_PER_SECOND = 1000000000
72-
const NANOS_PER_MILLISECOND = 1000000
73-
const NANOS_PER_MINUTE = NANOS_PER_SECOND * SECONDS_PER_MINUTE
74-
const NANOS_PER_HOUR = NANOS_PER_MINUTE * MINUTES_PER_HOUR
75-
const DAYS_0000_TO_1970 = 719528
76-
const DAYS_PER_400_YEAR_CYCLE = 146097
77-
const SECONDS_PER_DAY = 86400
59+
export const YEAR_RANGE = new ValueRange(-999999999, 999999999)
60+
export const MONTH_OF_YEAR_RANGE = new ValueRange(1, 12)
61+
export const DAY_OF_MONTH_RANGE = new ValueRange(1, 31)
62+
export const HOUR_OF_DAY_RANGE = new ValueRange(0, 23)
63+
export const MINUTE_OF_HOUR_RANGE = new ValueRange(0, 59)
64+
export const SECOND_OF_MINUTE_RANGE = new ValueRange(0, 59)
65+
export const NANOSECOND_OF_SECOND_RANGE = new ValueRange(0, 999999999)
66+
67+
export const MINUTES_PER_HOUR = 60
68+
export const SECONDS_PER_MINUTE = 60
69+
export const SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR
70+
export const NANOS_PER_SECOND = 1000000000
71+
export const NANOS_PER_MILLISECOND = 1000000
72+
export const NANOS_PER_MINUTE = NANOS_PER_SECOND * SECONDS_PER_MINUTE
73+
export const NANOS_PER_HOUR = NANOS_PER_MINUTE * MINUTES_PER_HOUR
74+
export const DAYS_0000_TO_1970 = 719528
75+
export const DAYS_PER_400_YEAR_CYCLE = 146097
76+
export const SECONDS_PER_DAY = 86400
7877

7978
export function normalizeSecondsForDuration (seconds, nanoseconds) {
8079
return int(seconds).add(floorDiv(nanoseconds, NANOS_PER_SECOND))
@@ -104,26 +103,6 @@ export function localTimeToNanoOfDay (hour, minute, second, nanosecond) {
104103
return totalNanos.add(nanosecond)
105104
}
106105

107-
/**
108-
* Converts nanoseconds of the day into local time.
109-
* @param {Integer|number|string} nanoOfDay the nanoseconds of the day to convert.
110-
* @return {LocalTime} the local time representing given nanoseconds of the day.
111-
*/
112-
export function nanoOfDayToLocalTime (nanoOfDay) {
113-
nanoOfDay = int(nanoOfDay)
114-
115-
const hour = nanoOfDay.div(NANOS_PER_HOUR)
116-
nanoOfDay = nanoOfDay.subtract(hour.multiply(NANOS_PER_HOUR))
117-
118-
const minute = nanoOfDay.div(NANOS_PER_MINUTE)
119-
nanoOfDay = nanoOfDay.subtract(minute.multiply(NANOS_PER_MINUTE))
120-
121-
const second = nanoOfDay.div(NANOS_PER_SECOND)
122-
const nanosecond = nanoOfDay.subtract(second.multiply(NANOS_PER_SECOND))
123-
124-
return new LocalTime(hour, minute, second, nanosecond)
125-
}
126-
127106
/**
128107
* Converts given local date time into a single integer representing this same time in epoch seconds UTC.
129108
* @param {Integer|number|string} year the year of the local date-time to convert.
@@ -149,30 +128,6 @@ export function localDateTimeToEpochSecond (
149128
return epochDay.multiply(SECONDS_PER_DAY).add(localTimeSeconds)
150129
}
151130

152-
/**
153-
* Converts given epoch second and nanosecond adjustment into a local date time object.
154-
* @param {Integer|number|string} epochSecond the epoch second to use.
155-
* @param {Integer|number|string} nano the nanosecond to use.
156-
* @return {LocalDateTime} the local date time representing given epoch second and nano.
157-
*/
158-
export function epochSecondAndNanoToLocalDateTime (epochSecond, nano) {
159-
const epochDay = floorDiv(epochSecond, SECONDS_PER_DAY)
160-
const secondsOfDay = floorMod(epochSecond, SECONDS_PER_DAY)
161-
const nanoOfDay = secondsOfDay.multiply(NANOS_PER_SECOND).add(nano)
162-
163-
const localDate = epochDayToDate(epochDay)
164-
const localTime = nanoOfDayToLocalTime(nanoOfDay)
165-
return new LocalDateTime(
166-
localDate.year,
167-
localDate.month,
168-
localDate.day,
169-
localTime.hour,
170-
localTime.minute,
171-
localTime.second,
172-
localTime.nanosecond
173-
)
174-
}
175-
176131
/**
177132
* Converts given local date into a single integer representing it's epoch day.
178133
* @param {Integer|number|string} year the year of the local date to convert.
@@ -220,69 +175,6 @@ export function dateToEpochDay (year, month, day) {
220175
return epochDay.subtract(DAYS_0000_TO_1970)
221176
}
222177

223-
/**
224-
* Converts given epoch day to a local date.
225-
* @param {Integer|number|string} epochDay the epoch day to convert.
226-
* @return {Date} the date representing the epoch day in years, months and days.
227-
*/
228-
export function epochDayToDate (epochDay) {
229-
epochDay = int(epochDay)
230-
231-
let zeroDay = epochDay.add(DAYS_0000_TO_1970).subtract(60)
232-
let adjust = int(0)
233-
if (zeroDay.lessThan(0)) {
234-
const adjustCycles = zeroDay
235-
.add(1)
236-
.div(DAYS_PER_400_YEAR_CYCLE)
237-
.subtract(1)
238-
adjust = adjustCycles.multiply(400)
239-
zeroDay = zeroDay.add(adjustCycles.multiply(-DAYS_PER_400_YEAR_CYCLE))
240-
}
241-
let year = zeroDay
242-
.multiply(400)
243-
.add(591)
244-
.div(DAYS_PER_400_YEAR_CYCLE)
245-
let dayOfYearEst = zeroDay.subtract(
246-
year
247-
.multiply(365)
248-
.add(year.div(4))
249-
.subtract(year.div(100))
250-
.add(year.div(400))
251-
)
252-
if (dayOfYearEst.lessThan(0)) {
253-
year = year.subtract(1)
254-
dayOfYearEst = zeroDay.subtract(
255-
year
256-
.multiply(365)
257-
.add(year.div(4))
258-
.subtract(year.div(100))
259-
.add(year.div(400))
260-
)
261-
}
262-
year = year.add(adjust)
263-
const marchDayOfYear = dayOfYearEst
264-
265-
const marchMonth = marchDayOfYear
266-
.multiply(5)
267-
.add(2)
268-
.div(153)
269-
const month = marchMonth
270-
.add(2)
271-
.modulo(12)
272-
.add(1)
273-
const day = marchDayOfYear
274-
.subtract(
275-
marchMonth
276-
.multiply(306)
277-
.add(5)
278-
.div(10)
279-
)
280-
.add(1)
281-
year = year.add(marchMonth.div(10))
282-
283-
return new Date(year, month, day)
284-
}
285-
286178
/**
287179
* Format given duration to an ISO 8601 string.
288180
* @param {Integer|number|string} months the number of months.
@@ -529,7 +421,7 @@ function isLeapYear (year) {
529421
* @param {Integer|number|string} y the divisor.
530422
* @return {Integer} the result.
531423
*/
532-
function floorDiv (x, y) {
424+
export function floorDiv (x, y) {
533425
x = int(x)
534426
y = int(y)
535427

@@ -545,7 +437,7 @@ function floorDiv (x, y) {
545437
* @param {Integer|number|string} y the divisor.
546438
* @return {Integer} the result.
547439
*/
548-
function floorMod (x, y) {
440+
export function floorMod (x, y) {
549441
x = int(x)
550442
y = int(y)
551443

0 commit comments

Comments
 (0)