Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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