UNPKG

time-kinesis

Version:

A simple and powerful node.js Library to control time

256 lines (255 loc) 11.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const timezones_1 = require("./timezones"); const units_1 = require("./units"); const unit_factor_1 = require("./unit.factor"); const formatter_1 = require("./formatter"); let nativeTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone; function addMonth(datetime, amount) { let date = new Date(datetime.getDate()); date.setMonth(date.getMonth() + amount); return new DateTime(date, datetime.timezone); } ; function addYear(datetime, amount) { let date = new Date(datetime.getDate()); date.setFullYear(date.getFullYear() + amount); return new DateTime(date, datetime.timezone); } ; function startOfMonth(datetime) { let days = datetime.getDate().getDate() - 1; return datetime.sub(days, units_1.units.days).startOf(units_1.units.day); } ; function startOfYear(datetime) { let months = datetime.getDate().getMonth(); return datetime.sub(months, units_1.units.months).startOf(units_1.units.month); } ; function endOfMonth(datetime) { return datetime.add(1, units_1.units.month).startOf(units_1.units.month).sub(1); } ; function endOfYear(datetime) { return datetime.add(1, units_1.units.year).startOf(units_1.units.year).sub(1); } ; function yearDiff(from, to, considerTimezone = true) { let target = considerTimezone ? to.tz(from.timezone) : to; return target.getDate().getFullYear() - from.getDate().getFullYear(); } ; function monthDiff(from, to, considerTimezone = true) { let target = considerTimezone ? to.tz(from.timezone) : to; let yearMonths = yearDiff(from, target, false) * 12; return yearMonths + target.getDate().getMonth() - from.getDate().getMonth(); } ; function diff(from, to, unit = units_1.units.millisecond, considerTimezone = true) { let target = considerTimezone ? to.tz(from.timezone) : to; // @ts-ignore let factor = unit_factor_1.unitFactor[unit]; if (factor) return (target.unix() - from.unix()) / factor; if ([units_1.units.month, units_1.units.months].some(u => u == unit)) return monthDiff(from, target, false); if ([units_1.units.year, units_1.units.years].some(u => u == unit)) return yearDiff(from, target, false); throw new Error(`cannot add unit ${unit}: kinesis not mapped`); } ; function add(datetime, amount, unit = units_1.units.millisecond) { if (unit == units_1.units.millisecond) return new DateTime(datetime.unix() + amount, datetime.timezone); // @ts-ignore let factor = unit_factor_1.unitFactor[unit]; if (factor) // @ts-ignore return new DateTime(datetime.unix() + (unit_factor_1.unitFactor[unit] * amount), datetime.timezone); if ([units_1.units.month, units_1.units.months].some(u => u == unit)) return addMonth(datetime, amount); if ([units_1.units.year, units_1.units.years].some(u => u == unit)) return addYear(datetime, amount); throw new Error(`cannot add unit ${unit}: kinesis not mapped`); } ; function startOf(datetime, unit) { // @ts-ignore let factor = unit_factor_1.unitFactor[unit]; if (factor) { // @ts-ignore let rest = new DateTime(datetime.getDate(), timezones_1.timezones.UTC).tz(nativeTimezone).unix() % factor; return new DateTime(datetime.unix() - rest, datetime.timezone); } if ([units_1.units.month, units_1.units.months].some(u => u == unit)) return startOfMonth(datetime); if ([units_1.units.year, units_1.units.years].some(u => u == unit)) return startOfYear(datetime); throw new Error(`cannot get start of unit ${unit}: kinesis not mapped`); } ; function endOf(datetime, unit) { // @ts-ignore let factor = unit_factor_1.unitFactor[unit]; if (factor) { // @ts-ignore let rest = new DateTime(datetime.getDate(), timezones_1.timezones.UTC).tz(nativeTimezone).unix() % factor; return new DateTime(datetime.unix() - rest + factor - 1, datetime.timezone); } if ([units_1.units.month, units_1.units.months].some(u => u == unit)) return endOfMonth(datetime); if ([units_1.units.year, units_1.units.years].some(u => u == unit)) return endOfYear(datetime); throw new Error(`cannot get start of unit ${unit}: kinesis not mapped`); } ; class DateTime { //unix returns unix epoch. // it is the number of milliseconds between the DateTime instance and the start of 1970; unix() { return this.getDate().getTime(); } ; //add returns a new DateTime instance, adding the given time amount. The current DateTime instance will not be altered; add(amount = 0, unit = units_1.units.millisecond) { return add(this, amount, unit); } ; //sub returns a new DateTime instance, subtracting the given time amount. The current DateTime instance will not be altered; sub(amount = 0, unit = units_1.units.millisecond) { return this.add(-amount, unit); } ; diff(datetime, unit = units_1.units.millisecond, considerTimezone = true) { return diff(this, datetime, unit, considerTimezone); } ; //setTimezone alter the instance timezone to the given one; //Date and time will *NOT* be converted. To convert date and time, use the tz method; setTimezone(timezone) { this.timezone = timezone; return; } ; //tzOffset returns the number of milliseconds between the instance timezone, and the given one; tzOffset(timezone) { return timezones_1.getTimezoneOffset(timezone) - timezones_1.getTimezoneOffset(this.timezone); } ; //tz returns a new instance of DateTime in the given timezone. Date and time will be converted too. The current DateTime instance will not be altered; tz(timezone) { return new DateTime(this.getDate().getTime() + this.tzOffset(timezone), timezone); } ; //utc method returns a new instance of DateTime in UTC timezone. Date and time will be converted too. The current DateTime instance will not be altered; utc() { return this.tz(timezones_1.timezones.UTC); } ; //format method returns a string, with de DateTime instance formatted on the given pattern; //format patterns will be documented soon. But you can use the same patterns of moment.js; format(pattern) { return formatter_1.format(this, pattern); } ; //isValid return true if this instance of DateTime is valid; //invalid instances are created when invalid parameters are given in the constructor method; isValid() { return !isNaN(this.unix()); } ; //startOf return a new instance of DateTime in the same timezone, but in the start of the given time unit. The current DateTime instance will not be altered; //For Example: it will return the same day on it's first millisecond, if 'day' is the given time unit; //For Example: it will return the same minute on it's first millisecond, if 'minute' is the given time unit; //For Example: it will return the same month on it's first millisecond, if 'month' is the given time unit; startOf(unit) { return startOf(this, unit); } ; //endOf return a new instance of DateTime in the same timezone, but in the end of the given time unit. The current DateTime instance will not be altered; //For Example: it will return the same day on it's last millisecond, if 'day' is the given time unit; //For Example: it will return the same minute on it's last millisecond, if 'minute' is the given time unit; //For Example: it will return the same month on it's last millisecond, if 'month' is the given time unit; endOf(unit) { return endOf(this, unit); } ; //isEqual return true if this instance is equal the given one; //Additionally, the precision unit can be used. If not, millisecond unit will be considered; isEqual(datetime, precision = units_1.units.millisecond) { if (precision == units_1.units.millisecond) return datetime.unix() == this.unix(); return this.startOf(precision).unix() == datetime.startOf(precision).unix(); } ; //isAfter return true if this instance is after the given one; //Additionally, the precision unit can be used. If not, millisecond unit will be considered; isAfter(datetime, precision = units_1.units.millisecond) { if (precision == units_1.units.millisecond) return datetime.unix() < this.unix(); return datetime.startOf(precision).unix() < this.startOf(precision).unix(); } ; //isAfterOrEqual return true if this instance is after, or equal the given one; //Additionally, the precision unit can be used. If not, millisecond unit will be considered; isAfterOrEqual(datetime, precision = units_1.units.millisecond) { if (precision == units_1.units.millisecond) return datetime.unix() <= this.unix(); return datetime.startOf(precision).unix() <= this.startOf(precision).unix(); } ; //isBefore return true if this instance is before the given one; //Additionally, the precision unit can be used. If not, millisecond unit will be considered; isBefore(datetime, precision = units_1.units.millisecond) { if (precision == units_1.units.millisecond) return datetime.unix() > this.unix(); return datetime.startOf(precision).unix() > this.startOf(precision).unix(); } ; //isBeforeOrEqual return true if this instance is before, or equal the given one; //Additionally, the precision unit can be used. If not, millisecond unit will be considered; isBeforeOrEqual(datetime, precision = units_1.units.millisecond) { if (precision == units_1.units.millisecond) return datetime.unix() >= this.unix(); return datetime.startOf(precision).unix() >= this.startOf(precision).unix(); } ; toJSON() { return this.format('YYYY-MM-DD HH:mm:ss.sss Z ZZ'); } constructor(date, timezone) { // @ts-ignore if (date instanceof DateTime) { this.date = new Date(date.date); this.timezone = timezone || date.timezone; return; } timezone = timezone || timezones_1.getDefault(); // @ts-ignore this.timezone = timezone; if (!date && typeof date != 'number') { this.date = new Date(); return; } if (date instanceof Date) this.date = date; else this.date = new Date(date); return this; } ; getDate() { return this.date; } } exports.DateTime = DateTime; ; DateTime.prototype.toString = function () { return this.format('YYYY-MM-DD HH:mm:ss.sss Z ZZ'); }; function datetime(...props) { return new DateTime(...props); } exports.datetime = datetime; ;