humus-old/common/world-time.js
2019-04-05 23:14:17 -04:00

53 lines
996 B
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;
});
}
hour() {
return this.hour;
}
humanReadable() {
const rawHour = (this.hour + 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 (this.hour >= 12) {
return `${hour}:${minutes} pm`;
}
else {
return `${hour}:${minutes} am`;
}
}
patchState(patch) {
for (const {path, value} of patch) {
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);
}
}