To my surprise it turns out the .NET framework doesn't have built in conversion to and from UNIX timestamps. Here's two helper functions I should try to remember:

static double ConvertToUnixTimestamp(DateTime date)
{
        DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        TimeSpan delta = date - origin;
        return Math.Floor(delta.TotalSeconds);
}

static DateTime ConvertFromUnixTimestamp(double timestamp)
{
        DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        return origin.AddSeconds(timestamp);
}

Comments