Skip to content

Commit

Permalink
fix(form): support non-UTC timezone date time in VaadinDateTimePicker (
Browse files Browse the repository at this point in the history
…#2882) (CP: main) (#2886)

fix(form): support non-UTC timezone date time in VaadinDateTimePicker (#2882)

Existing for VaadinDateTimePickerStrategy failed when running in non-UTC timezone environment. This change addresses the bug and makes the form library tests always use a non-UTC timezone.

Co-authored-by: Anton Platonov <[email protected]>
Co-authored-by: Luciano Vernaschi <[email protected]>
  • Loading branch information
3 people authored Nov 18, 2024
1 parent 36dfcff commit 0a9bd31
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/ts/lit-form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"build:dts": "tsc --isolatedModules -p tsconfig.build.json",
"lint": "eslint src test",
"lint:fix": "npm run lint -- --fix",
"test": "karma start ../../../karma.config.cjs --port 9876",
"test": "echo 'Using arbitrary non-UTC timezone for VaadinDateTimePickerStrategy tests'; TZ=\"Etc/GMT-12\" karma start ../../../karma.config.cjs --port 9876",
"test:coverage": "npm run test -- --coverage",
"test:watch": "npm run test -- --watch",
"typecheck": "tsc --noEmit"
Expand Down
12 changes: 9 additions & 3 deletions packages/ts/lit-form/src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,17 @@ export class VaadinDateTimeFieldStrategy<
}

override set value(val: T | undefined) {
if (!val || isEmptyObject(val)) {
const timestamp = Date.parse(val as string);

if (!val || isEmptyObject(val) || Number.isNaN(timestamp)) {
super.value = '' as T;
return;
}
const date = Date.parse(val as string);
super.value = (Number.isNaN(date) ? '' : new Date(date).toISOString().slice(0, 19)) as T;

const date = new Date(timestamp);
// Convert to ISO 8601 local combined date and time representation
const tzOffsetMs = 60 * 1000 * date.getTimezoneOffset();
super.value = new Date(timestamp - tzOffsetMs).toISOString().slice(0, 19) as T;
}
}

Expand Down

0 comments on commit 0a9bd31

Please sign in to comment.