|
| 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 | +} |
0 commit comments