UNPKG

@bitblit/ratchet-common

Version:

Common tools for general use

84 lines 3.48 kB
import { DateTime } from 'luxon'; export class TimeZoneRatchet { timezoneIanaName; static PACIFIC = new TimeZoneRatchet('America/Los_Angeles'); constructor(timezoneIanaName) { this.timezoneIanaName = timezoneIanaName; if (!timezoneIanaName || timezoneIanaName.trim().length === 0) { throw 'Timezone cannot be null or empty'; } } get ianaName() { return this.timezoneIanaName; } currentHour() { const rval = DateTime.local().setZone(this.timezoneIanaName).hour; return rval; } toEpochSeconds(dt) { return Math.round(dt.toMillis() / 1000); } nowEpochSeconds() { return Math.floor(DateTime.local().setZone(this.timezoneIanaName).toSeconds()); } startOfTodayEpochSeconds() { const startOfToday = this.toEpochSeconds(DateTime.local().setZone(this.timezoneIanaName).set({ hour: 0, minute: 0, second: 0, millisecond: 0 })); return startOfToday; } startOfMatchingDayEpochSeconds(inputTS) { const startOfToday = this.toEpochSeconds(DateTime.fromMillis(inputTS).set({ hour: 0, minute: 0, second: 0, millisecond: 0 })); return startOfToday; } startOfMatchingDayEpochMS(inputTS) { return this.startOfMatchingDayEpochSeconds(inputTS) * 1000; } startOfCurrentHourEpochSeconds() { const rval = this.toEpochSeconds(DateTime.local().setZone(this.timezoneIanaName).set({ minute: 0, second: 0, millisecond: 0 })); return rval; } startOfCurrentMinuteEpochSeconds() { const rval = this.toEpochSeconds(DateTime.local().setZone(this.timezoneIanaName).set({ second: 0, millisecond: 0 })); return rval; } startOfCurrentSecondEpochSeconds() { const rval = this.toEpochSeconds(DateTime.local().setZone(this.timezoneIanaName).set({ millisecond: 0 })); return rval; } startOfTodayEpochMS() { const startOfToday = DateTime.local().setZone(this.timezoneIanaName).set({ hour: 0, minute: 0, second: 0, millisecond: 0 }).toMillis(); return startOfToday; } dailySlotCount(slotWidthMs) { return Math.ceil(86400000 / slotWidthMs); } currentTimeSlotIdx(slotWidthMs) { if (slotWidthMs < 1) { throw new Error('Cannot process with slot less than one ms wide'); } const startOfToday = this.startOfTodayEpochMS(); const now = new Date().getTime(); const delta = now - startOfToday; const idx = Math.floor(delta / slotWidthMs); return idx; } matchingTimeSlotIdx(timestamp, slotWidthMs) { if (slotWidthMs < 1) { throw new Error('Cannot process with slot less than one ms wide'); } const startOfDay = this.startOfMatchingDayEpochMS(timestamp); const delta = timestamp - startOfDay; const idx = Math.floor(delta / slotWidthMs); return idx; } startOfCurrentSlotEpochMS(slotWidthMs) { const startOfToday = this.startOfTodayEpochMS(); const currentIdx = this.currentTimeSlotIdx(slotWidthMs); return startOfToday + currentIdx * slotWidthMs; } startOfMatchingSlotEpochMS(timestamp, slotWidthMs) { const startOfDay = this.startOfMatchingDayEpochMS(timestamp); const currentIdx = this.matchingTimeSlotIdx(timestamp, slotWidthMs); return startOfDay + currentIdx * slotWidthMs; } } //# sourceMappingURL=time-zone-ratchet.js.map