Unix DateTime Discrepancy

advertisements

The function below is supposed to convert the following unix time into a C# DateTime object:

1362506086000

The time above is supposed to represent March 5, 2012 12:54 PM.

However, everytime I run it through the function below, it changes to 5:54 PM:

3/5/2013 5:54:46 PM

    static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
    static readonly double MaxUnixSeconds = (DateTime.MaxValue - UnixEpoch).TotalSeconds;

    public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
    {
        return unixTimeStamp > MaxUnixSeconds
           ? UnixEpoch.AddMilliseconds(unixTimeStamp)
           : UnixEpoch.AddSeconds(unixTimeStamp);
    }

How can I get the unix time to be properly represented as 12:54 instead of 5:54?


It sounds like a timezone issue. Ensure that the timezone used to generate the Unix time is the same as the timezone on the Windows system, or convert accordingly.