humus-old/common/world-time.js

62 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-04-04 07:31:21 -05:00
import {Ticker} from '@avocado/timing';
export class WorldTime {
constructor() {
2019-04-08 08:01:39 -05:00
this._hour = 0;
2019-04-04 17:13:06 -05:00
this.ticker = new Ticker(1);
2019-04-04 07:31:21 -05:00
this._state = 0;
this.ticker.on('tick', () => {
2019-04-08 08:01:39 -05:00
this._state = this._hour;
2019-04-04 07:31:21 -05:00
});
}
2019-04-12 11:01:55 -05:00
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`;
}
}
2019-04-08 08:01:39 -05:00
get hour() {
return this._hour;
}
set hour(hour) {
this._hour = hour;
this._state = hour;
2019-04-04 07:31:21 -05:00
}
humanReadable() {
}
2019-04-08 08:01:39 -05:00
patchState(steps) {
for (const step of steps) {
const {path, value} = step;
2019-04-05 22:14:17 -05:00
if ('/' === path) {
2019-04-08 08:01:39 -05:00
this._hour = value;
2019-04-05 22:14:17 -05:00
}
}
2019-04-05 15:16:50 -05:00
}
2019-04-04 07:31:21 -05:00
secondsPerHour() {
return 60;
}
get state() {
return this._state;
}
tick(elapsed) {
2019-04-08 08:01:39 -05:00
this._hour += (elapsed / this.secondsPerHour());
this._hour = this._hour % 24;
2019-04-04 07:31:21 -05:00
this.ticker.tick(elapsed);
}
}