Skip to content

Commit

Permalink
date validator
Browse files Browse the repository at this point in the history
  • Loading branch information
insmac committed Dec 23, 2024
1 parent 4afefdf commit 8e24991
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/web-console/src/scenes/Editor/Metrics/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isValidDate } from "./../../../utils/dateTime"
import { format, formatISO, subMinutes } from "date-fns"
import { utcToLocal } from "../../../utils/dateTime"
import uPlot from "uplot"
Expand Down Expand Up @@ -292,8 +293,7 @@ export const isDateToken = (token: string) => {

// Converts tokens like `now-1h` or `now-7d` to date string
export const durationTokenToDate = (token: string) => {
if (!isDateToken(token))
return token.startsWith("now") ? "Invalid date" : token
if (!isDateToken(token)) return isValidDate(token) ? token : "Invalid date"
const now = new Date()
if (token === "now") return formatISO(now)
const [_, _operator, value, unit] = token.match(/now(-)?(\d+)([a-z]+)$/)!
Expand Down
4 changes: 4 additions & 0 deletions packages/web-console/src/utils/dateTime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { format } from "date-fns"
import { TZDate } from "@date-fns/tz"

export const isValidDate = (date: string): boolean => {
return !isNaN(Date.parse(date))
}

export const getLocalTimeZone = () => {
return Intl.DateTimeFormat().resolvedOptions().timeZone
}
Expand Down

0 comments on commit 8e24991

Please sign in to comment.