30 lines
1 KiB
TypeScript
30 lines
1 KiB
TypeScript
import { ref, computed } from 'vue'
|
|
|
|
export default function useTime() {
|
|
// the day is split in 1000 parts, so we start in the morning
|
|
const time = ref(230)
|
|
|
|
function updateTime() {
|
|
time.value = (time.value + 0.1) % 1000
|
|
}
|
|
|
|
const timeOfDay = computed(() => {
|
|
if (time.value >= 900 || time.value < 80) return 'night'
|
|
if (time.value >= 80 && time.value < 120) return 'morning0'
|
|
if (time.value >= 120 && time.value < 150) return 'morning1'
|
|
if (time.value >= 150 && time.value < 240) return 'morning2'
|
|
if (time.value >= 700 && time.value < 800) return 'evening0'
|
|
if (time.value >= 800 && time.value < 850) return 'evening1'
|
|
if (time.value >= 850 && time.value < 900) return 'evening2'
|
|
return 'day'
|
|
})
|
|
|
|
const clock = computed(() => {
|
|
const t = time.value * 86.4 // 1000 ticks to 86400 seconds (per day)
|
|
const h = ~~(t / 3600.0)
|
|
const m = ~~((t / 3600.0 - h) * 60.0)
|
|
return `${(h + 2) % 24}:${m < 10 ? '0' : ''}${m}`
|
|
})
|
|
|
|
return { time, updateTime, timeOfDay, clock }
|
|
}
|