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: force utc when converting DateTime values #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

meenzen
Copy link
Contributor

@meenzen meenzen commented Jan 4, 2024

Alternative to #65

As far as I understand we need to pass UTC values to rust anyway, so by forcing UTC both ways we can avoid confusion and user error.

DateTime always defaults to local time and needs to be explicitly converted to utc.

Signed-off-by: Samuel Meenzen <[email protected]>
@meenzen meenzen mentioned this pull request Jan 4, 2024
@arg0d
Copy link
Collaborator

arg0d commented Jan 5, 2024

Oh damn, I didn't realize I was testing in a container where timezone is UTC. I'm gonna try to wrap my head aroud this next week.

@meenzen
Copy link
Contributor Author

meenzen commented Jan 5, 2024

Yeah this seems like a major oversight of GitHub Actions. Really makes you wonder how many timezone related issues remained undetected because of this.

@arg0d
Copy link
Collaborator

arg0d commented Jan 5, 2024

IMO its better to keep bindings layer simple, and don't adjust DateTime for timezone. That way, what you pass into the bindings is what you get in Rust.

On the other hand, adjusting for timezone would ensure Rust library always gets UTC-0 date-time.

It seems difficult to choose the correct behaviour without a specific use case. I'm gonna check how this works in other bindings implementations - Swift, Kotlin, Python, Go.

Timezone can be changed in Docker container with this command, it changes timezone to UTC+2:

ln -sf /usr/share/zoneinfo/Europe/Bucharest /etc/localtime

@arg0d
Copy link
Collaborator

arg0d commented Jun 18, 2024

Ok, so after taking a closer look at this after these few months break, I made another observation while tinkering around with your changes. It looks like DateTime objects are not inherently in local time, it's just that DateTime.Now and DateTime.Parse return objects in local time. Manually constructed DateTime , or DateTime.Min and DateTime.Max, are in universal time. On the other end, it looks like Rust SystemTime::now() returns universal time. I think this is very unfortunate, since it's not possible to both avoid DateTime adjustment in the bindings layer, and also make DateTime.Now and SystemTime::Now compatible.

It's possible to make DateTime.Now and SystemTime::now() compatible, but only by adding adjustment to the bindings layer. The adjustments are pretty simple: convert DateTime to universal time when serializing, and convert DateTime to local time when deserializing. This way, when passing DateTime.Now to bindings, Rust code would see universal time. When receiving SystemTime::now() from Rust, C# code would see local time.

@meenzen
Copy link
Contributor Author

meenzen commented Jun 18, 2024

I really don't like how DateTime woorks in C#, it always causes headaches. For that exact reason I always try to use DateTimeOffset where possible. Everywhere else I just use UTC DateTime.

If we want keep using DateTime here, please add a documentation comment that indicates the type of the returned DateTime objects.

@arg0d
Copy link
Collaborator

arg0d commented Jun 19, 2024

I didn't know about DateTimeOffset, I need to look into that. I'm still not sure what is the best way forward.. To avoid making even more of a mess, I'm just gonna leave things as they are, and merge the other PR that makes the tests pass. I'm not sure where is the best place to document the fact that bindings expect and produce UTC DateTime, so will create a separate issue for that.

@arg0d arg0d mentioned this pull request Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants