Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does .Net get UTC?

I am considering switching the time of our application from local to UTC. However, someone asked the question how it calculated UTC and I don't know myself. I did a search, but didn't see the answer anywhere.

Does it simply convert local time based on timezone?

If so, is there any way to account for a computer whose time is off and has no internet connection? (I figure not, but thought I would ask anyway).

like image 533
EatATaco Avatar asked Dec 19 '25 14:12

EatATaco


1 Answers

how it calculated UTC

It is not calculated at all. It is exactly the other way around, UTC is simple and it is local time that requires an Internet connection to accurately track daylight saving time transition rules.

Local time is entirely too ambiguous to reliably run an operating system on. Windows' clock runs on UTC. Also used for example for the various time stamps on files on your disk, your files don't suddenly get an hour older on a DST transition.

So DateTime.UtcNow directly pinvokes the GetSystemTimeAsFileTime() winapi function without massaging the returned value at all. It is DateTime.Now that's difficult. Clearly you don't want to hesitate to switch :)

like image 105
Hans Passant Avatar answered Dec 21 '25 06:12

Hans Passant