Skip to content

Commit 1875aeb

Browse files
authored
Merge pull request #2354 from Jamesbarford/feat/chose-date-fmt
Toggle status page date format between 24hr and 12hr
2 parents 759a79d + 2574e33 commit 1875aeb

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

site/frontend/src/pages/compare/prefs.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {createStoredValue} from "../../storage";
2+
import {DATE_FMT_24HR} from "../../utils/formatting";
23

34
export const PREF_FILTERS_OPENED = createStoredValue(
45
"compare.filters-opened",
@@ -8,3 +9,7 @@ export const PREF_AGGREGATIONS_OPENED = createStoredValue(
89
"compare.aggregations-opened",
910
false
1011
);
12+
export const PREF_DATETIME_FORMAT = createStoredValue(
13+
"general.date-time-format",
14+
DATE_FMT_24HR
15+
);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script setup lang="ts">
2+
import {DATE_FMT_12HR} from "../../utils/formatting";
3+
4+
const props = defineProps<{
5+
dateFmt: string;
6+
toggleDate(): void;
7+
}>();
8+
</script>
9+
10+
<template>
11+
<span>
12+
<input
13+
@click="props.toggleDate"
14+
:checked="props.dateFmt === DATE_FMT_12HR"
15+
type="checkbox"
16+
id="user-date-fmt-toggle"
17+
/>
18+
<label for="user-date-fmt-toggle">Show 12hr date format</label><br />
19+
</span>
20+
</template>

site/frontend/src/pages/status/page.vue

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {
99
formatISODate,
1010
formatSecondsAsDuration,
1111
parseDateIsoStringOrNull,
12+
DATE_FMT_12HR,
13+
DATE_FMT_24HR,
14+
preferredDateTimeFormat,
1215
} from "../../utils/formatting";
1316
import {useExpandedStore} from "../../utils/expansion";
1417
import {
@@ -20,6 +23,7 @@ import {
2023
} from "./data";
2124
import Collector from "./collector.vue";
2225
import CommitSha from "./commit-sha.vue";
26+
import DateFmtPicker from "./date-format-selection.vue";
2327
2428
const loading = ref(true);
2529
@@ -204,6 +208,16 @@ const {toggleExpanded: toggleExpandedErrors, isExpanded: hasExpandedErrors} =
204208
205209
const tableWidth = 8;
206210
211+
function toggleDate() {
212+
let nextDateFmt: string;
213+
if (preferredDateTimeFormat.value === DATE_FMT_24HR) {
214+
nextDateFmt = DATE_FMT_12HR;
215+
} else {
216+
nextDateFmt = DATE_FMT_24HR;
217+
}
218+
preferredDateTimeFormat.value = nextDateFmt;
219+
}
220+
207221
loadStatusData(loading);
208222
</script>
209223

@@ -212,8 +226,12 @@ loadStatusData(loading);
212226
<div class="status-page-wrapper">
213227
<div class="timeline-wrapper">
214228
<h1>Timeline</h1>
215-
<span class="small-padding-bottom">
229+
<span class="local-time-message small-padding-bottom">
216230
<strong>Times are local.</strong>
231+
<DateFmtPicker
232+
:toggleDate="toggleDate"
233+
:dateFmt="preferredDateTimeFormat"
234+
/>
217235
</span>
218236
<span class="small-padding-bottom">
219237
<ExpectedCurrentRequestCompletion />
@@ -421,4 +439,11 @@ loadStatusData(loading);
421439
.small-padding-bottom {
422440
padding-bottom: 8px;
423441
}
442+
443+
.local-time-message {
444+
display: flex;
445+
flex-direction: column;
446+
align-items: center;
447+
padding-bottom: 12px;
448+
}
424449
</style>

site/frontend/src/utils/formatting.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {parseISO, format} from "date-fns";
2+
import {createPersistedRef} from "../storage";
3+
import {PREF_DATETIME_FORMAT} from "../pages/compare/prefs";
24

35
// `time` has to be in seconds
46
export function formatSecondsAsDuration(time: number): string {
@@ -20,10 +22,18 @@ export function formatSecondsAsDuration(time: number): string {
2022
return s;
2123
}
2224

23-
// Takes a date like `2025-09-10T08:22:47.161348Z` -> `"2025-09-10 08:22:47"`
25+
// Date formats taken from https://date-fns.org/v4.1.0/docs/format
26+
export const DATE_FMT_24HR = "yyyy-MM-dd HH:mm:ss";
27+
export const DATE_FMT_12HR = "yyyy-MM-dd hh:mm:ss a";
28+
29+
export const preferredDateTimeFormat = createPersistedRef(PREF_DATETIME_FORMAT);
30+
31+
// Takes a date like `2025-09-10T08:22:47.161348Z` and formats it according to
32+
// the user preference stored in local storage (either 12 hour or 24 hour format).
2433
export function formatISODate(dateString?: string): string {
2534
if (dateString) {
26-
return format(parseISO(dateString), "yyyy-MM-dd HH:mm:ss");
35+
const dateFmt = preferredDateTimeFormat.value;
36+
return format(parseISO(dateString), dateFmt);
2737
}
2838
return "";
2939
}

0 commit comments

Comments
 (0)