Skip to content

Commit

Permalink
lib: Fix timeformat.parseShortDate()
Browse files Browse the repository at this point in the history
Do the "strip off time and set to midnight" step already for the
reference Date input of parse(). Substracting it afterwards previously
caused the returned date to be off by one day for time zones other than
UTC.

Add `test_timeformat_timezones()` pytest to cover various time zones.
  • Loading branch information
martinpitt committed Jun 26, 2024
1 parent 9f74083 commit 81bc367
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
5 changes: 1 addition & 4 deletions pkg/base1/test-timeformat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ QUnit.test("firstDayOfWeek", assert => {
assert.equal(timeformat.firstDayOfWeek(), 1);
});

// FIXME: This test is currently time zone dependent; parseShortDate() always
// interprets its argument as midnight UTC instead of local time; so it's even
// off by a day for any TZ east of UTC.
QUnit.skip("parsing", assert => {
QUnit.test("parsing", assert => {
cockpit.language = "en";
const en = timeformat.parseShortDate("1/20/2024");
assert.equal(en.getDate(), 20);
Expand Down
10 changes: 4 additions & 6 deletions pkg/lib/timeformat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ export const distanceToNow = (t: Time, addSuffix?: boolean): string => formatDis

// Parse a string localized date like 30.06.21 to a Date Object
export function parseShortDate(dateStr: string): Date {
const parsed = parse(dateStr, dateShortFormat(), new Date());

// Strip time which may cause bugs in calendar
const time = parsed.getTime();
const timePortion = time % (3600 * 1000 * 24);
return new Date(time - timePortion);
// strip off time (i.e. set to midnight), to avoid confusing calendar widgets
const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
return parse(dateStr, dateShortFormat(), today);
}

/***
Expand Down
19 changes: 19 additions & 0 deletions test/pytest/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,22 @@ def test_browser(html):
subprocess.run(['test/common/tap-cdp', f'{BUILDDIR}/test-server',
sys.executable, '-m', *coverage, 'cockpit.bridge', '--debug',
f'./qunit/{html}'], check=True, stderr=subprocess.STDOUT)


# run test-timeformat.ts in different time zones: west/UTC/east
@pytest.mark.parametrize('tz', ['America/Toronto', 'Europe/London', 'UTC', 'Europe/Berlin', 'Australia/Sydney'])
def test_timeformat_timezones(tz):
if not os.path.exists(f'{BUILDDIR}/test-server'):
pytest.skip('no test-server')
# this doesn't get built in rpm/deb package build environments, similar to test_browser()
built_test = './qunit/base1/test-timeformat.html'
if not os.path.exists(built_test):
pytest.skip(f'{built_test} not found')

env = os.environ.copy()
env['TZ'] = tz

# Merge 2>&1 so that pytest displays an interleaved log
subprocess.run(['test/common/tap-cdp', f'{BUILDDIR}/test-server',
sys.executable, '-m', 'cockpit.bridge', '--debug',
built_test], check=True, stderr=subprocess.STDOUT, env=env)

0 comments on commit 81bc367

Please sign in to comment.