Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor: replace is24Hour with hour12 and update logic accordingly

Andrii Andrushchyshyndf111b21c17b
diff --git a/client/src/app/[site]/main/components/MainSection/Chart.tsx b/client/src/app/[site]/main/components/MainSection/Chart.tsx
index ce937b904..ccd8a8857 100644
--- a/client/src/app/[site]/main/components/MainSection/Chart.tsx
+++ b/client/src/app/[site]/main/components/MainSection/Chart.tsx
@@ -8,7 +8,7 @@ import { GetOverviewBucketedResponse } from "../../../../../api/analytics/useGet
import { APIResponse } from "../../../../../api/types";
import { Time } from "../../../../../components/DateSelector/types";
import { formatSecondsAsMinutesAndSeconds } from "../../../../../lib/utils";
-import { userLocale, is24Hour } from "../../../../../lib/dateTimeUtils";
+import { userLocale, hour12 } from "../../../../../lib/dateTimeUtils";
export const formatter = Intl.NumberFormat(userLocale, { notation: "compact" });
@@ -293,9 +293,9 @@ export function Chart({
format: (value) => {
const dt = DateTime.fromJSDate(value).setLocale(userLocale);
if (time.mode === "day" || time.mode === "last-24-hours" ) {
- return dt.toFormat(is24Hour ? 'HH:mm' : 'ha');
+ return dt.toFormat(hour12 ? "ha" : "HH:mm");
}
- return dt.toFormat(is24Hour ? 'dd MMM' : 'MMM d');
+ return dt.toFormat(hour12 ? "MMM d" : "dd MMM");
},
}}
axisLeft={{
@@ -376,13 +376,13 @@ export function Chart({
const formatDateTime = (dt: DateTime, bucket: TimeBucket) => {
const showMinutes = ["minute", "five_minutes", "ten_minutes", "fifteen_minutes", "hour"].includes(bucket);
const options: Intl.DateTimeFormatOptions = {
- month: 'short',
- day: 'numeric',
- hour: 'numeric',
- hour12: !is24Hour,
+ month: "short",
+ day: "numeric",
+ hour: "numeric",
+ hour12: hour12,
};
- if (showMinutes && is24Hour) {
- options.minute = 'numeric';
+ if (showMinutes && !hour12) {
+ options.minute = "numeric";
}
if (bucket === "day") {
options.minute = undefined;
diff --git a/client/src/components/Sessions/SessionCard.tsx b/client/src/components/Sessions/SessionCard.tsx
index a3dee8202..fa478251b 100644
--- a/client/src/components/Sessions/SessionCard.tsx
+++ b/client/src/components/Sessions/SessionCard.tsx
@@ -23,7 +23,7 @@ import { cn, formatter, getCountryName } from "../../lib/utils";
import { formatDuration } from "../../lib/dateTimeUtils";
import { Badge } from "../ui/badge";
import { SessionDetails } from "./SessionDetails";
-import { userLocale, is24Hour } from "../../lib/dateTimeUtils";
+import { userLocale, hour12 } from "../../lib/dateTimeUtils";
interface SessionCardProps {
session: GetSessionsResponse[number];
@@ -194,7 +194,7 @@ export function SessionCard({ session, onClick, userId }: SessionCardProps) {
zone: "utc",
}).setLocale(userLocale)
.toLocal()
- .toFormat(is24Hour ? 'dd MMM, HH:mm' : 'MMM d, h:mm a')
+ .toFormat(hour12 ? "MMM d, h:mm a" : "dd MMM, HH:mm")
}
</span>
<span className="hidden md:block">{durationFormatted}</span>
diff --git a/client/src/components/Sessions/SessionDetails.tsx b/client/src/components/Sessions/SessionDetails.tsx
index b03424f1f..26bc76f70 100644
--- a/client/src/components/Sessions/SessionDetails.tsx
+++ b/client/src/components/Sessions/SessionDetails.tsx
@@ -29,7 +29,7 @@ import { OperatingSystem } from "../../app/[site]/components/shared/icons/Operat
import { cn, getCountryName, getLanguageName } from "../../lib/utils";
import { formatDuration } from "../../lib/dateTimeUtils";
import { Button } from "../ui/button";
-import { is24Hour } from "../../lib/dateTimeUtils";
+import { hour12 } from "../../lib/dateTimeUtils";
// Component to display a single pageview or event
function PageviewItem({
@@ -45,7 +45,7 @@ function PageviewItem({
}) {
const isEvent = item.type !== "pageview";
const timestamp = DateTime.fromSQL(item.timestamp, { zone: "utc" }).toLocal();
- const formattedTime = timestamp.toFormat(is24Hour ? "HH:mm:ss" : "h:mm:ss a");
+ const formattedTime = timestamp.toFormat(hour12 ? "h:mm:ss a" : "HH:mm:ss");
// Calculate duration if this is a pageview and we have the next timestamp
let duration = null;
diff --git a/client/src/components/SiteSessionChart.tsx b/client/src/components/SiteSessionChart.tsx
index 4f24d81cd..2da141d8e 100644
--- a/client/src/components/SiteSessionChart.tsx
+++ b/client/src/components/SiteSessionChart.tsx
@@ -4,7 +4,7 @@ import { DateTime } from "luxon";
import { useMemo } from "react";
import { GetOverviewBucketedResponse } from "../api/analytics/useGetOverviewBucketed";
import { formatter } from "../lib/utils";
-import { is24Hour } from "../lib/dateTimeUtils";
+import { hour12 } from "../lib/dateTimeUtils";
interface SiteSessionChartProps {
data: GetOverviewBucketedResponse;
@@ -61,7 +61,7 @@ export function SiteSessionChart({
tickValues: 3,
format: (value) => {
const time = DateTime.fromJSDate(value);
- return time.toFormat(is24Hour ? "HH:mm" : "ha");
+ return time.toFormat(hour12 ? "ha" : "HH:mm");
},
}}
axisLeft={{
@@ -98,7 +98,7 @@ export function SiteSessionChart({
<div className="text-xs mb-1">Sessions</div>
<div className="flex justify-between text-xs w-20">
<div className="text-muted-foreground">
- { currentTime.toFormat(is24Hour ? "HH:mm" : "ha") }
+ { currentTime.toFormat(hour12 ? "ha" : "HH:mm") }
</div>
<div className="font-medium">{currentY.toLocaleString()}</div>
</div>
diff --git a/client/src/lib/dateTimeUtils.ts b/client/src/lib/dateTimeUtils.ts
index 8d7b84678..3facb5ac3 100644
--- a/client/src/lib/dateTimeUtils.ts
+++ b/client/src/lib/dateTimeUtils.ts
@@ -1,9 +1,13 @@
-import { Duration, DateTime, DurationLikeObject, Settings } from "luxon";
+import { Duration, DurationLikeObject, Settings } from "luxon";
// Detect user locale
export const userLocale = typeof navigator !== "undefined" ? navigator.language : "en-US";
+// Detect user 12h/24h preference
+const resolved = new Intl.DateTimeFormat(userLocale, { hour: "numeric" }).resolvedOptions();
+export const hour12 = resolved.hourCycle === "h12";
+
// Set default locale globally
Settings.defaultLocale = userLocale;
@@ -106,7 +110,7 @@ export function formatDuration(minutes: number, seconds: number): string {
/**
* Formats a given hour and minute into a localized time string,
- * respecting either 24-hour or 12-hour format based on the `is24Hour` flag.
+ * respecting either 24-hour or 12-hour format based on the `hour12` flag.
*
* @param hour - The hour of the day (0–23).
* @param minute - The minute of the hour (0–59).
@@ -120,7 +124,7 @@ export const formatLocalTime = (hour: number, minute: number): string => {
const formatter = new Intl.DateTimeFormat(undefined, {
hour: "numeric",
minute: "2-digit",
- hour12: !is24Hour,
+ hour12: hour12,
});
// Create a new Date object set to the given hour and minute
@@ -132,9 +136,6 @@ export const formatLocalTime = (hour: number, minute: number): string => {
};
-// Detect user 12h/24h preference
-const resolved = new Intl.DateTimeFormat(undefined, { hour: "numeric" }).resolvedOptions();
-export const is24Hour = resolved.hourCycle === "h23" || resolved.hourCycle === "h24";
// localized full day names
// e.g., "Monday" "Tuesday" "Wednesday"
@@ -148,7 +149,7 @@ export const shortDayNames:string[] = getLocalizedWeekdayNames(userLocale, "shor
// e.g., "00" "01" "02"
export const hourLabels: string[] = getLocalizedTimeLabels(userLocale, {
hour: "numeric",
- hour12: !is24Hour,
+ hour12: hour12,
});
// localized time (hour with minutes) labels
@@ -156,5 +157,5 @@ export const hourLabels: string[] = getLocalizedTimeLabels(userLocale, {
export const hourMinuteLabels: string[] = getLocalizedTimeLabels(userLocale, {
hour: "numeric",
minute: "2-digit",
- hour12: !is24Hour,
+ hour12: hour12,
});