4

In a recent job interview about C# someone asked me the following question:

  • What is the difference between LocalTime and UniversalTime in c# and when should I use them?

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?

Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266
  • 2
    It's hard to answer this without knowing how much you already know about time zones. It would also help to know where you live, so I could give you concrete examples that would apply to you. – Jon Skeet Jan 30 '15 at 13:06
  • It is already in your question. UTC is timeline from which other timezones derived. So, if you consider London time it is UTC time. And your local time is London time + or - offset. So, if you are ahead of london time then it will be UTC time + . If you are behind then -. – Amit Jan 30 '15 at 13:14

1 Answers1

8

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.

ya23
  • 14,226
  • 9
  • 46
  • 43