humus-old/common/world-time.js
2019-04-12 11:01:55 -05:00

62 lines
1.1 KiB
JavaScript

import {Ticker} from '@avocado/timing';
export class WorldTime {
constructor() {
this._hour = 0;
this.ticker = new Ticker(1);
this._state = 0;
this.ticker.on('tick', () => {
this._state = this._hour;
});
}
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 = hour;
}
humanReadable() {
}
patchState(steps) {
for (const step of steps) {
const {path, value} = step;
if ('/' === path) {
this._hour = value;
}
}
}
secondsPerHour() {
return 60;
}
get state() {
return this._state;
}
tick(elapsed) {
this._hour += (elapsed / this.secondsPerHour());
this._hour = this._hour % 24;
this.ticker.tick(elapsed);
}
}