UNPKG

14.2 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.DateTime = exports.LocalDateTime = exports.Date = exports.Time = exports.LocalTime = exports.Duration = undefined;
7
8var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray');
9
10var _slicedToArray3 = _interopRequireDefault(_slicedToArray2);
11
12var _defineProperty = require('babel-runtime/core-js/object/define-property');
13
14var _defineProperty2 = _interopRequireDefault(_defineProperty);
15
16var _freeze = require('babel-runtime/core-js/object/freeze');
17
18var _freeze2 = _interopRequireDefault(_freeze);
19
20var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
21
22var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
23
24var _createClass2 = require('babel-runtime/helpers/createClass');
25
26var _createClass3 = _interopRequireDefault(_createClass2);
27
28exports.isDuration = isDuration;
29exports.isLocalTime = isLocalTime;
30exports.isTime = isTime;
31exports.isDate = isDate;
32exports.isLocalDateTime = isLocalDateTime;
33exports.isDateTime = isDateTime;
34
35var _temporalUtil = require('./internal/temporal-util');
36
37var _error = require('./error');
38
39function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
40
41/**
42 * Copyright (c) 2002-2018 Neo4j Sweden AB [http://neo4j.com]
43 *
44 * This file is part of Neo4j.
45 *
46 * Licensed under the Apache License, Version 2.0 (the "License");
47 * you may not use this file except in compliance with the License.
48 * You may obtain a copy of the License at
49 *
50 * http://www.apache.org/licenses/LICENSE-2.0
51 *
52 * Unless required by applicable law or agreed to in writing, software
53 * distributed under the License is distributed on an "AS IS" BASIS,
54 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
55 * See the License for the specific language governing permissions and
56 * limitations under the License.
57 */
58
59var IDENTIFIER_PROPERTY_ATTRIBUTES = {
60 value: true,
61 enumerable: false,
62 configurable: false
63};
64
65var DURATION_IDENTIFIER_PROPERTY = '__isDuration__';
66var LOCAL_TIME_IDENTIFIER_PROPERTY = '__isLocalTime__';
67var TIME_IDENTIFIER_PROPERTY = '__isTime__';
68var DATE_IDENTIFIER_PROPERTY = '__isDate__';
69var LOCAL_DATE_TIME_IDENTIFIER_PROPERTY = '__isLocalDateTime__';
70var DATE_TIME_IDENTIFIER_PROPERTY = '__isDateTime__';
71
72/**
73 * Represents an ISO 8601 duration. Contains both date-based values (years, months, days) and time-based values (seconds, nanoseconds).
74 * Created <code>Duration</code> objects are frozen with {@link Object#freeze()} in constructor and thus immutable.
75 */
76
77var Duration = exports.Duration = function () {
78
79 /**
80 * @constructor
81 * @param {Integer|number} months the number of months for the new duration.
82 * @param {Integer|number} days the number of days for the new duration.
83 * @param {Integer|number} seconds the number of seconds for the new duration.
84 * @param {Integer|number} nanoseconds the number of nanoseconds for the new duration.
85 */
86 function Duration(months, days, seconds, nanoseconds) {
87 (0, _classCallCheck3.default)(this, Duration);
88
89 this.months = months;
90 this.days = days;
91 this.seconds = seconds;
92 this.nanoseconds = nanoseconds;
93 (0, _freeze2.default)(this);
94 }
95
96 (0, _createClass3.default)(Duration, [{
97 key: 'toString',
98 value: function toString() {
99 return (0, _temporalUtil.durationToIsoString)(this.months, this.days, this.seconds, this.nanoseconds);
100 }
101 }]);
102 return Duration;
103}();
104
105(0, _defineProperty2.default)(Duration.prototype, DURATION_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
106
107/**
108 * Test if given object is an instance of {@link Duration} class.
109 * @param {object} obj the object to test.
110 * @return {boolean} <code>true</code> if given object is a {@link Duration}, <code>false</code> otherwise.
111 */
112function isDuration(obj) {
113 return hasIdentifierProperty(obj, DURATION_IDENTIFIER_PROPERTY);
114}
115
116/**
117 * Represents an instant capturing the time of day, but not the date, nor the timezone.
118 * Created <code>LocalTime</code> objects are frozen with {@link Object#freeze()} in constructor and thus immutable.
119 */
120
121var LocalTime = exports.LocalTime = function () {
122
123 /**
124 * @constructor
125 * @param {Integer|number} hour the hour for the new local time.
126 * @param {Integer|number} minute the minute for the new local time.
127 * @param {Integer|number} second the second for the new local time.
128 * @param {Integer|number} nanosecond the nanosecond for the new local time.
129 */
130 function LocalTime(hour, minute, second, nanosecond) {
131 (0, _classCallCheck3.default)(this, LocalTime);
132
133 this.hour = hour;
134 this.minute = minute;
135 this.second = second;
136 this.nanosecond = nanosecond;
137 (0, _freeze2.default)(this);
138 }
139
140 (0, _createClass3.default)(LocalTime, [{
141 key: 'toString',
142 value: function toString() {
143 return (0, _temporalUtil.timeToIsoString)(this.hour, this.minute, this.second, this.nanosecond);
144 }
145 }]);
146 return LocalTime;
147}();
148
149(0, _defineProperty2.default)(LocalTime.prototype, LOCAL_TIME_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
150
151/**
152 * Test if given object is an instance of {@link LocalTime} class.
153 * @param {object} obj the object to test.
154 * @return {boolean} <code>true</code> if given object is a {@link LocalTime}, <code>false</code> otherwise.
155 */
156function isLocalTime(obj) {
157 return hasIdentifierProperty(obj, LOCAL_TIME_IDENTIFIER_PROPERTY);
158}
159
160/**
161 * Represents an instant capturing the time of day, and the timezone offset in seconds, but not the date.
162 * Created <code>Time</code> objects are frozen with {@link Object#freeze()} in constructor and thus immutable.
163 */
164
165var Time = exports.Time = function () {
166
167 /**
168 * @constructor
169 * @param {Integer|number} hour the hour for the new local time.
170 * @param {Integer|number} minute the minute for the new local time.
171 * @param {Integer|number} second the second for the new local time.
172 * @param {Integer|number} nanosecond the nanosecond for the new local time.
173 * @param {Integer|number} timeZoneOffsetSeconds the time zone offset in seconds.
174 */
175 function Time(hour, minute, second, nanosecond, timeZoneOffsetSeconds) {
176 (0, _classCallCheck3.default)(this, Time);
177
178 this.hour = hour;
179 this.minute = minute;
180 this.second = second;
181 this.nanosecond = nanosecond;
182 this.timeZoneOffsetSeconds = timeZoneOffsetSeconds;
183 (0, _freeze2.default)(this);
184 }
185
186 (0, _createClass3.default)(Time, [{
187 key: 'toString',
188 value: function toString() {
189 return (0, _temporalUtil.timeToIsoString)(this.hour, this.minute, this.second, this.nanosecond) + (0, _temporalUtil.timeZoneOffsetToIsoString)(this.timeZoneOffsetSeconds);
190 }
191 }]);
192 return Time;
193}();
194
195(0, _defineProperty2.default)(Time.prototype, TIME_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
196
197/**
198 * Test if given object is an instance of {@link Time} class.
199 * @param {object} obj the object to test.
200 * @return {boolean} <code>true</code> if given object is a {@link Time}, <code>false</code> otherwise.
201 */
202function isTime(obj) {
203 return hasIdentifierProperty(obj, TIME_IDENTIFIER_PROPERTY);
204}
205
206/**
207 * Represents an instant capturing the date, but not the time, nor the timezone.
208 * Created <code>Date</code> objects are frozen with {@link Object#freeze()} in constructor and thus immutable.
209 */
210
211var Date = exports.Date = function () {
212
213 /**
214 * @constructor
215 * @param {Integer|number} year the year for the new local date.
216 * @param {Integer|number} month the month for the new local date.
217 * @param {Integer|number} day the day for the new local date.
218 */
219 function Date(year, month, day) {
220 (0, _classCallCheck3.default)(this, Date);
221
222 this.year = year;
223 this.month = month;
224 this.day = day;
225 (0, _freeze2.default)(this);
226 }
227
228 (0, _createClass3.default)(Date, [{
229 key: 'toString',
230 value: function toString() {
231 return (0, _temporalUtil.dateToIsoString)(this.year, this.month, this.day);
232 }
233 }]);
234 return Date;
235}();
236
237(0, _defineProperty2.default)(Date.prototype, DATE_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
238
239/**
240 * Test if given object is an instance of {@link Date} class.
241 * @param {object} obj the object to test.
242 * @return {boolean} <code>true</code> if given object is a {@link Date}, <code>false</code> otherwise.
243 */
244function isDate(obj) {
245 return hasIdentifierProperty(obj, DATE_IDENTIFIER_PROPERTY);
246}
247
248/**
249 * Represents an instant capturing the date and the time, but not the timezone.
250 * Created <code>LocalDateTime</code> objects are frozen with {@link Object#freeze()} in constructor and thus immutable.
251 */
252
253var LocalDateTime = exports.LocalDateTime = function () {
254
255 /**
256 * @constructor
257 * @param {Integer|number} year the year for the new local date.
258 * @param {Integer|number} month the month for the new local date.
259 * @param {Integer|number} day the day for the new local date.
260 * @param {Integer|number} hour the hour for the new local time.
261 * @param {Integer|number} minute the minute for the new local time.
262 * @param {Integer|number} second the second for the new local time.
263 * @param {Integer|number} nanosecond the nanosecond for the new local time.
264 */
265 function LocalDateTime(year, month, day, hour, minute, second, nanosecond) {
266 (0, _classCallCheck3.default)(this, LocalDateTime);
267
268 this.year = year;
269 this.month = month;
270 this.day = day;
271 this.hour = hour;
272 this.minute = minute;
273 this.second = second;
274 this.nanosecond = nanosecond;
275 (0, _freeze2.default)(this);
276 }
277
278 (0, _createClass3.default)(LocalDateTime, [{
279 key: 'toString',
280 value: function toString() {
281 return localDateTimeToString(this.year, this.month, this.day, this.hour, this.minute, this.second, this.nanosecond);
282 }
283 }]);
284 return LocalDateTime;
285}();
286
287(0, _defineProperty2.default)(LocalDateTime.prototype, LOCAL_DATE_TIME_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
288
289/**
290 * Test if given object is an instance of {@link LocalDateTime} class.
291 * @param {object} obj the object to test.
292 * @return {boolean} <code>true</code> if given object is a {@link LocalDateTime}, <code>false</code> otherwise.
293 */
294function isLocalDateTime(obj) {
295 return hasIdentifierProperty(obj, LOCAL_DATE_TIME_IDENTIFIER_PROPERTY);
296}
297
298/**
299 * Represents an instant capturing the date, the time and the timezone identifier.
300 * Created <code>DateTime</code> objects are frozen with {@link Object#freeze()} in constructor and thus immutable.
301 */
302
303var DateTime = exports.DateTime = function () {
304
305 /**
306 * @constructor
307 * @param {Integer|number} year the year for the new date-time.
308 * @param {Integer|number} month the month for the new date-time.
309 * @param {Integer|number} day the day for the new date-time.
310 * @param {Integer|number} hour the hour for the new date-time.
311 * @param {Integer|number} minute the minute for the new date-time.
312 * @param {Integer|number} second the second for the new date-time.
313 * @param {Integer|number} nanosecond the nanosecond for the new date-time.
314 * @param {Integer|number|null} timeZoneOffsetSeconds the total time zone offset in seconds for the new date-time. Either this argument or <code>timeZoneId</code> should be defined.
315 * @param {string|null} timeZoneId the time zone id for the new date-time. Either this argument or <code>timeZoneOffsetSeconds</code> should be defined.
316 */
317 function DateTime(year, month, day, hour, minute, second, nanosecond, timeZoneOffsetSeconds, timeZoneId) {
318 (0, _classCallCheck3.default)(this, DateTime);
319
320 this.year = year;
321 this.month = month;
322 this.day = day;
323 this.hour = hour;
324 this.minute = minute;
325 this.second = second;
326 this.nanosecond = nanosecond;
327
328 var _verifyTimeZoneArgume = verifyTimeZoneArguments(timeZoneOffsetSeconds, timeZoneId),
329 _verifyTimeZoneArgume2 = (0, _slicedToArray3.default)(_verifyTimeZoneArgume, 2),
330 offset = _verifyTimeZoneArgume2[0],
331 id = _verifyTimeZoneArgume2[1];
332
333 this.timeZoneOffsetSeconds = offset;
334 this.timeZoneId = id;
335
336 (0, _freeze2.default)(this);
337 }
338
339 (0, _createClass3.default)(DateTime, [{
340 key: 'toString',
341 value: function toString() {
342 var localDateTimeStr = localDateTimeToString(this.year, this.month, this.day, this.hour, this.minute, this.second, this.nanosecond);
343 var timeZoneStr = this.timeZoneId ? '[' + this.timeZoneId + ']' : (0, _temporalUtil.timeZoneOffsetToIsoString)(this.timeZoneOffsetSeconds);
344 return localDateTimeStr + timeZoneStr;
345 }
346 }]);
347 return DateTime;
348}();
349
350(0, _defineProperty2.default)(DateTime.prototype, DATE_TIME_IDENTIFIER_PROPERTY, IDENTIFIER_PROPERTY_ATTRIBUTES);
351
352/**
353 * Test if given object is an instance of {@link DateTime} class.
354 * @param {object} obj the object to test.
355 * @return {boolean} <code>true</code> if given object is a {@link DateTime}, <code>false</code> otherwise.
356 */
357function isDateTime(obj) {
358 return hasIdentifierProperty(obj, DATE_TIME_IDENTIFIER_PROPERTY);
359}
360
361function hasIdentifierProperty(obj, property) {
362 return (obj && obj[property]) === true;
363}
364
365function localDateTimeToString(year, month, day, hour, minute, second, nanosecond) {
366 return (0, _temporalUtil.dateToIsoString)(year, month, day) + 'T' + (0, _temporalUtil.timeToIsoString)(hour, minute, second, nanosecond);
367}
368
369function verifyTimeZoneArguments(timeZoneOffsetSeconds, timeZoneId) {
370 var offsetDefined = timeZoneOffsetSeconds || timeZoneOffsetSeconds === 0;
371 var idDefined = timeZoneId && timeZoneId !== '';
372
373 if (offsetDefined && !idDefined) {
374 return [timeZoneOffsetSeconds, null];
375 } else if (!offsetDefined && idDefined) {
376 return [null, timeZoneId];
377 } else if (offsetDefined && idDefined) {
378 throw (0, _error.newError)('Unable to create DateTime with both time zone offset and id. Please specify either of them. Given offset: ' + timeZoneOffsetSeconds + ' and id: ' + timeZoneId);
379 } else {
380 throw (0, _error.newError)('Unable to create DateTime without either time zone offset or id. Please specify either of them. Given offset: ' + timeZoneOffsetSeconds + ' and id: ' + timeZoneId);
381 }
382}
\No newline at end of file