-
Notifications
You must be signed in to change notification settings - Fork 242
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
Timezone Conversions #378
Comments
Thank you for your message. It's great that you are aware of the timezone issue in your project. It's true that the DateTimeService.cs file with the Now => DateTime.UtcNow code is a step in the right direction. To convert between local time and UTC time, you can use the TimeZoneInfo class in C#. This class provides methods to convert between different time zones and can be used to convert the UTC time to the local time of the user. You can also use the DateTimeOffset structure, which includes both the UTC time and the local time offset. I hope this helps! Let me know if you have any further questions. |
Well, this isn't necessarily an issue with "my" project, but with the whole project in general. This is because, as far as I can see, we don't do any conversion, which means everybody always sees the time based on the device location the project is hosted on. Switching from I'll start working on it in the following days, and see how far I can come. I'll probably write some extension method that somehow gets the user his current timezone and converts the UTC timestamp to the client's timezone. |
@neozhu, I've found something pretty cool. This library sends a javascript call to the user his browser, and dynamically changes the times where specified to the browser his local time, so whoever watches the page sees the time based on their own TimeZone with little to no effort. I've come pretty far with implementing this, but I got stuck on a certain point and hoped you would be able to help me out. On the We add a <ToLocal DateTime="context.Item.TimeStamp" Format="dd/mm/yyyy HH:MM:ss"></ToLocal> Which results in the following screenshot: Instead of: Unfortunately, it goes wrong with If we can get this to work, we are basically done with showing the proper times across the whole website, since it's really easy to implement, especially because we will have all the You can test this by using this branch! |
@Bram1903 |
You mean instead of having to specify I can create a PR as a draft, so we can both work on it, without having to merge it instantly, so you are able to modify it a bit. |
Problem Explained
Currently, we are storing
DateTime
based on the location of the device the application is hosted on, but we don't take into account that if someone accesses this website from another timezone it doesn't show up properly.We should store all the
DateTime
's asDateTime.UtcNow
in the database, and convert the time back to a user his local time on request. Therefore we would be able to convert a UTC timestamp back to the user his timezone, so everyone sees a timestamp in their own local format.I would recommend reading this very interesting article by Shay Rojansky, who is a member of the entity framework team at Microsoft, and lead developer of the PostgreSQL entity framework provider.
For example currently, because I'm using PostgreSQL I had to enable
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
, which results in all the timestamps being converted to UTC now when writing to the Database, but because we are only showing the end user the local saved timezone for me all the timestamps are not converted properly, which is also a problem for people who are outside of the timezone the application is hosted on.I suggest we start working on a proper conversion for timezones to boost this project in terms of professionalism, and production readiness.
Problem Example
Local console log time:
Website log time:
As you can see my current time was
14:20
, but since theDateTime
is being saved inUTC
the website will show up inUTC
12:20
.As I said earlier this isn't only a problem when using PostgreSQL, but also when someone would be outside of the timezone the application is hosted on, which would make it impossible to use worldwide.
The text was updated successfully, but these errors were encountered: