| Property |
Value
|
| Module |
Time Utilities
|
| File |
dashboard/lib/utils/timeUtils.ts
|
| Language |
TypeScript
|
| Lines |
108
|
| Type |
Utility Module
|
| Dependencies |
Browser Intl API, Date API
|
Overview
timeUtils.ts provides time parsing and formatting utilities for the dashboard's time-travel query controls. The module exports four functions: parseDuration for converting duration strings (e.g., "30s", "5m", "2h", "1d") to seconds, parseTimestampToUnixEpoch for converting timestamp strings to Unix epoch seconds, getCurrentTimeInSystemTimezone for formatting the current time using the browser's Intl.DateTimeFormat in the system timezone, and formatUnixEpoch for converting Unix epoch seconds back to ISO strings. These utilities are used by the TimeControls component to parse user-provided time inputs for historical stats queries on the graph visualization pages.
Code Reference
Source Location
dashboard/lib/utils/timeUtils.ts
Signature
export function parseDuration(durationStr: string): number
export function parseTimestampToUnixEpoch(
timestamp: string,
timezone?: string
): number
export function getCurrentTimeInSystemTimezone(): string
export function formatUnixEpoch(unixEpoch: number): string
Import
import {
parseDuration,
parseTimestampToUnixEpoch,
getCurrentTimeInSystemTimezone,
formatUnixEpoch,
} from "../lib/utils/timeUtils"
I/O Contract
parseDuration
| Parameter |
Type |
Description
|
durationStr |
string |
Duration string matching pattern /^(\d+)([smhd])$/
|
| Returns |
number |
Duration in seconds
|
| Throws |
Error |
If the format does not match the expected pattern
|
Supported units:
| Unit |
Multiplier |
Example
|
s |
1 |
"30s" -> 30
|
m |
60 |
"5m" -> 300
|
h |
3600 |
"2h" -> 7200
|
d |
86400 |
"1d" -> 86400
|
parseTimestampToUnixEpoch
| Parameter |
Type |
Default |
Description
|
timestamp |
string |
(required) |
Timestamp string parseable by the JavaScript Date constructor
|
timezone |
string |
System timezone via Intl.DateTimeFormat().resolvedOptions().timeZone |
The timezone for interpretation (used for documentation; actual parsing relies on Date constructor behavior)
|
| Returns |
number |
|
Unix epoch time in seconds (floored)
|
| Throws |
Error |
|
If the timestamp produces an invalid Date
|
getCurrentTimeInSystemTimezone
| Parameter |
Type |
Description
|
| (none) |
|
No parameters
|
| Returns |
string |
Current time formatted as YYYY-MM-DDTHH:mm:ss in the system timezone, using the en-CA locale with Intl.DateTimeFormat
|
formatUnixEpoch
| Parameter |
Type |
Description
|
unixEpoch |
number |
Unix epoch time in seconds
|
| Returns |
string |
ISO 8601 string (e.g., "2024-01-15T10:30:00.000Z")
|
Usage Examples
Parsing Duration Strings
import { parseDuration } from "../lib/utils/timeUtils"
parseDuration("30s") // returns 30
parseDuration("5m") // returns 300
parseDuration("2h") // returns 7200
parseDuration("1d") // returns 86400
try {
parseDuration("invalid")
} catch (e) {
// Error: Invalid duration format. Use format like "30s", "5m", "2h", "1d"
}
Parsing Timestamps
import { parseTimestampToUnixEpoch } from "../lib/utils/timeUtils"
const epoch = parseTimestampToUnixEpoch("2024-01-15T10:30:00")
// Returns Unix epoch in seconds, e.g., 1705312200
Getting Current Time for Display
import { getCurrentTimeInSystemTimezone } from "../lib/utils/timeUtils"
const now = getCurrentTimeInSystemTimezone()
// Returns e.g., "2024-01-15T10:30:00" in the system timezone
Formatting Epoch for Display
import { formatUnixEpoch } from "../lib/utils/timeUtils"
const isoStr = formatUnixEpoch(1705312200)
// Returns "2024-01-15T10:30:00.000Z"
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.