import {Ticker} from '@avocado/timing'; const MAGIC_TO_FIT_HOUR_INTO_USHORT = 2730; export class WorldTime { constructor() { this._hour = 0; this.ticker = new Ticker(1); this._state = 0; this.ticker.on('tick', () => { this._state = (this._hour * MAGIC_TO_FIT_HOUR_INTO_USHORT) >> 0; }); } static format(time) { const rawHour = (time + 12) % 12; const hour = Math.floor(0 === Math.floor(rawHour) ? 12 : rawHour); const minutes = String(Math.floor((rawHour - Math.floor(rawHour)) * 60)).padStart(2, '0'); if (time >= 12) { return `${hour}:${minutes} pm`; } else { return `${hour}:${minutes} am`; } } get hour() { return this._hour; } set hour(hour) { this._hour = hour; this._state = (this._hour * MAGIC_TO_FIT_HOUR_INTO_USHORT) >> 0; } patchState(steps) { for (const step of steps) { const {path, value} = step; if ('/' === path) { this._hour = value / MAGIC_TO_FIT_HOUR_INTO_USHORT; } } } secondsPerHour() { return 60; } get state() { return this._state; } tick(elapsed) { this._hour += (elapsed / this.secondsPerHour()); this._hour = this._hour % 24; this.ticker.tick(elapsed); } }