question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Timezone Conversions

See original GitHub issue

Problem Explained

Currently, we are storingDateTime 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 as DateTime.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:

image

Website log time:

image

As you can see my current time was 14:20, but since the DateTime is being saved in UTC the website will show up in UTC 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.

Issue Analytics

  • State:closed
  • Created 5 months ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Bram1903commented, May 5, 2023

I’ve been able to fix it! The issue was that the code didn’t know the DateTime kind, which after specifying the kind works great!

Old:

DateTime="context.Item.TimeStamp"

New:

DateTime="DateTime.SpecifyKind(context.Item.TimeStamp, DateTimeKind.Utc)"

The converting now works flawlessly, as shown in the below screenshots!

Screenshot of how the time is being saved in the Database: image

Screenshot of the log page: image

Testing

I opened two browsers at the same time, and for one I manually modified the browser’s timezone and set it to: America/Los_Angeles Below screenshot is the result:

The left side is my local timezone Europe/Amsterdam, and on the right, I have the modified browser which shows the results in America/Los_Angeles

image

1reaction
Bram1903commented, May 5, 2023

@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 Logs.Razor page we show the log timestamp in UTC, which is where we want to convert that time to the user his local time as follows:

We add a CellTemplate under the PropertyColumn where we have our timestamp containing the following line of code:

<ToLocal DateTime="context.Item.TimeStamp" Format="dd/mm/yyyy HH:MM:ss"></ToLocal>

Which results in the following screenshot:

image

Instead of:

image

Unfortunately, it goes wrong with DateTime="context.Item.TimeStamp", whereas when I change context.Item.TimeStamp to DateTime.UtcNow in order to test if it is capable of converting that to the proper timezone based on the browser it works perfectly! The weird thing is that it does change the format in both cases, but when using DateTime="context.Item.TimeStamp" it doesn’t convert that time at all but still applies the correct date formatting. You can test this by changing: Format="dd/mm/yyyy HH:MM:ss" to Format="default", and you will see that it does apply the proper date formatting to DateTime="context.Item.TimeStamp", but doesn’t correctly convert it to the proper timezone.

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 DateTime’s in UTC. I hope you are able to figure out why it does work when using DateTime.UtcNow, but doesn’t when using the timestamp from the LogDto model.

You can test this by using this branch!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Time Zone Converter – Time Difference Calculator
Provides time zone conversions taking into account Daylight Saving Time (DST), local time zone and accepts present, past, or future dates.
Read more >
Time Converter and World Clock - Conversion at a Glance ...
World Time Buddy (WTB) is a convenient world clock, a time zone converter, and an online meeting scheduler. It's one of the best...
Read more >
Dateful Time Zone Converter
Dateful Time Zone Converter converts times instantly as you type. Convert between major world cities, countries and timezones in both directions.
Read more >
Time zone converter
Compare time in different time zones. Find the best time for a phone meeting.
Read more >
Time Zone Converter
Time zone and local time converter. Compare time between two, three, four and more time zones, with standard and daylight saving times.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found