0

I have 2 functions to convert between .NET DateTime and Unix timestamp. But I am not sure if I have taken timezones ( of the EPOCH and DateTime parameter) into account. Please help:

public class Util
{
    static DateTime EPOCH = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);

    public static DateTime ConvertUnixTimeStamp(ulong unixTimeStamp)
    {
        var dt = EPOCH.AddSeconds(unixTimeStamp);
        return dt;
    }

    public static double ConvertDatetimeToUnixTimeStamp(DateTime dt)
    {
        TimeSpan t = (dt - EPOCH);
        return Math.Floor(t.TotalSeconds);
    }
}
dan_l
  • 1,692
  • 2
  • 23
  • 40
  • possible duplicate of [How to convert UNIX timestamp to DateTime and vice versa?](http://stackoverflow.com/questions/249760/how-to-convert-unix-timestamp-to-datetime-and-vice-versa) – TrueWill Jan 05 '12 at 03:40
  • If you are worried about timezones, just replace `DateTime` with `DateTimeOffset` – John Jeffery Jan 05 '12 at 03:55

1 Answers1

0
    static DateTime EPOCH = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);

    public static double ConvertDatetimeToUnixTimeStamp(DateTime date , int Time_Zone = 0)
    {
        TimeSpan The_Date = (date - EPOCH);
        return Math.Floor(The_Date.TotalSeconds) - (Time_Zone * 3600);
    }
lion
  • 1