Skip to content

Fixes #1480. Convert DateOnly properties to local timezone#1481

Merged
RicoSuter merged 6 commits into
RicoSuter:masterfrom
Shaddix:master
Sep 16, 2022
Merged

Fixes #1480. Convert DateOnly properties to local timezone#1481
RicoSuter merged 6 commits into
RicoSuter:masterfrom
Shaddix:master

Conversation

@Shaddix

@Shaddix Shaddix commented Jan 16, 2022

Copy link
Copy Markdown
Contributor

Fixes #1480

Comment thread src/NJsonSchema.CodeGeneration.TypeScript/Templates/ConvertToClass.liquid Outdated
@RicoSuter

Copy link
Copy Markdown
Owner

If this PR should still be merged, please fix the conflicts and we'll have a look at it.

# Conflicts:
#	src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptGeneratorSettings.cs
@Shaddix

Shaddix commented May 16, 2022

Copy link
Copy Markdown
Contributor Author

conflicts are fixed

@RicoSuter RicoSuter merged commit d76549a into RicoSuter:master Sep 16, 2022
@RicoSuter

Copy link
Copy Markdown
Owner

Thanks for the PR.

@wuzzeb

wuzzeb commented Sep 21, 2023

Copy link
Copy Markdown

I think this PR doesn't completely fix the issue of dates and timezones, because serialization always assumes a local timezone.

Thus, if the setting ConvertDateToLocalTimezone is false, the parsing code will use new Date("2023-09-21") which assumes UTC but the formatting code calls formatDate (https://github.com/RicoSuter/NJsonSchema/blob/master/src/NJsonSchema.CodeGeneration.TypeScript/Templates/File.FormatDate.liquid) which uses d.getMonth() etc which assume local timezone.

This means if ConvertDateToLocalTimezone is false, you can parse and then immediately convert back to JSON and get a different result.

There should also be a formatUTCDate function which uses instead getUTCMonth and friends, because dates are assumed to be in UTC.

Then, when ConvertDateToLocalTimezone is false, the generated toJSON function should use formtUTCDate instead of formatDate.

Then, both the serialization and deserialization will both use the same expected format and you can serialize followed by a deserialize without changing the data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle DateOnly in typescript client as local date (not UTC)

3 participants