Skip to content

Commit b8d8a55

Browse files
committed
Respect requested timezone in date labels
1 parent a5621b7 commit b8d8a55

2 files changed

Lines changed: 63 additions & 10 deletions

File tree

‎src/Exceptionless.Web/ClientApp/src/lib/features/shared/dates.test.ts‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,57 @@
11
import { describe, expect, it } from 'vitest';
22

3-
import { getDifferenceInSeconds, getRelativeTimeFormatUnit, getSetIntervalTime } from './dates';
3+
import { formatDateLabel, getDifferenceInSeconds, getRelativeTimeFormatUnit, getSetIntervalTime } from './dates';
4+
5+
describe('formatDateLabel', () => {
6+
it('preserves local date and relative-label behavior without a timezone override', () => {
7+
// Arrange
8+
const current = new Date(2026, 0, 2, 12);
9+
10+
// Act / Assert
11+
expect(formatDateLabel(new Date(2026, 0, 2), current)).toBe('Today');
12+
expect(formatDateLabel(new Date(2026, 0, 1), current)).toBe('Yesterday');
13+
expect(formatDateLabel(new Date(2026, 0, 2, 14, 30), current)).toContain('Today at ');
14+
});
15+
16+
it('uses the requested timezone for relative days and year boundaries', () => {
17+
// Arrange
18+
const date = new Date('2025-12-31T23:00:00Z');
19+
const current = new Date('2026-01-01T12:00:00Z');
20+
21+
// Act
22+
const label = formatDateLabel(date, current, { timeZone: 'Pacific/Auckland' });
23+
24+
// Assert
25+
expect(label).toContain('Yesterday at ');
26+
const absoluteLabel = formatDateLabel(date, current, { includeRelative: false, timeZone: 'Pacific/Auckland' });
27+
expect(absoluteLabel).not.toContain('2025');
28+
expect(absoluteLabel).not.toContain('2026');
29+
});
30+
31+
it('uses the requested timezone to determine midnight', () => {
32+
// Arrange
33+
const date = new Date('2026-01-01T00:00:00Z');
34+
35+
// Act
36+
const label = formatDateLabel(date, new Date('2026-01-02T12:00:00Z'), { includeRelative: false, month: 'short', timeZone: 'UTC' });
37+
38+
// Assert
39+
expect(label).toBe(new Intl.DateTimeFormat(undefined, { day: 'numeric', month: 'short', timeZone: 'UTC' }).format(date));
40+
});
41+
42+
it('preserves time when local midnight is not midnight in the requested timezone', () => {
43+
// Arrange
44+
const date = new Date(2026, 0, 1);
45+
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone === 'Asia/Kolkata' ? 'UTC' : 'Asia/Kolkata';
46+
47+
// Act
48+
const label = formatDateLabel(date, new Date(2026, 0, 2), { includeRelative: false, month: 'short', timeZone });
49+
50+
// Assert
51+
expect(label).toContain(' at ');
52+
expect(label).toContain(new Intl.DateTimeFormat(undefined, { hour: 'numeric', hour12: true, minute: '2-digit', timeZone }).format(date));
53+
});
54+
});
455

556
const Time = {
657
days: (n: number) => n * 60 * 60 * 24,

‎src/Exceptionless.Web/ClientApp/src/lib/features/shared/dates.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { fromDate, getLocalTimeZone, isSameDay, isSameYear } from '@internationalized/date';
2+
13
/**
24
* Configuration options for date and time formatting
35
*/
@@ -54,19 +56,19 @@ export function formatDate(value: Date): string {
5456
export function formatDateLabel(date: Date, currentDate: Date = new Date(), options: DateLabelFormatOptions = {}): string {
5557
const { hour12 = true, includeRelative = true, joiner = ' at ', month = 'long', timeZone } = options;
5658

57-
const sameDay = date.toDateString() === currentDate.toDateString();
58-
const yesterday = new Date(currentDate);
59-
yesterday.setDate(currentDate.getDate() - 1);
60-
const isYesterday = date.toDateString() === yesterday.toDateString();
61-
const isSameYear = date.getFullYear() === currentDate.getFullYear();
59+
const displayDate = fromDate(date, timeZone ?? getLocalTimeZone());
60+
const displayCurrentDate = fromDate(currentDate, timeZone ?? getLocalTimeZone());
61+
const sameDay = isSameDay(displayDate, displayCurrentDate);
62+
const isYesterday = isSameDay(displayDate, displayCurrentDate.subtract({ days: 1 }));
63+
const sameYear = isSameYear(displayDate, displayCurrentDate);
6264

63-
const isMidnight = date.getHours() === 0 && date.getMinutes() === 0 && date.getSeconds() === 0 && date.getMilliseconds() === 0;
65+
const isMidnight = displayDate.hour === 0 && displayDate.minute === 0 && displayDate.second === 0 && displayDate.millisecond === 0;
6466

6567
// Build the date formatter (omit year if same year)
6668
const dateFmt = new Intl.DateTimeFormat(undefined, {
6769
day: 'numeric',
6870
month,
69-
...(isSameYear ? undefined : { year: 'numeric' }),
71+
...(sameYear ? undefined : { year: 'numeric' }),
7072
...(timeZone ? { timeZone } : undefined)
7173
});
7274

@@ -80,8 +82,8 @@ export function formatDateLabel(date: Date, currentDate: Date = new Date(), opti
8082
}
8183

8284
// Minimal time components per your rules
83-
const sec = date.getSeconds();
84-
const min = date.getMinutes();
85+
const sec = displayDate.second;
86+
const min = displayDate.minute;
8587

8688
const timeOpts: Intl.DateTimeFormatOptions = {
8789
hour: 'numeric',

0 commit comments

Comments
 (0)
⌂ 换网址