@ragestudio/scylla-odm 0.22.2 → 0.22.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/batch/index.d.ts +3 -3
- package/batch/index.d.ts.map +1 -1
- package/client.d.ts +6 -5
- package/client.d.ts.map +1 -1
- package/client.js +7 -7
- package/client.js.map +1 -1
- package/cql_gen/create_table.d.ts +1 -1
- package/cql_gen/create_table.d.ts.map +1 -1
- package/document/index.d.ts +3 -3
- package/document/index.d.ts.map +1 -1
- package/driver/LICENSE.txt +177 -0
- package/driver/NOTICE.txt +67 -0
- package/driver/auth/index.d.ts +37 -0
- package/driver/auth/index.js +37 -0
- package/driver/auth/no-auth-provider.js +73 -0
- package/driver/auth/plain-text-auth-provider.js +81 -0
- package/driver/auth/provider.js +77 -0
- package/driver/client-options.js +442 -0
- package/driver/client.js +1267 -0
- package/driver/concurrent/index.d.ts +49 -0
- package/driver/concurrent/index.js +366 -0
- package/driver/connection.js +1034 -0
- package/driver/control-connection.js +1282 -0
- package/driver/encoder.js +2316 -0
- package/driver/errors.js +223 -0
- package/driver/execution-options.js +612 -0
- package/driver/execution-profile.js +274 -0
- package/driver/host-connection-pool.js +587 -0
- package/driver/host.js +699 -0
- package/driver/index.d.ts +387 -0
- package/driver/index.js +81 -0
- package/driver/mapping/cache.js +214 -0
- package/driver/mapping/doc-info-adapter.js +171 -0
- package/driver/mapping/index.d.ts +219 -0
- package/driver/mapping/index.js +57 -0
- package/driver/mapping/mapper.js +225 -0
- package/driver/mapping/mapping-handler.js +641 -0
- package/driver/mapping/model-batch-item.js +215 -0
- package/driver/mapping/model-batch-mapper.js +141 -0
- package/driver/mapping/model-mapper.js +315 -0
- package/driver/mapping/model-mapping-info.js +225 -0
- package/driver/mapping/object-selector.js +417 -0
- package/driver/mapping/q.js +156 -0
- package/driver/mapping/query-generator.js +556 -0
- package/driver/mapping/result-mapper.js +123 -0
- package/driver/mapping/result.js +139 -0
- package/driver/mapping/table-mappings.js +133 -0
- package/driver/mapping/tree.js +160 -0
- package/driver/metadata/aggregate.js +79 -0
- package/driver/metadata/client-state.js +119 -0
- package/driver/metadata/data-collection.js +182 -0
- package/driver/metadata/event-debouncer.js +174 -0
- package/driver/metadata/index.d.ts +276 -0
- package/driver/metadata/index.js +1156 -0
- package/driver/metadata/materialized-view.js +49 -0
- package/driver/metadata/schema-function.js +98 -0
- package/driver/metadata/schema-index.js +166 -0
- package/driver/metadata/schema-parser.js +1399 -0
- package/driver/metadata/table-metadata.js +77 -0
- package/driver/operation-state.js +206 -0
- package/driver/policies/address-resolution.js +145 -0
- package/driver/policies/index.d.ts +241 -0
- package/driver/policies/index.js +110 -0
- package/driver/policies/load-balancing.js +970 -0
- package/driver/policies/reconnection.js +166 -0
- package/driver/policies/retry.js +326 -0
- package/driver/policies/speculative-execution.js +150 -0
- package/driver/policies/timestamp-generation.js +176 -0
- package/driver/prepare-handler.js +347 -0
- package/driver/promise-utils.js +191 -0
- package/driver/readers.js +624 -0
- package/driver/request-execution.js +644 -0
- package/driver/request-handler.js +332 -0
- package/driver/requests.js +618 -0
- package/driver/stream-id-stack.js +209 -0
- package/driver/streams.js +745 -0
- package/driver/token.js +325 -0
- package/driver/tokenizer.js +631 -0
- package/driver/types/big-decimal.js +282 -0
- package/driver/types/duration.js +576 -0
- package/driver/types/index.d.ts +486 -0
- package/driver/types/index.js +733 -0
- package/driver/types/inet-address.js +262 -0
- package/driver/types/integer.js +818 -0
- package/driver/types/local-date.js +280 -0
- package/driver/types/local-time.js +299 -0
- package/driver/types/mutable-long.js +385 -0
- package/driver/types/protocol-version.js +391 -0
- package/driver/types/result-set.js +287 -0
- package/driver/types/result-stream.js +164 -0
- package/driver/types/row.js +85 -0
- package/driver/types/time-uuid.js +414 -0
- package/driver/types/tuple.js +103 -0
- package/driver/types/uuid.js +160 -0
- package/driver/types/vector.js +130 -0
- package/driver/types/version-number.js +153 -0
- package/driver/utils.js +1485 -0
- package/driver/writers.js +350 -0
- package/global.d.ts +1 -1
- package/global.d.ts.map +1 -1
- package/index.d.ts +6 -6
- package/index.d.ts.map +1 -1
- package/index.js +6 -6
- package/index.js.map +1 -1
- package/migrate/index.d.ts +1 -1
- package/migrate/index.d.ts.map +1 -1
- package/migrate/index.js +1 -1
- package/migrate/index.js.map +1 -1
- package/model/index.d.ts +6 -6
- package/model/index.d.ts.map +1 -1
- package/model/index.js +10 -10
- package/model/index.js.map +1 -1
- package/operations/countAll.d.ts +1 -1
- package/operations/countAll.d.ts.map +1 -1
- package/operations/delete.d.ts +3 -4
- package/operations/delete.d.ts.map +1 -1
- package/operations/delete.js +1 -1
- package/operations/delete.js.map +1 -1
- package/operations/find.d.ts +2 -2
- package/operations/find.d.ts.map +1 -1
- package/operations/find.js +1 -1
- package/operations/find.js.map +1 -1
- package/operations/findOne.d.ts +2 -2
- package/operations/findOne.d.ts.map +1 -1
- package/operations/findOne.js +1 -1
- package/operations/findOne.js.map +1 -1
- package/operations/insert.d.ts +3 -3
- package/operations/insert.d.ts.map +1 -1
- package/operations/insert.js +2 -2
- package/operations/insert.js.map +1 -1
- package/operations/sync.d.ts +1 -1
- package/operations/sync.d.ts.map +1 -1
- package/operations/sync.js +1 -1
- package/operations/sync.js.map +1 -1
- package/operations/tableExists.d.ts +1 -1
- package/operations/tableExists.d.ts.map +1 -1
- package/operations/update.d.ts +3 -3
- package/operations/update.d.ts.map +1 -1
- package/operations/update.js +2 -2
- package/operations/update.js.map +1 -1
- package/package.json +4 -12
- package/schema/index.d.ts +1 -1
- package/schema/index.d.ts.map +1 -1
- package/types.d.ts +4 -4
- package/types.d.ts.map +1 -1
- package/utils/queryParser.d.ts +1 -1
- package/utils/queryParser.d.ts.map +1 -1
- package/utils/queryParser.js +1 -1
- package/utils/queryParser.js.map +1 -1
- package/utils/typeChecker.d.ts +1 -1
- package/utils/typeChecker.d.ts.map +1 -1
- package/utils/typeChecker.js +1 -1
- package/utils/typeChecker.js.map +1 -1
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
|
4
|
+
* distributed with this work for additional information
|
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
|
7
|
+
* "License"); you may not use this file except in compliance
|
|
8
|
+
* with the License. You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
import util from "util"
|
|
19
|
+
|
|
20
|
+
import utils from "../utils.js"
|
|
21
|
+
/** @module types */
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @private
|
|
25
|
+
* @const
|
|
26
|
+
*/
|
|
27
|
+
const millisecondsPerDay = 86400000
|
|
28
|
+
/**
|
|
29
|
+
* @private
|
|
30
|
+
*/
|
|
31
|
+
const dateCenter = Math.pow(2, 31)
|
|
32
|
+
/**
|
|
33
|
+
*
|
|
34
|
+
* Creates a new instance of LocalDate.
|
|
35
|
+
* @class
|
|
36
|
+
* @classdesc A date without a time-zone in the ISO-8601 calendar system, such as 2010-08-05.
|
|
37
|
+
* <p>
|
|
38
|
+
* LocalDate is an immutable object that represents a date, often viewed as year-month-day. For example, the value "1st October 2014" can be stored in a LocalDate.
|
|
39
|
+
* </p>
|
|
40
|
+
* <p>
|
|
41
|
+
* This class does not store or represent a time or time-zone. Instead, it is a description of the date, as used for birthdays. It cannot represent an instant on the time-line without additional information such as an offset or time-zone.
|
|
42
|
+
* </p>
|
|
43
|
+
* <p>
|
|
44
|
+
* Note that this type can represent dates in the range [-5877641-06-23; 5881580-07-17] while the ES5 date type can only represent values in the range of [-271821-04-20; 275760-09-13].
|
|
45
|
+
* In the event that year, month, day parameters do not fall within the ES5 date range an Error will be thrown. If you wish to represent a date outside of this range, pass a single
|
|
46
|
+
* parameter indicating the days since epoch. For example, -1 represents 1969-12-31.
|
|
47
|
+
* </p>
|
|
48
|
+
* @param {Number} year The year or days since epoch. If days since epoch, month and day should not be provided.
|
|
49
|
+
* @param {Number} month Between 1 and 12 inclusive.
|
|
50
|
+
* @param {Number} day Between 1 and the number of days in the given month of the given year.
|
|
51
|
+
*
|
|
52
|
+
* @property {Date} date The date representation if falls within a range of an ES5 data type, otherwise an invalid date.
|
|
53
|
+
*
|
|
54
|
+
* @constructor
|
|
55
|
+
*/
|
|
56
|
+
function LocalDate(year, month, day) {
|
|
57
|
+
//implementation detail: internally uses a UTC based date
|
|
58
|
+
if (
|
|
59
|
+
typeof year === "number" &&
|
|
60
|
+
typeof month === "number" &&
|
|
61
|
+
typeof day === "number"
|
|
62
|
+
) {
|
|
63
|
+
// Use setUTCFullYear as if there is a 2 digit year, Date.UTC() assumes
|
|
64
|
+
// that is the 20th century.
|
|
65
|
+
this.date = new Date()
|
|
66
|
+
this.date.setUTCHours(0, 0, 0, 0)
|
|
67
|
+
this.date.setUTCFullYear(year, month - 1, day)
|
|
68
|
+
if (isNaN(this.date.getTime())) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
util.format(
|
|
71
|
+
"%d-%d-%d does not form a valid ES5 date!",
|
|
72
|
+
year,
|
|
73
|
+
month,
|
|
74
|
+
day,
|
|
75
|
+
),
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
} else if (typeof month === "undefined" && typeof day === "undefined") {
|
|
79
|
+
if (typeof year === "number") {
|
|
80
|
+
//in days since epoch.
|
|
81
|
+
if (year < -2147483648 || year > 2147483647) {
|
|
82
|
+
throw new Error(
|
|
83
|
+
"You must provide a valid value for days since epoch (-2147483648 <= value <= 2147483647).",
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
this.date = new Date(year * millisecondsPerDay)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (typeof this.date === "undefined") {
|
|
91
|
+
throw new Error("You must provide a valid year, month and day")
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* If date cannot be represented yet given a valid days since epoch, track
|
|
96
|
+
* it internally.
|
|
97
|
+
*/
|
|
98
|
+
this._value = isNaN(this.date.getTime()) ? year : null
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* A number representing the year. May return NaN if cannot be represented as
|
|
102
|
+
* a Date.
|
|
103
|
+
* @type Number
|
|
104
|
+
*/
|
|
105
|
+
this.year = this.date.getUTCFullYear()
|
|
106
|
+
/**
|
|
107
|
+
* A number between 1 and 12 inclusive representing the month. May return
|
|
108
|
+
* NaN if cannot be represented as a Date.
|
|
109
|
+
* @type Number
|
|
110
|
+
*/
|
|
111
|
+
this.month = this.date.getUTCMonth() + 1
|
|
112
|
+
/**
|
|
113
|
+
* A number between 1 and the number of days in the given month of the given year (28, 29, 30, 31).
|
|
114
|
+
* May return NaN if cannot be represented as a Date.
|
|
115
|
+
* @type Number
|
|
116
|
+
*/
|
|
117
|
+
this.day = this.date.getUTCDate()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Creates a new instance of LocalDate using the current year, month and day from the system clock in the default time-zone.
|
|
122
|
+
*/
|
|
123
|
+
LocalDate.now = function () {
|
|
124
|
+
return LocalDate.fromDate(new Date())
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Creates a new instance of LocalDate using the current date from the system clock at UTC.
|
|
129
|
+
*/
|
|
130
|
+
LocalDate.utcNow = function () {
|
|
131
|
+
return new LocalDate(Date.now())
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Creates a new instance of LocalDate using the year, month and day from the provided local date time.
|
|
136
|
+
* @param {Date} date
|
|
137
|
+
*/
|
|
138
|
+
LocalDate.fromDate = function (date) {
|
|
139
|
+
if (isNaN(date.getTime())) {
|
|
140
|
+
throw new TypeError("Invalid date: " + date)
|
|
141
|
+
}
|
|
142
|
+
return new LocalDate(
|
|
143
|
+
date.getFullYear(),
|
|
144
|
+
date.getMonth() + 1,
|
|
145
|
+
date.getDate(),
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Creates a new instance of LocalDate using the year, month and day provided in the form: yyyy-mm-dd or
|
|
151
|
+
* days since epoch (i.e. -1 for Dec 31, 1969).
|
|
152
|
+
* @param {String} value
|
|
153
|
+
*/
|
|
154
|
+
LocalDate.fromString = function (value) {
|
|
155
|
+
const dashCount = (value.match(/-/g) || []).length
|
|
156
|
+
if (dashCount >= 2) {
|
|
157
|
+
let multiplier = 1
|
|
158
|
+
if (value[0] === "-") {
|
|
159
|
+
value = value.substring(1)
|
|
160
|
+
multiplier = -1
|
|
161
|
+
}
|
|
162
|
+
const parts = value.split("-")
|
|
163
|
+
return new LocalDate(
|
|
164
|
+
multiplier * parseInt(parts[0], 10),
|
|
165
|
+
parseInt(parts[1], 10),
|
|
166
|
+
parseInt(parts[2], 10),
|
|
167
|
+
)
|
|
168
|
+
}
|
|
169
|
+
if (value.match(/^-?\d+$/)) {
|
|
170
|
+
// Parse as days since epoch.
|
|
171
|
+
return new LocalDate(parseInt(value, 10))
|
|
172
|
+
}
|
|
173
|
+
throw new Error("Invalid input '" + value + "'.")
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Creates a new instance of LocalDate using the bytes representation.
|
|
178
|
+
* @param {Buffer} buffer
|
|
179
|
+
*/
|
|
180
|
+
LocalDate.fromBuffer = function (buffer) {
|
|
181
|
+
//move to unix epoch: 0.
|
|
182
|
+
return new LocalDate(buffer.readUInt32BE(0) - dateCenter)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Compares this LocalDate with the given one.
|
|
187
|
+
* @param {LocalDate} other date to compare against.
|
|
188
|
+
* @return {number} 0 if they are the same, 1 if the this is greater, and -1
|
|
189
|
+
* if the given one is greater.
|
|
190
|
+
*/
|
|
191
|
+
LocalDate.prototype.compare = function (other) {
|
|
192
|
+
const thisValue = isNaN(this.date.getTime())
|
|
193
|
+
? this._value * millisecondsPerDay
|
|
194
|
+
: this.date.getTime()
|
|
195
|
+
const otherValue = isNaN(other.date.getTime())
|
|
196
|
+
? other._value * millisecondsPerDay
|
|
197
|
+
: other.date.getTime()
|
|
198
|
+
const diff = thisValue - otherValue
|
|
199
|
+
if (diff < 0) {
|
|
200
|
+
return -1
|
|
201
|
+
}
|
|
202
|
+
if (diff > 0) {
|
|
203
|
+
return 1
|
|
204
|
+
}
|
|
205
|
+
return 0
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Returns true if the value of the LocalDate instance and other are the same
|
|
210
|
+
* @param {LocalDate} other
|
|
211
|
+
* @returns {Boolean}
|
|
212
|
+
*/
|
|
213
|
+
LocalDate.prototype.equals = function (other) {
|
|
214
|
+
return other instanceof LocalDate && this.compare(other) === 0
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
LocalDate.prototype.inspect = function () {
|
|
218
|
+
return this.constructor.name + ": " + this.toString()
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Gets the bytes representation of the instance.
|
|
223
|
+
* @returns {Buffer}
|
|
224
|
+
*/
|
|
225
|
+
LocalDate.prototype.toBuffer = function () {
|
|
226
|
+
//days since unix epoch
|
|
227
|
+
const daysSinceEpoch = isNaN(this.date.getTime())
|
|
228
|
+
? this._value
|
|
229
|
+
: Math.floor(this.date.getTime() / millisecondsPerDay)
|
|
230
|
+
const value = daysSinceEpoch + dateCenter
|
|
231
|
+
const buf = utils.allocBufferUnsafe(4)
|
|
232
|
+
buf.writeUInt32BE(value, 0)
|
|
233
|
+
return buf
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Gets the string representation of the instance in the form: yyyy-mm-dd if
|
|
238
|
+
* the value can be parsed as a Date, otherwise days since epoch.
|
|
239
|
+
* @returns {String}
|
|
240
|
+
*/
|
|
241
|
+
LocalDate.prototype.toString = function () {
|
|
242
|
+
let result
|
|
243
|
+
//if cannot be parsed as date, return days since epoch representation.
|
|
244
|
+
if (isNaN(this.date.getTime())) {
|
|
245
|
+
return this._value.toString()
|
|
246
|
+
}
|
|
247
|
+
if (this.year < 0) {
|
|
248
|
+
result = "-" + fillZeros((this.year * -1).toString(), 4)
|
|
249
|
+
} else {
|
|
250
|
+
result = fillZeros(this.year.toString(), 4)
|
|
251
|
+
}
|
|
252
|
+
result +=
|
|
253
|
+
"-" +
|
|
254
|
+
fillZeros(this.month.toString(), 2) +
|
|
255
|
+
"-" +
|
|
256
|
+
fillZeros(this.day.toString(), 2)
|
|
257
|
+
return result
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Gets the string representation of the instance in the form: yyyy-mm-dd, valid for JSON.
|
|
262
|
+
* @returns {String}
|
|
263
|
+
*/
|
|
264
|
+
LocalDate.prototype.toJSON = function () {
|
|
265
|
+
return this.toString()
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* @param {String} value
|
|
270
|
+
* @param {Number} amount
|
|
271
|
+
* @private
|
|
272
|
+
*/
|
|
273
|
+
function fillZeros(value, amount) {
|
|
274
|
+
if (value.length >= amount) {
|
|
275
|
+
return value
|
|
276
|
+
}
|
|
277
|
+
return utils.stringRepeat("0", amount - value.length) + value
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export default LocalDate
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
|
4
|
+
* distributed with this work for additional information
|
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
|
7
|
+
* "License"); you may not use this file except in compliance
|
|
8
|
+
* with the License. You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
import Long from "long"
|
|
19
|
+
import util from "util"
|
|
20
|
+
import utils from "../utils.js"
|
|
21
|
+
/** @module types */
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @const
|
|
25
|
+
* @private
|
|
26
|
+
* */
|
|
27
|
+
const maxNanos = Long.fromString("86399999999999")
|
|
28
|
+
/**
|
|
29
|
+
* Nanoseconds in a second
|
|
30
|
+
* @const
|
|
31
|
+
* @private
|
|
32
|
+
* */
|
|
33
|
+
const nanoSecInSec = Long.fromNumber(1000000000)
|
|
34
|
+
/**
|
|
35
|
+
* Nanoseconds in a millisecond
|
|
36
|
+
* @const
|
|
37
|
+
* @private
|
|
38
|
+
* */
|
|
39
|
+
const nanoSecInMillis = Long.fromNumber(1000000)
|
|
40
|
+
/**
|
|
41
|
+
* Milliseconds in day
|
|
42
|
+
* @const
|
|
43
|
+
* @private
|
|
44
|
+
* */
|
|
45
|
+
const millisInDay = 86400000
|
|
46
|
+
/**
|
|
47
|
+
*
|
|
48
|
+
* Creates a new instance of LocalTime.
|
|
49
|
+
* @class
|
|
50
|
+
* @classdesc A time without a time-zone in the ISO-8601 calendar system, such as 10:30:05.
|
|
51
|
+
* <p>
|
|
52
|
+
* LocalTime is an immutable date-time object that represents a time, often viewed as hour-minute-second. Time is represented to nanosecond precision. For example, the value "13:45.30.123456789" can be stored in a LocalTime.
|
|
53
|
+
* </p>
|
|
54
|
+
* @param {Long} totalNanoseconds Total nanoseconds since midnight.
|
|
55
|
+
* @constructor
|
|
56
|
+
*/
|
|
57
|
+
function LocalTime(totalNanoseconds) {
|
|
58
|
+
if (!(totalNanoseconds instanceof Long)) {
|
|
59
|
+
throw new Error("You must specify a Long value as totalNanoseconds")
|
|
60
|
+
}
|
|
61
|
+
if (
|
|
62
|
+
totalNanoseconds.lessThan(Long.ZERO) ||
|
|
63
|
+
totalNanoseconds.greaterThan(maxNanos)
|
|
64
|
+
) {
|
|
65
|
+
throw new Error("Total nanoseconds out of range")
|
|
66
|
+
}
|
|
67
|
+
this.value = totalNanoseconds
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Gets the hour component of the time represented by the current instance, a number from 0 to 23.
|
|
71
|
+
* @type Number
|
|
72
|
+
*/
|
|
73
|
+
this.hour = this._getParts()[0]
|
|
74
|
+
/**
|
|
75
|
+
* Gets the minute component of the time represented by the current instance, a number from 0 to 59.
|
|
76
|
+
* @type Number
|
|
77
|
+
*/
|
|
78
|
+
this.minute = this._getParts()[1]
|
|
79
|
+
/**
|
|
80
|
+
* Gets the second component of the time represented by the current instance, a number from 0 to 59.
|
|
81
|
+
* @type Number
|
|
82
|
+
*/
|
|
83
|
+
this.second = this._getParts()[2]
|
|
84
|
+
/**
|
|
85
|
+
* Gets the nanoseconds component of the time represented by the current instance, a number from 0 to 999999999.
|
|
86
|
+
* @type Number
|
|
87
|
+
*/
|
|
88
|
+
this.nanosecond = this._getParts()[3]
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Parses an string representation and returns a new LocalDate.
|
|
93
|
+
* @param {String} value
|
|
94
|
+
* @returns {LocalTime}
|
|
95
|
+
*/
|
|
96
|
+
LocalTime.fromString = function (value) {
|
|
97
|
+
if (typeof value !== "string") {
|
|
98
|
+
throw new Error("Argument type invalid: " + util.inspect(value))
|
|
99
|
+
}
|
|
100
|
+
const parts = value.split(":")
|
|
101
|
+
let millis =
|
|
102
|
+
parseInt(parts[0], 10) * 3600000 + parseInt(parts[1], 10) * 60000
|
|
103
|
+
let nanos
|
|
104
|
+
if (parts.length === 3) {
|
|
105
|
+
const secParts = parts[2].split(".")
|
|
106
|
+
millis += parseInt(secParts[0], 10) * 1000
|
|
107
|
+
if (secParts.length === 2) {
|
|
108
|
+
nanos = secParts[1]
|
|
109
|
+
//add zeros at the end
|
|
110
|
+
nanos = nanos + utils.stringRepeat("0", 9 - nanos.length)
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return LocalTime.fromMilliseconds(millis, parseInt(nanos, 10) || 0)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Uses the current local time (in milliseconds) and the nanoseconds to create a new instance of LocalTime
|
|
118
|
+
* @param {Number} [nanoseconds] A Number from 0 to 999,999, representing the time nanosecond portion.
|
|
119
|
+
* @returns {LocalTime}
|
|
120
|
+
*/
|
|
121
|
+
LocalTime.now = function (nanoseconds) {
|
|
122
|
+
return LocalTime.fromDate(new Date(), nanoseconds)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Uses the provided local time (in milliseconds) and the nanoseconds to create a new instance of LocalTime
|
|
127
|
+
* @param {Date} date Local date portion to extract the time passed since midnight.
|
|
128
|
+
* @param {Number} [nanoseconds] A Number from 0 to 999,999, representing the nanosecond time portion.
|
|
129
|
+
* @returns {LocalTime}
|
|
130
|
+
*/
|
|
131
|
+
LocalTime.fromDate = function (date, nanoseconds) {
|
|
132
|
+
if (!(date instanceof Date)) {
|
|
133
|
+
throw new Error("Not a valid date")
|
|
134
|
+
}
|
|
135
|
+
//Use the local representation, only the milliseconds portion
|
|
136
|
+
const millis =
|
|
137
|
+
(date.getTime() + date.getTimezoneOffset() * -60000) % millisInDay
|
|
138
|
+
return LocalTime.fromMilliseconds(millis, nanoseconds)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Uses the provided local time (in milliseconds) and the nanoseconds to create a new instance of LocalTime
|
|
143
|
+
* @param {Number} milliseconds A Number from 0 to 86,399,999.
|
|
144
|
+
* @param {Number} [nanoseconds] A Number from 0 to 999,999, representing the time nanosecond portion.
|
|
145
|
+
* @returns {LocalTime}
|
|
146
|
+
*/
|
|
147
|
+
LocalTime.fromMilliseconds = function (milliseconds, nanoseconds) {
|
|
148
|
+
if (typeof nanoseconds !== "number") {
|
|
149
|
+
nanoseconds = 0
|
|
150
|
+
}
|
|
151
|
+
return new LocalTime(
|
|
152
|
+
Long.fromNumber(milliseconds)
|
|
153
|
+
.multiply(nanoSecInMillis)
|
|
154
|
+
.add(Long.fromNumber(nanoseconds)),
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Creates a new instance of LocalTime from the bytes representation.
|
|
160
|
+
* @param {Buffer} value
|
|
161
|
+
* @returns {LocalTime}
|
|
162
|
+
*/
|
|
163
|
+
LocalTime.fromBuffer = function (value) {
|
|
164
|
+
if (!(value instanceof Buffer)) {
|
|
165
|
+
throw new TypeError("Expected Buffer, obtained " + util.inspect(value))
|
|
166
|
+
}
|
|
167
|
+
return new LocalTime(new Long(value.readInt32BE(4), value.readInt32BE(0)))
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Compares this LocalTime with the given one.
|
|
172
|
+
* @param {LocalTime} other time to compare against.
|
|
173
|
+
* @return {number} 0 if they are the same, 1 if the this is greater, and -1
|
|
174
|
+
* if the given one is greater.
|
|
175
|
+
*/
|
|
176
|
+
LocalTime.prototype.compare = function (other) {
|
|
177
|
+
return this.value.compare(other.value)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Returns true if the value of the LocalTime instance and other are the same
|
|
182
|
+
* @param {LocalTime} other
|
|
183
|
+
* @returns {Boolean}
|
|
184
|
+
*/
|
|
185
|
+
LocalTime.prototype.equals = function (other) {
|
|
186
|
+
return other instanceof LocalTime && this.compare(other) === 0
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Gets the total amount of nanoseconds since midnight for this instance.
|
|
191
|
+
* @returns {Long}
|
|
192
|
+
*/
|
|
193
|
+
LocalTime.prototype.getTotalNanoseconds = function () {
|
|
194
|
+
return this.value
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
LocalTime.prototype.inspect = function () {
|
|
198
|
+
return this.constructor.name + ": " + this.toString()
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Returns a big-endian bytes representation of the instance
|
|
203
|
+
* @returns {Buffer}
|
|
204
|
+
*/
|
|
205
|
+
LocalTime.prototype.toBuffer = function () {
|
|
206
|
+
const buffer = utils.allocBufferUnsafe(8)
|
|
207
|
+
buffer.writeUInt32BE(this.value.getHighBitsUnsigned(), 0)
|
|
208
|
+
buffer.writeUInt32BE(this.value.getLowBitsUnsigned(), 4)
|
|
209
|
+
return buffer
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Returns the string representation of the instance in the form of hh:MM:ss.ns
|
|
214
|
+
* @returns {String}
|
|
215
|
+
*/
|
|
216
|
+
LocalTime.prototype.toString = function () {
|
|
217
|
+
return formatTime(this._getParts())
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Gets the string representation of the instance in the form: hh:MM:ss.ns
|
|
222
|
+
* @returns {String}
|
|
223
|
+
*/
|
|
224
|
+
LocalTime.prototype.toJSON = function () {
|
|
225
|
+
return this.toString()
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* @returns {Array.<Number>}
|
|
230
|
+
* @ignore
|
|
231
|
+
*/
|
|
232
|
+
LocalTime.prototype._getParts = function () {
|
|
233
|
+
if (!this._partsCache) {
|
|
234
|
+
//hours, minutes, seconds and nanos
|
|
235
|
+
const parts = [0, 0, 0, 0]
|
|
236
|
+
const secs = this.value.div(nanoSecInSec)
|
|
237
|
+
//faster modulo
|
|
238
|
+
//total nanos
|
|
239
|
+
parts[3] = this.value.subtract(secs.multiply(nanoSecInSec)).toNumber()
|
|
240
|
+
//seconds
|
|
241
|
+
parts[2] = secs.toNumber()
|
|
242
|
+
if (parts[2] >= 60) {
|
|
243
|
+
//minutes
|
|
244
|
+
parts[1] = Math.floor(parts[2] / 60)
|
|
245
|
+
parts[2] = parts[2] % 60
|
|
246
|
+
}
|
|
247
|
+
if (parts[1] >= 60) {
|
|
248
|
+
//hours
|
|
249
|
+
parts[0] = Math.floor(parts[1] / 60)
|
|
250
|
+
parts[1] = parts[1] % 60
|
|
251
|
+
}
|
|
252
|
+
this._partsCache = parts
|
|
253
|
+
}
|
|
254
|
+
return this._partsCache
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* @param {Array.<Number>} values
|
|
259
|
+
* @private
|
|
260
|
+
*/
|
|
261
|
+
function formatTime(values) {
|
|
262
|
+
let result
|
|
263
|
+
if (values[0] < 10) {
|
|
264
|
+
result = "0" + values[0] + ":"
|
|
265
|
+
} else {
|
|
266
|
+
result = values[0] + ":"
|
|
267
|
+
}
|
|
268
|
+
if (values[1] < 10) {
|
|
269
|
+
result += "0" + values[1] + ":"
|
|
270
|
+
} else {
|
|
271
|
+
result += values[1] + ":"
|
|
272
|
+
}
|
|
273
|
+
if (values[2] < 10) {
|
|
274
|
+
result += "0" + values[2]
|
|
275
|
+
} else {
|
|
276
|
+
result += values[2]
|
|
277
|
+
}
|
|
278
|
+
if (values[3] > 0) {
|
|
279
|
+
let nanos = values[3].toString()
|
|
280
|
+
//nine digits
|
|
281
|
+
if (nanos.length < 9) {
|
|
282
|
+
nanos = utils.stringRepeat("0", 9 - nanos.length) + nanos
|
|
283
|
+
}
|
|
284
|
+
let lastPosition
|
|
285
|
+
for (let i = nanos.length - 1; i > 0; i--) {
|
|
286
|
+
if (nanos[i] !== "0") {
|
|
287
|
+
break
|
|
288
|
+
}
|
|
289
|
+
lastPosition = i
|
|
290
|
+
}
|
|
291
|
+
if (lastPosition) {
|
|
292
|
+
nanos = nanos.substring(0, lastPosition)
|
|
293
|
+
}
|
|
294
|
+
result += "." + nanos
|
|
295
|
+
}
|
|
296
|
+
return result
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export default LocalTime
|