mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 20:14:49 +00:00
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { inject, Injectable } from '@angular/core'
|
|
import { PatchDB } from 'patch-db-client'
|
|
import { combineLatest, defer, map, shareReplay, switchMap, timer } from 'rxjs'
|
|
import { ApiService } from 'src/app/services/api/embassy-api.service'
|
|
import { DataModel } from 'src/app/services/patch-db/data-model'
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class TimeService {
|
|
private readonly patch = inject<PatchDB<DataModel>>(PatchDB)
|
|
private readonly time$ = defer(() =>
|
|
inject(ApiService).getSystemTime({}),
|
|
).pipe(
|
|
switchMap(({ now, uptime }) =>
|
|
timer(0, 1000).pipe(
|
|
map(index => ({
|
|
now: new Date(now).valueOf() + 1000 * index,
|
|
uptime: uptime + index,
|
|
})),
|
|
),
|
|
),
|
|
shareReplay(1),
|
|
)
|
|
|
|
readonly now$ = combineLatest([
|
|
this.time$,
|
|
this.patch.watch$('serverInfo', 'ntpSynced'),
|
|
]).pipe(map(([{ now }, synced]) => ({ now, synced })))
|
|
|
|
readonly uptime$ = this.time$.pipe(
|
|
map(({ uptime }) => {
|
|
const days = Math.floor(uptime / (24 * 60 * 60))
|
|
const daysSec = uptime % (24 * 60 * 60)
|
|
const hours = Math.floor(daysSec / (60 * 60))
|
|
const hoursSec = uptime % (60 * 60)
|
|
const minutes = Math.floor(hoursSec / 60)
|
|
const seconds = uptime % 60
|
|
return { days, hours, minutes, seconds }
|
|
}),
|
|
)
|
|
}
|