Skip to content

Correct date conversions - #75

Merged
oliverbock merged 1 commit into
JavascriptNet:masterfrom
spahnke:date-time
Mar 4, 2019
Merged

oliverbock merged 1 commit into
JavascriptNet:masterfrom
spahnke:date-time

Conversation

@spahnke

@spahnke spahnke commented Mar 4, 2019

Copy link
Copy Markdown
Collaborator

Unfortunately we discovered another nasty edge case regarding the date conversion 👀😞
@oliverbock, @Ablu: Can you please review this very carefully? I only added new tests and did not touch the old ones. AFAIK there is no API to set the current time zone; it is determined by the operation system. So I could not write tests for a specific time zone. Rather I executed the date tests manually after setting my time zone locally. They were green in all the samples I took.

Problem

We need to be very careful with date conversions because C# does not use the IANA timezone database to determine offsets from UTC / daylight savings time. However, V8 does use the IANA timezone database which leads to discrepancies in the date conversion.

Example

Germany did not observe daylight savings time from 1950-1979 and therefore observed UTC+1 throughout the whole year. However, C# thinks Germany did observe daylight savings time and assumes incorrectly that Germany observed UTC+2 during the summer time.

V8

new Date(1978, 5, 15) // "Thu Jun 15 1978 00:00:00 GMT+0100 (Mitteleuropäische Normalzeit)"

C#
If we get the ticks since 1970-01-01 from V8 to construct a UTC DateTime object we get "1978-06-14 23:00:00" which is correct. A subsequent call to .ToLocalTime() on that date object yields "1978-06-15 01:00:00" which is wrong; it should be "1978-06-15 00:00:00"!

The same problem exists in the other direction.

We therefore construct date objects directly from the date components we get from V8 and vice versa. I used this approach because I did not want to pull in a dependency on e.g. NodaTime. The C++ API currently doesn't offer a correct implementation either.

We need to be very careful with date conversions because C# does not use
the IANA timezone database to determine offsets from UTC / daylight
savings time. However, V8 does use the IANA timezone database which lead
to discrepancies in the date conversion.

Example:
Germany did not observe daylight savings time from 1950-1979 and therefore
observed UTC+1 throughout the whole year. However, C# thinks Germany did
observe daylight savings time and assumes incorrectly that Germany observed
UTC+2 during the summer time.

V8
new Date(1978, 5, 15) // "Thu Jun 15 1978 00:00:00 GMT+0100 (Mitteleuropäische Normalzeit)"

C#
If we get the ticks since 1970-01-01 from V8 to construct a UTC DateTime
object we get "1978-06-14 23:00:00" which is correct. A subsequent call
to .ToLocalTime() on that date object yields "1978-06-15 01:00:00" which
is wrong; it should be "1978-06-15 00:00:00"!

The same problem exists in the other direction.

We therefore construct date objects directly from the date components we
get from V8 and vice versa.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants