Skip to content

Instantly share code, notes, and snippets.

@musoftware
Created April 19, 2016 22:51
Show Gist options
  • Save musoftware/714239ac692cfa765d929f8f8c4eed99 to your computer and use it in GitHub Desktop.
Save musoftware/714239ac692cfa765d929f8f8c4eed99 to your computer and use it in GitHub Desktop.
Unix Timestamp Converter
internal static class UnixTimestampConverter
{
public static DateTime Epoch
{
get
{
return new DateTime(1970, 1, 1, 0, 0, 0, 0);
}
}
public static DateTime ToDateTime(this uint unixTimestamp)
{
return UnixTimestampConverter.Epoch.AddSeconds((double) unixTimestamp);
}
public static DateTime ToDateTime(this ulong unixTimestamp)
{
return UnixTimestampConverter.Epoch.AddSeconds((double) unixTimestamp);
}
public static ulong ToUnixTime(this DateTime dateTime)
{
return (ulong) (dateTime.ToUniversalTime() - UnixTimestampConverter.Epoch).TotalSeconds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment