humus-old/common/world-time.js

61 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-04-04 07:31:21 -05:00
import {Ticker} from '@avocado/timing';
const MAGIC_TO_FIT_HOUR_INTO_USHORT = 2730;
2019-04-04 07:31:21 -05:00
export class WorldTime {
constructor() {
2019-04-08 08:01:39 -05:00
this._hour = 0;
2019-04-23 15:26:56 -05:00
this.ticker = new Ticker(0.25);
2019-04-04 07:31:21 -05:00
this._state = 0;
this.ticker.on('tick', () => {
this._state = (this._hour * MAGIC_TO_FIT_HOUR_INTO_USHORT) >> 0;
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 = (this._hour * MAGIC_TO_FIT_HOUR_INTO_USHORT) >> 0;
2019-04-04 07:31:21 -05:00
}
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) {
this._hour = value / MAGIC_TO_FIT_HOUR_INTO_USHORT;
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);
}
}