Created
June 12, 2015 03:49
-
-
Save jakejscott/b1e69cdd8a419fcaeca0 to your computer and use it in GitHub Desktop.
UnixDate.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class UnixDateTime | |
{ | |
public static DateTimeOffset FromUnixTimeSeconds(long seconds) | |
{ | |
if (seconds < -62135596800L || seconds > 253402300799L) | |
throw new ArgumentOutOfRangeException("seconds", seconds, ""); | |
return new DateTimeOffset(seconds * 10000000L + 621355968000000000L, TimeSpan.Zero); | |
} | |
public static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds) | |
{ | |
if (milliseconds < -62135596800000L || milliseconds > 253402300799999L) | |
throw new ArgumentOutOfRangeException("milliseconds", milliseconds, ""); | |
return new DateTimeOffset(milliseconds * 10000L + 621355968000000000L, TimeSpan.Zero); | |
} | |
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp) | |
{ | |
DateTime datetime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(unixTimeStamp).ToLocalTime(); | |
return datetime; | |
} | |
public static long ToUnixTimeSeconds(this DateTimeOffset utcDateTime) | |
{ | |
return utcDateTime.Ticks / 10000000L - 62135596800L; | |
} | |
public static long ToUnixTimeMilliseconds(this DateTimeOffset utcDateTime) | |
{ | |
return utcDateTime.Ticks / 10000L - 62135596800000L; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment