UNPKG

21.7 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.isDuration = isDuration;
11exports.isLocalTime = isLocalTime;
12exports.isTime = isTime;
13exports.isDate = isDate;
14exports.isLocalDateTime = isLocalDateTime;
15exports.isDateTime = isDateTime;
16exports.DateTime = exports.LocalDateTime = exports.Date = exports.Time = exports.LocalTime = exports.Duration = void 0;
17
18var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
19
20var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
21
22var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
23
24var util = _interopRequireWildcard(require("./internal/temporal-util"));
25
26var _util = require("./internal/util");
27
28var _error = require("./error");
29
30/**
31 * Copyright (c) 2002-2019 "Neo4j,"
32 * Neo4j Sweden AB [http://neo4j.com]
33 *
34 * This file is part of Neo4j.
35 *
36 * Licensed under the Apache License, Version 2.0 (the "License");
37 * you may not use this file except in compliance with the License.
38 * You may obtain a copy of the License at
39 *
40 * http://www.apache.org/licenses/LICENSE-2.0
41 *
42 * Unless required by applicable law or agreed to in writing, software
43 * distributed under the License is distributed on an "AS IS" BASIS,
44 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45 * See the License for the specific language governing permissions and
46 * limitations under the License.
47 */
48var IDENTIFIER_PROPERTY_ATTRIBUTES = {
49 value: true,
50 enumerable: false,
51 configurable: false,
52 writable: false
53};
54var DURATION_IDENTIFIER_PROPERTY = '__isDuration__';
55var LOCAL_TIME_IDENTIFIER_PROPERTY = '__isLocalTime__';
56var TIME_IDENTIFIER_PROPERTY = '__isTime__';
57var DATE_IDENTIFIER_PROPERTY = '__isDate__';
58var LOCAL_DATE_TIME_IDENTIFIER_PROPERTY = '__isLocalDateTime__';
59var DATE_TIME_IDENTIFIER_PROPERTY = '__isDateTime__';
60/**
61 * Represents an ISO 8601 duration. Contains both date-based values (years, months, days) and time-based values (seconds, nanoseconds).
62 * Created `Duration` objects are frozen with `Object.freeze()` in constructor and thus immutable.
63 */
64
65var Duration =
66/*#__PURE__*/
67function () {
68 /**
69 * @constructor
70 * @param {Integer|number} months - The number of months for the new duration.
71 * @param {Integer|number} days - The number of days for the new duration.
72 * @param {Integer|number} seconds - The number of seconds for the new duration.
73 * @param {Integer|number} nanoseconds - The number of nanoseconds for the new duration.
74 */
75 function Duration(months, days, seconds, nanoseconds) {
76 (0, _classCallCheck2["default"])(this, Duration);
77
78 /**
79 * The number of months.
80 * @type {Integer|number}
81 */
82 this.months = (0, _util.assertNumberOrInteger)(months, 'Months');
83 /**
84 * The number of days.
85 * @type {Integer|number}
86 */
87
88 this.days = (0, _util.assertNumberOrInteger)(days, 'Days');
89 (0, _util.assertNumberOrInteger)(seconds, 'Seconds');
90 (0, _util.assertNumberOrInteger)(nanoseconds, 'Nanoseconds');
91 /**
92 * The number of seconds.
93 * @type {Integer|number}
94 */
95
96 this.seconds = util.normalizeSecondsForDuration(seconds, nanoseconds);
97 /**
98 * The number of nanoseconds.
99 * @type {Integer|number}
100 */
101
102 this.nanoseconds = util.normalizeNanosecondsForDuration(nanoseconds);
103 Object.freeze(this);
104 }
105 /**
106 * @ignore
107 */
108
109
110 (0, _createClass2["default"])(Duration, [{
111 key: "toString",
112 value: function toString() {
113 return util.durationToIsoString(this.months, this.days, this.seconds, this.nanoseconds);
114 }
115 }]);
116 return Duration;
117}();
118
119exports.Duration = Duration;
120Object.defineProperty(Duration.prototype, DURATION_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
121/**
122 * Test if given object is an instance of {@link Duration} class.
123 * @param {Object} obj the object to test.
124 * @return {boolean} `true` if given object is a {@link Duration}, `false` otherwise.
125 */
126
127function isDuration(obj) {
128 return hasIdentifierProperty(obj, DURATION_IDENTIFIER_PROPERTY);
129}
130/**
131 * Represents an instant capturing the time of day, but not the date, nor the timezone.
132 * Created {@link LocalTime} objects are frozen with `Object.freeze()` in constructor and thus immutable.
133 */
134
135
136var LocalTime =
137/*#__PURE__*/
138function () {
139 /**
140 * @constructor
141 * @param {Integer|number} hour - The hour for the new local time.
142 * @param {Integer|number} minute - The minute for the new local time.
143 * @param {Integer|number} second - The second for the new local time.
144 * @param {Integer|number} nanosecond - The nanosecond for the new local time.
145 */
146 function LocalTime(hour, minute, second, nanosecond) {
147 (0, _classCallCheck2["default"])(this, LocalTime);
148
149 /**
150 * The hour.
151 * @type {Integer|number}
152 */
153 this.hour = util.assertValidHour(hour);
154 /**
155 * The minute.
156 * @type {Integer|number}
157 */
158
159 this.minute = util.assertValidMinute(minute);
160 /**
161 * The second.
162 * @type {Integer|number}
163 */
164
165 this.second = util.assertValidSecond(second);
166 /**
167 * The nanosecond.
168 * @type {Integer|number}
169 */
170
171 this.nanosecond = util.assertValidNanosecond(nanosecond);
172 Object.freeze(this);
173 }
174 /**
175 * Create a {@link LocalTime} object from the given standard JavaScript `Date` and optional nanoseconds.
176 * Year, month, day and time zone offset components of the given date are ignored.
177 * @param {global.Date} standardDate - The standard JavaScript date to convert.
178 * @param {Integer|number|undefined} nanosecond - The optional amount of nanoseconds.
179 * @return {LocalTime} New LocalTime.
180 */
181
182
183 (0, _createClass2["default"])(LocalTime, [{
184 key: "toString",
185
186 /**
187 * @ignore
188 */
189 value: function toString() {
190 return util.timeToIsoString(this.hour, this.minute, this.second, this.nanosecond);
191 }
192 }], [{
193 key: "fromStandardDate",
194 value: function fromStandardDate(standardDate, nanosecond) {
195 verifyStandardDateAndNanos(standardDate, nanosecond);
196 return new LocalTime(standardDate.getHours(), standardDate.getMinutes(), standardDate.getSeconds(), util.totalNanoseconds(standardDate, nanosecond));
197 }
198 }]);
199 return LocalTime;
200}();
201
202exports.LocalTime = LocalTime;
203Object.defineProperty(LocalTime.prototype, LOCAL_TIME_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
204/**
205 * Test if given object is an instance of {@link LocalTime} class.
206 * @param {Object} obj the object to test.
207 * @return {boolean} `true` if given object is a {@link LocalTime}, `false` otherwise.
208 */
209
210function isLocalTime(obj) {
211 return hasIdentifierProperty(obj, LOCAL_TIME_IDENTIFIER_PROPERTY);
212}
213/**
214 * Represents an instant capturing the time of day, and the timezone offset in seconds, but not the date.
215 * Created {@link Time} objects are frozen with `Object.freeze()` in constructor and thus immutable.
216 */
217
218
219var Time =
220/*#__PURE__*/
221function () {
222 /**
223 * @constructor
224 * @param {Integer|number} hour - The hour for the new local time.
225 * @param {Integer|number} minute - The minute for the new local time.
226 * @param {Integer|number} second - The second for the new local time.
227 * @param {Integer|number} nanosecond - The nanosecond for the new local time.
228 * @param {Integer|number} timeZoneOffsetSeconds - The time zone offset in seconds. Value represents the difference, in seconds, from UTC to local time.
229 * This is different from standard JavaScript `Date.getTimezoneOffset()` which is the difference, in minutes, from local time to UTC.
230 */
231 function Time(hour, minute, second, nanosecond, timeZoneOffsetSeconds) {
232 (0, _classCallCheck2["default"])(this, Time);
233
234 /**
235 * The hour.
236 * @type {Integer|number}
237 */
238 this.hour = util.assertValidHour(hour);
239 /**
240 * The minute.
241 * @type {Integer|number}
242 */
243
244 this.minute = util.assertValidMinute(minute);
245 /**
246 * The second.
247 * @type {Integer|number}
248 */
249
250 this.second = util.assertValidSecond(second);
251 /**
252 * The nanosecond.
253 * @type {Integer|number}
254 */
255
256 this.nanosecond = util.assertValidNanosecond(nanosecond);
257 /**
258 * The time zone offset in seconds.
259 * @type {Integer|number}
260 */
261
262 this.timeZoneOffsetSeconds = (0, _util.assertNumberOrInteger)(timeZoneOffsetSeconds, 'Time zone offset in seconds');
263 Object.freeze(this);
264 }
265 /**
266 * Create a {@link Time} object from the given standard JavaScript `Date` and optional nanoseconds.
267 * Year, month and day components of the given date are ignored.
268 * @param {global.Date} standardDate - The standard JavaScript date to convert.
269 * @param {Integer|number|undefined} nanosecond - The optional amount of nanoseconds.
270 * @return {Time} New Time.
271 */
272
273
274 (0, _createClass2["default"])(Time, [{
275 key: "toString",
276
277 /**
278 * @ignore
279 */
280 value: function toString() {
281 return util.timeToIsoString(this.hour, this.minute, this.second, this.nanosecond) + util.timeZoneOffsetToIsoString(this.timeZoneOffsetSeconds);
282 }
283 }], [{
284 key: "fromStandardDate",
285 value: function fromStandardDate(standardDate, nanosecond) {
286 verifyStandardDateAndNanos(standardDate, nanosecond);
287 return new Time(standardDate.getHours(), standardDate.getMinutes(), standardDate.getSeconds(), util.totalNanoseconds(standardDate, nanosecond), util.timeZoneOffsetInSeconds(standardDate));
288 }
289 }]);
290 return Time;
291}();
292
293exports.Time = Time;
294Object.defineProperty(Time.prototype, TIME_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
295/**
296 * Test if given object is an instance of {@link Time} class.
297 * @param {Object} obj the object to test.
298 * @return {boolean} `true` if given object is a {@link Time}, `false` otherwise.
299 */
300
301function isTime(obj) {
302 return hasIdentifierProperty(obj, TIME_IDENTIFIER_PROPERTY);
303}
304/**
305 * Represents an instant capturing the date, but not the time, nor the timezone.
306 * Created {@link Date} objects are frozen with `Object.freeze()` in constructor and thus immutable.
307 */
308
309
310var Date =
311/*#__PURE__*/
312function () {
313 /**
314 * @constructor
315 * @param {Integer|number} year - The year for the new local date.
316 * @param {Integer|number} month - The month for the new local date.
317 * @param {Integer|number} day - The day for the new local date.
318 */
319 function Date(year, month, day) {
320 (0, _classCallCheck2["default"])(this, Date);
321
322 /**
323 * The year.
324 * @type {Integer|number}
325 */
326 this.year = util.assertValidYear(year);
327 /**
328 * The month.
329 * @type {Integer|number}
330 */
331
332 this.month = util.assertValidMonth(month);
333 /**
334 * The day.
335 * @type {Integer|number}
336 */
337
338 this.day = util.assertValidDay(day);
339 Object.freeze(this);
340 }
341 /**
342 * Create a {@link Date} object from the given standard JavaScript `Date`.
343 * Hour, minute, second, millisecond and time zone offset components of the given date are ignored.
344 * @param {global.Date} standardDate - The standard JavaScript date to convert.
345 * @return {Date} New Date.
346 */
347
348
349 (0, _createClass2["default"])(Date, [{
350 key: "toString",
351
352 /**
353 * @ignore
354 */
355 value: function toString() {
356 return util.dateToIsoString(this.year, this.month, this.day);
357 }
358 }], [{
359 key: "fromStandardDate",
360 value: function fromStandardDate(standardDate) {
361 verifyStandardDateAndNanos(standardDate, null);
362 return new Date(standardDate.getFullYear(), standardDate.getMonth() + 1, standardDate.getDate());
363 }
364 }]);
365 return Date;
366}();
367
368exports.Date = Date;
369Object.defineProperty(Date.prototype, DATE_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
370/**
371 * Test if given object is an instance of {@link Date} class.
372 * @param {Object} obj - The object to test.
373 * @return {boolean} `true` if given object is a {@link Date}, `false` otherwise.
374 */
375
376function isDate(obj) {
377 return hasIdentifierProperty(obj, DATE_IDENTIFIER_PROPERTY);
378}
379/**
380 * Represents an instant capturing the date and the time, but not the timezone.
381 * Created {@link LocalDateTime} objects are frozen with `Object.freeze()` in constructor and thus immutable.
382 */
383
384
385var LocalDateTime =
386/*#__PURE__*/
387function () {
388 /**
389 * @constructor
390 * @param {Integer|number} year - The year for the new local date.
391 * @param {Integer|number} month - The month for the new local date.
392 * @param {Integer|number} day - The day for the new local date.
393 * @param {Integer|number} hour - The hour for the new local time.
394 * @param {Integer|number} minute - The minute for the new local time.
395 * @param {Integer|number} second - The second for the new local time.
396 * @param {Integer|number} nanosecond - The nanosecond for the new local time.
397 */
398 function LocalDateTime(year, month, day, hour, minute, second, nanosecond) {
399 (0, _classCallCheck2["default"])(this, LocalDateTime);
400
401 /**
402 * The year.
403 * @type {Integer|number}
404 */
405 this.year = util.assertValidYear(year);
406 /**
407 * The month.
408 * @type {Integer|number}
409 */
410
411 this.month = util.assertValidMonth(month);
412 /**
413 * The day.
414 * @type {Integer|number}
415 */
416
417 this.day = util.assertValidDay(day);
418 /**
419 * The hour.
420 * @type {Integer|number}
421 */
422
423 this.hour = util.assertValidHour(hour);
424 /**
425 * The minute.
426 * @type {Integer|number}
427 */
428
429 this.minute = util.assertValidMinute(minute);
430 /**
431 * The second.
432 * @type {Integer|number}
433 */
434
435 this.second = util.assertValidSecond(second);
436 /**
437 * The nanosecond.
438 * @type {Integer|number}
439 */
440
441 this.nanosecond = util.assertValidNanosecond(nanosecond);
442 Object.freeze(this);
443 }
444 /**
445 * Create a {@link LocalDateTime} object from the given standard JavaScript `Date` and optional nanoseconds.
446 * Time zone offset component of the given date is ignored.
447 * @param {global.Date} standardDate - The standard JavaScript date to convert.
448 * @param {Integer|number|undefined} nanosecond - The optional amount of nanoseconds.
449 * @return {LocalDateTime} New LocalDateTime.
450 */
451
452
453 (0, _createClass2["default"])(LocalDateTime, [{
454 key: "toString",
455
456 /**
457 * @ignore
458 */
459 value: function toString() {
460 return localDateTimeToString(this.year, this.month, this.day, this.hour, this.minute, this.second, this.nanosecond);
461 }
462 }], [{
463 key: "fromStandardDate",
464 value: function fromStandardDate(standardDate, nanosecond) {
465 verifyStandardDateAndNanos(standardDate, nanosecond);
466 return new LocalDateTime(standardDate.getFullYear(), standardDate.getMonth() + 1, standardDate.getDate(), standardDate.getHours(), standardDate.getMinutes(), standardDate.getSeconds(), util.totalNanoseconds(standardDate, nanosecond));
467 }
468 }]);
469 return LocalDateTime;
470}();
471
472exports.LocalDateTime = LocalDateTime;
473Object.defineProperty(LocalDateTime.prototype, LOCAL_DATE_TIME_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
474/**
475 * Test if given object is an instance of {@link LocalDateTime} class.
476 * @param {Object} obj - The object to test.
477 * @return {boolean} `true` if given object is a {@link LocalDateTime}, `false` otherwise.
478 */
479
480function isLocalDateTime(obj) {
481 return hasIdentifierProperty(obj, LOCAL_DATE_TIME_IDENTIFIER_PROPERTY);
482}
483/**
484 * Represents an instant capturing the date, the time and the timezone identifier.
485 * Created {@ DateTime} objects are frozen with `Object.freeze()` in constructor and thus immutable.
486 */
487
488
489var DateTime =
490/*#__PURE__*/
491function () {
492 /**
493 * @constructor
494 * @param {Integer|number} year - The year for the new date-time.
495 * @param {Integer|number} month - The month for the new date-time.
496 * @param {Integer|number} day - The day for the new date-time.
497 * @param {Integer|number} hour - The hour for the new date-time.
498 * @param {Integer|number} minute - The minute for the new date-time.
499 * @param {Integer|number} second - The second for the new date-time.
500 * @param {Integer|number} nanosecond - The nanosecond for the new date-time.
501 * @param {Integer|number} timeZoneOffsetSeconds - The time zone offset in seconds. Either this argument or `timeZoneId` should be defined.
502 * Value represents the difference, in seconds, from UTC to local time.
503 * This is different from standard JavaScript `Date.getTimezoneOffset()` which is the difference, in minutes, from local time to UTC.
504 * @param {string|null} timeZoneId - The time zone id for the new date-time. Either this argument or `timeZoneOffsetSeconds` should be defined.
505 */
506 function DateTime(year, month, day, hour, minute, second, nanosecond, timeZoneOffsetSeconds, timeZoneId) {
507 (0, _classCallCheck2["default"])(this, DateTime);
508
509 /**
510 * The year.
511 * @type {Integer|number}
512 */
513 this.year = util.assertValidYear(year);
514 /**
515 * The month.
516 * @type {Integer|number}
517 */
518
519 this.month = util.assertValidMonth(month);
520 /**
521 * The day.
522 * @type {Integer|number}
523 */
524
525 this.day = util.assertValidDay(day);
526 /**
527 * The hour.
528 * @type {Integer|number}
529 */
530
531 this.hour = util.assertValidHour(hour);
532 /**
533 * The minute.
534 * @type {Integer|number}
535 */
536
537 this.minute = util.assertValidMinute(minute);
538 /**
539 * The second.
540 * @type {Integer|number}
541 */
542
543 this.second = util.assertValidSecond(second);
544 /**
545 * The nanosecond.
546 * @type {Integer|number}
547 */
548
549 this.nanosecond = util.assertValidNanosecond(nanosecond);
550
551 var _verifyTimeZoneArgume = verifyTimeZoneArguments(timeZoneOffsetSeconds, timeZoneId),
552 _verifyTimeZoneArgume2 = (0, _slicedToArray2["default"])(_verifyTimeZoneArgume, 2),
553 offset = _verifyTimeZoneArgume2[0],
554 id = _verifyTimeZoneArgume2[1];
555 /**
556 * The time zone offset in seconds.
557 *
558 * *Either this or {@link timeZoneId} is defined.*
559 *
560 * @type {Integer|number}
561 */
562
563
564 this.timeZoneOffsetSeconds = offset;
565 /**
566 * The time zone id.
567 *
568 * *Either this or {@link timeZoneOffsetSeconds} is defined.*
569 *
570 * @type {string}
571 */
572
573 this.timeZoneId = id;
574 Object.freeze(this);
575 }
576 /**
577 * Create a {@link DateTime} object from the given standard JavaScript `Date` and optional nanoseconds.
578 * @param {global.Date} standardDate - The standard JavaScript date to convert.
579 * @param {Integer|number|undefined} nanosecond - The optional amount of nanoseconds.
580 * @return {DateTime} New DateTime.
581 */
582
583
584 (0, _createClass2["default"])(DateTime, [{
585 key: "toString",
586
587 /**
588 * @ignore
589 */
590 value: function toString() {
591 var localDateTimeStr = localDateTimeToString(this.year, this.month, this.day, this.hour, this.minute, this.second, this.nanosecond);
592 var timeZoneStr = this.timeZoneId ? "[".concat(this.timeZoneId, "]") : util.timeZoneOffsetToIsoString(this.timeZoneOffsetSeconds);
593 return localDateTimeStr + timeZoneStr;
594 }
595 }], [{
596 key: "fromStandardDate",
597 value: function fromStandardDate(standardDate, nanosecond) {
598 verifyStandardDateAndNanos(standardDate, nanosecond);
599 return new DateTime(standardDate.getFullYear(), standardDate.getMonth() + 1, standardDate.getDate(), standardDate.getHours(), standardDate.getMinutes(), standardDate.getSeconds(), util.totalNanoseconds(standardDate, nanosecond), util.timeZoneOffsetInSeconds(standardDate), null
600 /* no time zone id */
601 );
602 }
603 }]);
604 return DateTime;
605}();
606
607exports.DateTime = DateTime;
608Object.defineProperty(DateTime.prototype, DATE_TIME_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
609/**
610 * Test if given object is an instance of {@link DateTime} class.
611 * @param {Object} obj - The object to test.
612 * @return {boolean} `true` if given object is a {@link DateTime}, `false` otherwise.
613 */
614
615function isDateTime(obj) {
616 return hasIdentifierProperty(obj, DATE_TIME_IDENTIFIER_PROPERTY);
617}
618
619function hasIdentifierProperty(obj, property) {
620 return (obj && obj[property]) === true;
621}
622
623function localDateTimeToString(year, month, day, hour, minute, second, nanosecond) {
624 return util.dateToIsoString(year, month, day) + 'T' + util.timeToIsoString(hour, minute, second, nanosecond);
625}
626
627function verifyTimeZoneArguments(timeZoneOffsetSeconds, timeZoneId) {
628 var offsetDefined = timeZoneOffsetSeconds || timeZoneOffsetSeconds === 0;
629 var idDefined = timeZoneId && timeZoneId !== '';
630
631 if (offsetDefined && !idDefined) {
632 (0, _util.assertNumberOrInteger)(timeZoneOffsetSeconds, 'Time zone offset in seconds');
633 return [timeZoneOffsetSeconds, null];
634 } else if (!offsetDefined && idDefined) {
635 (0, _util.assertString)(timeZoneId, 'Time zone ID');
636 return [null, timeZoneId];
637 } else if (offsetDefined && idDefined) {
638 throw (0, _error.newError)("Unable to create DateTime with both time zone offset and id. Please specify either of them. Given offset: ".concat(timeZoneOffsetSeconds, " and id: ").concat(timeZoneId));
639 } else {
640 throw (0, _error.newError)("Unable to create DateTime without either time zone offset or id. Please specify either of them. Given offset: ".concat(timeZoneOffsetSeconds, " and id: ").concat(timeZoneId));
641 }
642}
643
644function verifyStandardDateAndNanos(standardDate, nanosecond) {
645 (0, _util.assertValidDate)(standardDate, 'Standard date');
646
647 if (nanosecond !== null && nanosecond !== undefined) {
648 (0, _util.assertNumberOrInteger)(nanosecond, 'Nanosecond');
649 }
650}
\No newline at end of file