@rschedule/joda-date-adapter
Version:
An rSchedule DateAdapter for "js-joda" ZonedDateTime objects.
208 lines (197 loc) • 6.86 kB
JavaScript
(function(global, factory) {
typeof exports === 'object' && typeof module !== 'undefined'
? factory(exports, require('@js-joda/core'), require('@rschedule/core'))
: typeof define === 'function' && define.amd
? define(['exports', '@js-joda/core', '@rschedule/core'], factory)
: ((global = global || self),
factory((global.rScheduleJodaDateAdapter = {}), global.JSJoda, global.rSchedule));
})(this, function(exports, core, core$1) {
'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function(d, b) {
extendStatics =
Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array &&
function(d, b) {
d.__proto__ = b;
}) ||
function(d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
};
return extendStatics(d, b);
};
function __extends(d, b) {
extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __());
}
function __read(o, n) {
var m = typeof Symbol === 'function' && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o),
r,
ar = [],
e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
} catch (error) {
e = { error: error };
} finally {
try {
if (r && !r.done && (m = i['return'])) m.call(i);
} finally {
if (e) throw e.error;
}
}
return ar;
}
function __spread() {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
}
/**
* The `JodaDateAdapter` is a DateAdapter for "@js-joda/core" `ZonedDateTime`
* objects.
*
* It supports timezone handling in so far as js-joda supports
* timezone handling. That is, it only supports the SYSTEM and UTC
* time zones unless you have loaded the optional @js-joda/timezone
* package.
*/
var JodaDateAdapter = /** @class */ (function(_super) {
__extends(JodaDateAdapter, _super);
function JodaDateAdapter(date, options) {
if (options === void 0) {
options = {};
}
var _this = _super.call(this, undefined, options) || this;
_this.date = date;
_this.timezone =
_this.date.zone() === core.ZoneId.SYSTEM ? null : _this.date.zone().toString();
if (_this.timezone === 'Z') {
_this.timezone = 'UTC';
}
if (options.metadata) {
Object.assign(_this.metadata, options.metadata);
}
_this.assertIsValid();
return _this;
}
/**
* Checks if object is an instance of `ZonedDateTime`
*/
JodaDateAdapter.isDate = function(object) {
return object instanceof core.ZonedDateTime;
};
JodaDateAdapter.fromDate = function(date, options) {
return new JodaDateAdapter(date, options);
};
JodaDateAdapter.fromJSON = function(json) {
var zone = json.timezone === null ? core.ZoneId.SYSTEM : core.ZoneId.of(json.timezone);
var date = new JodaDateAdapter(
core.ZonedDateTime.of(
json.year,
json.month,
json.day,
json.hour,
json.minute,
json.second,
json.millisecond * 1000000,
zone,
),
{ duration: json.duration },
);
if (json.metadata) {
Object.assign(date.metadata, json.metadata);
}
return date;
};
JodaDateAdapter.fromDateTime = function(datetime) {
var _a;
var date = JodaDateAdapter.fromJSON(datetime.toJSON());
(_a = date.generators).push.apply(_a, __spread(datetime.generators));
if (datetime.metadata) {
Object.assign(date.metadata, datetime.metadata);
}
return date;
};
Object.defineProperty(JodaDateAdapter.prototype, 'end', {
get: function() {
if (!this.duration) return;
if (this._end) return this._end;
this._end = this.date.plusNanos(this.duration * 1000000);
return this._end;
},
enumerable: false,
configurable: true,
});
JodaDateAdapter.prototype.set = function(prop, value) {
if (prop === 'timezone') {
if (this.timezone === value) return this;
else {
return new JodaDateAdapter(
this.date.withZoneSameInstant(
value === null ? core.ZoneId.SYSTEM : core.ZoneId.of(value),
),
{
duration: this.duration,
generators: this.generators,
},
);
}
} else if (prop === 'duration') {
if (this.duration === value) return this;
else {
return new JodaDateAdapter(this.date, {
duration: value,
generators: this.generators,
});
}
}
throw new core$1.ArgumentError('Unknown prop "' + prop + '" for JodaDateAdapter#set()');
};
JodaDateAdapter.prototype.valueOf = function() {
return this.date.toInstant().toEpochMilli();
};
JodaDateAdapter.prototype.toJSON = function() {
var json = {
timezone: this.timezone,
year: this.date.year(),
month: this.date.monthValue(),
day: this.date.dayOfMonth(),
hour: this.date.hour(),
minute: this.date.minute(),
second: this.date.second(),
millisecond: this.date.nano() / 1000000,
};
if (this.duration) {
json.duration = this.duration;
}
return json;
};
JodaDateAdapter.prototype.assertIsValid = function() {
if (this.duration && this.duration <= 0) {
throw new core$1.InvalidDateAdapterError('If provided, duration must be greater than 0.');
}
return true;
};
JodaDateAdapter.hasTimezoneSupport = true;
return JodaDateAdapter;
})(core$1.DateAdapterBase);
exports.JodaDateAdapter = JodaDateAdapter;
Object.defineProperty(exports, '__esModule', { value: true });
});