In a recent job interview about C# someone asked me the following question:
After checking the documentation I found the foloowing definition for LocalTime:
The local time is equal to the Coordinated Universal Time (UTC) time plus the UTC offset.
And the following one for UniversalTime:
The Coordinated Universal Time (UTC) is equal to the local time minus the UTC offset.
So, the way I see it, UniversalTime is the reverse of LocalTime, but they both do the same thing and get the same results.
So, when should I use each one? Is there any real difference?
The UTC is time at some arbitrarily chosen area (Greenwich), adjusted by few seconds due to Earth orbiting irregularities.
Local time is a time at specific point on Earth. For example, if the UTC time is 0:00 and you are in Cairo, you will observe 2:00, because the time zone in Cairo has an offset of 2 hours ahead (usually denoted "UTC+2").
For that example:
The local time is equal to the Coordinated Universal Time (UTC) time plus the UTC offset.
Local time would be 0:00 + 2h = 2:00.
The Coordinated Universal Time (UTC) is equal to the local time minus the UTC offset.
UTC would be 2:00 - 2h = 0:00.
In context of the question, the interviewer was probably seeking an answer that you would store the time on the server side always as UTC and only convert it to user's local time when displaying it to the users.
This by the way is not specific to C#. If you are interested in more details, wikipedia has a really good explanation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With