1 | "use strict";
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
4 |
|
5 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", {
|
8 | value: true
|
9 | });
|
10 | exports.isDuration = isDuration;
|
11 | exports.isLocalTime = isLocalTime;
|
12 | exports.isTime = isTime;
|
13 | exports.isDate = isDate;
|
14 | exports.isLocalDateTime = isLocalDateTime;
|
15 | exports.isDateTime = isDateTime;
|
16 | exports.DateTime = exports.LocalDateTime = exports.Date = exports.Time = exports.LocalTime = exports.Duration = void 0;
|
17 |
|
18 | var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
19 |
|
20 | var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
21 |
|
22 | var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
23 |
|
24 | var util = _interopRequireWildcard(require("./internal/temporal-util"));
|
25 |
|
26 | var _util = require("./internal/util");
|
27 |
|
28 | var _error = require("./error");
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | var IDENTIFIER_PROPERTY_ATTRIBUTES = {
|
49 | value: true,
|
50 | enumerable: false,
|
51 | configurable: false,
|
52 | writable: false
|
53 | };
|
54 | var DURATION_IDENTIFIER_PROPERTY = '__isDuration__';
|
55 | var LOCAL_TIME_IDENTIFIER_PROPERTY = '__isLocalTime__';
|
56 | var TIME_IDENTIFIER_PROPERTY = '__isTime__';
|
57 | var DATE_IDENTIFIER_PROPERTY = '__isDate__';
|
58 | var LOCAL_DATE_TIME_IDENTIFIER_PROPERTY = '__isLocalDateTime__';
|
59 | var DATE_TIME_IDENTIFIER_PROPERTY = '__isDateTime__';
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | var Duration =
|
66 |
|
67 | function () {
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | function Duration(months, days, seconds, nanoseconds) {
|
76 | (0, _classCallCheck2["default"])(this, Duration);
|
77 |
|
78 | |
79 |
|
80 |
|
81 |
|
82 | this.months = (0, _util.assertNumberOrInteger)(months, 'Months');
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 | this.days = (0, _util.assertNumberOrInteger)(days, 'Days');
|
89 | (0, _util.assertNumberOrInteger)(seconds, 'Seconds');
|
90 | (0, _util.assertNumberOrInteger)(nanoseconds, 'Nanoseconds');
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 | this.seconds = util.normalizeSecondsForDuration(seconds, nanoseconds);
|
97 | |
98 |
|
99 |
|
100 |
|
101 |
|
102 | this.nanoseconds = util.normalizeNanosecondsForDuration(nanoseconds);
|
103 | Object.freeze(this);
|
104 | }
|
105 | |
106 |
|
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 |
|
119 | exports.Duration = Duration;
|
120 | Object.defineProperty(Duration.prototype, DURATION_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function isDuration(obj) {
|
128 | return hasIdentifierProperty(obj, DURATION_IDENTIFIER_PROPERTY);
|
129 | }
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | var LocalTime =
|
137 |
|
138 | function () {
|
139 | |
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | function LocalTime(hour, minute, second, nanosecond) {
|
147 | (0, _classCallCheck2["default"])(this, LocalTime);
|
148 |
|
149 | |
150 |
|
151 |
|
152 |
|
153 | this.hour = util.assertValidHour(hour);
|
154 | |
155 |
|
156 |
|
157 |
|
158 |
|
159 | this.minute = util.assertValidMinute(minute);
|
160 | |
161 |
|
162 |
|
163 |
|
164 |
|
165 | this.second = util.assertValidSecond(second);
|
166 | |
167 |
|
168 |
|
169 |
|
170 |
|
171 | this.nanosecond = util.assertValidNanosecond(nanosecond);
|
172 | Object.freeze(this);
|
173 | }
|
174 | |
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | (0, _createClass2["default"])(LocalTime, [{
|
184 | key: "toString",
|
185 |
|
186 | |
187 |
|
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 |
|
202 | exports.LocalTime = LocalTime;
|
203 | Object.defineProperty(LocalTime.prototype, LOCAL_TIME_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | function isLocalTime(obj) {
|
211 | return hasIdentifierProperty(obj, LOCAL_TIME_IDENTIFIER_PROPERTY);
|
212 | }
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | var Time =
|
220 |
|
221 | function () {
|
222 | |
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | function Time(hour, minute, second, nanosecond, timeZoneOffsetSeconds) {
|
232 | (0, _classCallCheck2["default"])(this, Time);
|
233 |
|
234 | |
235 |
|
236 |
|
237 |
|
238 | this.hour = util.assertValidHour(hour);
|
239 | |
240 |
|
241 |
|
242 |
|
243 |
|
244 | this.minute = util.assertValidMinute(minute);
|
245 | |
246 |
|
247 |
|
248 |
|
249 |
|
250 | this.second = util.assertValidSecond(second);
|
251 | |
252 |
|
253 |
|
254 |
|
255 |
|
256 | this.nanosecond = util.assertValidNanosecond(nanosecond);
|
257 | |
258 |
|
259 |
|
260 |
|
261 |
|
262 | this.timeZoneOffsetSeconds = (0, _util.assertNumberOrInteger)(timeZoneOffsetSeconds, 'Time zone offset in seconds');
|
263 | Object.freeze(this);
|
264 | }
|
265 | |
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 | (0, _createClass2["default"])(Time, [{
|
275 | key: "toString",
|
276 |
|
277 | |
278 |
|
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 |
|
293 | exports.Time = Time;
|
294 | Object.defineProperty(Time.prototype, TIME_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 | function isTime(obj) {
|
302 | return hasIdentifierProperty(obj, TIME_IDENTIFIER_PROPERTY);
|
303 | }
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | var Date =
|
311 |
|
312 | function () {
|
313 | |
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 | function Date(year, month, day) {
|
320 | (0, _classCallCheck2["default"])(this, Date);
|
321 |
|
322 | |
323 |
|
324 |
|
325 |
|
326 | this.year = util.assertValidYear(year);
|
327 | |
328 |
|
329 |
|
330 |
|
331 |
|
332 | this.month = util.assertValidMonth(month);
|
333 | |
334 |
|
335 |
|
336 |
|
337 |
|
338 | this.day = util.assertValidDay(day);
|
339 | Object.freeze(this);
|
340 | }
|
341 | |
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 | (0, _createClass2["default"])(Date, [{
|
350 | key: "toString",
|
351 |
|
352 | |
353 |
|
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 |
|
368 | exports.Date = Date;
|
369 | Object.defineProperty(Date.prototype, DATE_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 | function isDate(obj) {
|
377 | return hasIdentifierProperty(obj, DATE_IDENTIFIER_PROPERTY);
|
378 | }
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 | var LocalDateTime =
|
386 |
|
387 | function () {
|
388 | |
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 | function LocalDateTime(year, month, day, hour, minute, second, nanosecond) {
|
399 | (0, _classCallCheck2["default"])(this, LocalDateTime);
|
400 |
|
401 | |
402 |
|
403 |
|
404 |
|
405 | this.year = util.assertValidYear(year);
|
406 | |
407 |
|
408 |
|
409 |
|
410 |
|
411 | this.month = util.assertValidMonth(month);
|
412 | |
413 |
|
414 |
|
415 |
|
416 |
|
417 | this.day = util.assertValidDay(day);
|
418 | |
419 |
|
420 |
|
421 |
|
422 |
|
423 | this.hour = util.assertValidHour(hour);
|
424 | |
425 |
|
426 |
|
427 |
|
428 |
|
429 | this.minute = util.assertValidMinute(minute);
|
430 | |
431 |
|
432 |
|
433 |
|
434 |
|
435 | this.second = util.assertValidSecond(second);
|
436 | |
437 |
|
438 |
|
439 |
|
440 |
|
441 | this.nanosecond = util.assertValidNanosecond(nanosecond);
|
442 | Object.freeze(this);
|
443 | }
|
444 | |
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 | (0, _createClass2["default"])(LocalDateTime, [{
|
454 | key: "toString",
|
455 |
|
456 | |
457 |
|
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 |
|
472 | exports.LocalDateTime = LocalDateTime;
|
473 | Object.defineProperty(LocalDateTime.prototype, LOCAL_DATE_TIME_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 | function isLocalDateTime(obj) {
|
481 | return hasIdentifierProperty(obj, LOCAL_DATE_TIME_IDENTIFIER_PROPERTY);
|
482 | }
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 | var DateTime =
|
490 |
|
491 | function () {
|
492 | |
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 | function DateTime(year, month, day, hour, minute, second, nanosecond, timeZoneOffsetSeconds, timeZoneId) {
|
507 | (0, _classCallCheck2["default"])(this, DateTime);
|
508 |
|
509 | |
510 |
|
511 |
|
512 |
|
513 | this.year = util.assertValidYear(year);
|
514 | |
515 |
|
516 |
|
517 |
|
518 |
|
519 | this.month = util.assertValidMonth(month);
|
520 | |
521 |
|
522 |
|
523 |
|
524 |
|
525 | this.day = util.assertValidDay(day);
|
526 | |
527 |
|
528 |
|
529 |
|
530 |
|
531 | this.hour = util.assertValidHour(hour);
|
532 | |
533 |
|
534 |
|
535 |
|
536 |
|
537 | this.minute = util.assertValidMinute(minute);
|
538 | |
539 |
|
540 |
|
541 |
|
542 |
|
543 | this.second = util.assertValidSecond(second);
|
544 | |
545 |
|
546 |
|
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 |
|
557 |
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 |
|
564 | this.timeZoneOffsetSeconds = offset;
|
565 | |
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 |
|
573 | this.timeZoneId = id;
|
574 | Object.freeze(this);
|
575 | }
|
576 | |
577 |
|
578 |
|
579 |
|
580 |
|
581 |
|
582 |
|
583 |
|
584 | (0, _createClass2["default"])(DateTime, [{
|
585 | key: "toString",
|
586 |
|
587 | |
588 |
|
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 |
|
601 | );
|
602 | }
|
603 | }]);
|
604 | return DateTime;
|
605 | }();
|
606 |
|
607 | exports.DateTime = DateTime;
|
608 | Object.defineProperty(DateTime.prototype, DATE_TIME_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
|
609 |
|
610 |
|
611 |
|
612 |
|
613 |
|
614 |
|
615 | function isDateTime(obj) {
|
616 | return hasIdentifierProperty(obj, DATE_TIME_IDENTIFIER_PROPERTY);
|
617 | }
|
618 |
|
619 | function hasIdentifierProperty(obj, property) {
|
620 | return (obj && obj[property]) === true;
|
621 | }
|
622 |
|
623 | function localDateTimeToString(year, month, day, hour, minute, second, nanosecond) {
|
624 | return util.dateToIsoString(year, month, day) + 'T' + util.timeToIsoString(hour, minute, second, nanosecond);
|
625 | }
|
626 |
|
627 | function 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 |
|
644 | function 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 |