rrule-rust
Version:
RRule implementation for Node.js written in Rust
125 lines (124 loc) • 4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DateTime = void 0;
/**
* Represents a date and time. Either local or UTC.
*/
class DateTime {
constructor(numeric) {
this.numeric = numeric;
}
get year() {
return Math.floor(this.numeric / 100000000000);
}
get month() {
return Math.floor((this.numeric / 1000000000) % 100);
}
get day() {
return Math.floor((this.numeric / 10000000) % 100);
}
get hour() {
return Math.floor((this.numeric / 100000) % 100);
}
get minute() {
return Math.floor((this.numeric / 1000) % 100);
}
get second() {
return Math.floor((this.numeric / 10) % 100);
}
get utc() {
return this.numeric % 10 == 1;
}
/**
* Creates a new DateTime object from the given date and time components.
*/
static create(year, month, day, hour, minute, second, utc) {
const numeric = year * 100000000000 +
month * 1000000000 +
day * 10000000 +
hour * 100000 +
minute * 1000 +
second * 10 +
(utc ? 1 : 0);
return new DateTime(numeric);
}
/**
* This method is shorthand for `DateTime.create` with `utc` set to `false`.
*/
static local(year, month, day, hour, minute, second) {
return DateTime.create(year, month, day, hour, minute, second, false);
}
/**
* This method is shorthand for `DateTime.create` with `utc` set to `true`.
*/
static utc(year, month, day, hour, minute, second) {
return DateTime.create(year, month, day, hour, minute, second, true);
}
/**
* Creates a new DateTime object from the given plain object.
*/
static fromObject(object, options) {
return DateTime.create(object.year, object.month, object.day, object.hour, object.minute, object.second, !!options?.utc);
}
/**
* Creates a new DateTime object from provided string representation of a date according to RFC 5545.
*/
static fromString(str) {
const typeError = new TypeError('Invalid date time string');
if (str.length > 16 || str.length < 15) {
throw typeError;
}
const year = parseInt(str.slice(0, 4));
const month = parseInt(str.slice(4, 6));
const day = parseInt(str.slice(6, 8));
const hour = parseInt(str.slice(9, 11));
const minute = parseInt(str.slice(11, 13));
const second = parseInt(str.slice(13, 15));
const utc = str.endsWith('Z');
if (isNaN(year) ||
isNaN(month) ||
isNaN(day) ||
isNaN(hour) ||
isNaN(minute) ||
isNaN(second)) {
throw typeError;
}
return DateTime.create(year, month, day, hour, minute, second, utc);
}
/** @internal */
static fromNumeric(numeric) {
return new DateTime(numeric);
}
/**
* Converts DateTime into a plain object.
*/
toObject() {
return {
year: this.year,
month: this.month,
day: this.day,
hour: this.hour,
minute: this.minute,
second: this.second,
};
}
/**
* Converts DateTime into ISO 8601 string:
* - `YYYYMMDDTHHMMSSZ` for UTC
* - `YYYYMMDDTHHMMSS` for local
*/
toString() {
const year = this.year.toString().padStart(4, '0');
const month = this.month.toString().padStart(2, '0');
const day = this.day.toString().padStart(2, '0');
const hour = this.hour.toString().padStart(2, '0');
const minute = this.minute.toString().padStart(2, '0');
const second = this.second.toString().padStart(2, '0');
return `${year}${month}${day}T${hour}${minute}${second}${this.utc ? 'Z' : ''}`;
}
/** @internal */
toNumeric() {
return this.numeric;
}
}
exports.DateTime = DateTime;