How do I convert from unix epoch time and account for daylight saving time in C#?

Viewed 9976

I have a function that converts from unix epoch time to a .NET DateTime value:

public static DateTime FromUnixEpochTime(double unixTime )
{
  DateTime d = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  return d.AddSeconds(unixTime);
}

Where I am (UK) the clocks go one hour forward for summer time.

In Python I get the local Epoch time using time.time() (and for the sake of argument, right now the time is 17:15:00) which gives me a value of 1286122500.

If I convert this back to a human readable time using time.localtime() it converts back to 17:15 as I would expect.

How do I convert unix epoch time back to a .NET DateTime value AND account for local daylight savings time. My function above converts 1286122500 back to 16:15 which is incorrect for my geographic location.

4 Answers
Related