Files
start-os/sdk/lib/util/inMs.ts
Jade 3eb0093d2a feature: Adding in the stopping state (#2677)
* feature: Adding in the stopping state

* chore: Deal with timeout in the sigterm for main

* chore: Update the timeout

* Update web/projects/ui/src/app/pages/apps-routes/app-list/app-list-pkg/app-list-pkg.component.ts

Co-authored-by: Aiden McClelland <3732071+dr-bonez@users.noreply.github.com>

* Update web/projects/ui/src/app/pages/apps-routes/app-show/components/app-show-status/app-show-status.component.ts

Co-authored-by: Aiden McClelland <3732071+dr-bonez@users.noreply.github.com>

---------

Co-authored-by: Aiden McClelland <3732071+dr-bonez@users.noreply.github.com>
2024-07-22 17:40:12 +00:00

32 lines
1.1 KiB
TypeScript

import { DEFAULT_SIGTERM_TIMEOUT } from "../mainFn"
const matchTimeRegex = /^\s*(\d+)?(\.\d+)?\s*(ms|s|m|h|d)/
const unitMultiplier = (unit?: string) => {
if (!unit) return 1
if (unit === "ms") return 1
if (unit === "s") return 1000
if (unit === "m") return 1000 * 60
if (unit === "h") return 1000 * 60 * 60
if (unit === "d") return 1000 * 60 * 60 * 24
throw new Error(`Invalid unit: ${unit}`)
}
const digitsMs = (digits: string | null, multiplier: number) => {
if (!digits) return 0
const value = parseInt(digits.slice(1))
const divideBy = multiplier / Math.pow(10, digits.length - 1)
return Math.round(value * divideBy)
}
export const inMs = (time?: string | number) => {
if (typeof time === "number") return time
if (!time) return undefined
const matches = time.match(matchTimeRegex)
if (!matches) throw new Error(`Invalid time format: ${time}`)
const [_, leftHandSide, digits, unit] = matches
const multiplier = unitMultiplier(unit)
const firstValue = parseInt(leftHandSide || "0") * multiplier
const secondValue = digitsMs(digits, multiplier)
return firstValue + secondValue
}