![]() ![]() If you are looking for more, use the following with more ticks precision long EPOCH = - new DateTime(1970, 1, 1,0,0,0,0). Literally speaking the epoch is Unix time 0 (midnight ), but 'epoch' is often used as a synonym for Unix time. (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since Janu(midnight UTC/GMT), not counting leap seconds (in ISO 8601: ). I suggest to save it as long not int as EPOCH_2038_SAFE long EPOCH_2038_SAFE = This is called the Year 2038 problem where the 32-bit signed Unix time will overflow. On systems where the representation of Unix time is as a signed 32-bit number, the representation will end after 231 - 1 seconds which will happen at 3:14:08 on 19 January 2038 UTC. Return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(epoch) To get the EPOCH with seconds only you may use var Epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds Īnd convert the Epoch to DateTime with the following method private DateTime Epoch2UTCNow(int epoch) It's well documented here DateTimeOffset.ToUnixTimeMilliseconds Net Framework 4.6 or higher Use the method DateTimeOffset.ToUnixTimeMilliseconds() It returns the number of milliseconds that have elapsed since T00:00:00.000Z. The Unix epoch is also called Unix time, POSIX time, or Unix timestamp. This means that at midnight of January 1, 1970, Unix time was 0. The Unix epoch is the number of seconds that have elapsed since Januat midnight UTC time minus the leap seconds. If you dont want to create the epoch DateTime instance when moving from DateTime to epoch you can also do: public static long ToUnixTime(this DateTime date) ToUniversalTime will convert a Local (or Unspecified) DateTime to Utc. This number is the number of seconds since the Unix epoch, which was midnight (00:00:00) on January 1, 1970, in Coordinated Universal Time (UTC).Leap seconds are ignored in Linux timestamps, so they arent analogous to real time. Return Convert.ToInt64((date.ToUniversalTime() - epoch).TotalSeconds) Linux timestamps hold a number rather than a date and time. To allow for date's Kind being either Utc or Local, use ToUniversalTime: public static long ToUnixTime(this DateTime date) Note the comment below from CodesInChaos that the above FromUnixTime returns a DateTime with a Kind of Utc, which is fine, but the above ToUnixTime is much more suspect in that doesn't account for what kind of DateTime the given date is. Return Convert.ToInt64((date - epoch).TotalSeconds) Public static long ToUnixTime(this DateTime date) Var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) With all credit to LukeH, I've put together some extension methods for easy use: public static DateTime FromUnixTime(this long unixTime)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |