From 10fea0b372bb5713b2ad2efb900f0197b79a687c Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sat, 16 Nov 2019 20:16:47 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20Use=20DateTimeOffset=20to=20conv?= =?UTF-8?q?ert=20Unix=20timestamps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- X10D/DateTimeExtensions.cs | 9 ++------- X10D/DoubleExtensions.cs | 13 ------------- X10D/Int64Extensions.cs | 10 ++++++++-- X10D/SingleExtensions.cs | 11 ----------- 4 files changed, 10 insertions(+), 33 deletions(-) diff --git a/X10D/DateTimeExtensions.cs b/X10D/DateTimeExtensions.cs index 0c3130f..bc8c2c2 100644 --- a/X10D/DateTimeExtensions.cs +++ b/X10D/DateTimeExtensions.cs @@ -11,11 +11,6 @@ /// public static class DateTimeExtensions { - /// - /// Represents the Unix epoch - midnight on January 1, 1970. - /// - public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0); - /// /// Calculates someone's age based on a date of birth. /// @@ -146,8 +141,8 @@ /// Returns a Unix timestamp representing the provided . public static long ToUnixTimeStamp(this DateTime time, bool useMillis = false) { - TimeSpan difference = time - UnixEpoch; - return (long)(useMillis ? difference.TotalMilliseconds : difference.TotalSeconds); + DateTimeOffset offset = time; + return useMillis ? offset.ToUnixTimeSeconds() : offset.ToUnixTimeMilliseconds(); } /// diff --git a/X10D/DoubleExtensions.cs b/X10D/DoubleExtensions.cs index 1dd50e7..9974f41 100644 --- a/X10D/DoubleExtensions.cs +++ b/X10D/DoubleExtensions.cs @@ -31,19 +31,6 @@ public static double DegreesToRadians(this double angle) => Math.PI * angle / 180.0; - /// - /// Converts the to a treating it as a Unix timestamp. - /// - /// The timestamp. - /// Optional. Whether or not the input value should be treated as milliseconds. Defaults - /// to . - /// Returns a representing seconds since the Unix - /// epoch. - public static DateTime FromUnixTimestamp(this double timestamp, bool isMillis = false) => - isMillis - ? DateTimeExtensions.UnixEpoch.AddMilliseconds(timestamp) - : DateTimeExtensions.UnixEpoch.AddSeconds(timestamp); - /// /// Converts the to a []. /// diff --git a/X10D/Int64Extensions.cs b/X10D/Int64Extensions.cs index 889eb72..d8c6cb7 100644 --- a/X10D/Int64Extensions.cs +++ b/X10D/Int64Extensions.cs @@ -82,8 +82,14 @@ /// to . /// Returns a representing seconds since the Unix /// epoch. - public static DateTime FromUnixTimestamp(this long timestamp, bool isMillis = false) => - ((double)timestamp).FromUnixTimestamp(isMillis); + public static DateTime FromUnixTimestamp(this long timestamp, bool isMillis = false) + { + DateTimeOffset offset = isMillis + ? DateTimeOffset.FromUnixTimeMilliseconds(timestamp) + : DateTimeOffset.FromUnixTimeSeconds(timestamp); + + return offset.DateTime; + } /// /// Converts the to a []. diff --git a/X10D/SingleExtensions.cs b/X10D/SingleExtensions.cs index ec37e84..ef4357c 100644 --- a/X10D/SingleExtensions.cs +++ b/X10D/SingleExtensions.cs @@ -31,17 +31,6 @@ public static float DegreesToRadians(this float angle) => (float)((double)angle).DegreesToRadians(); - /// - /// Converts the to a treating it as a Unix timestamp. - /// - /// The timestamp. - /// Optional. Whether or not the input value should be treated as milliseconds. Defaults - /// to . - /// Returns a representing seconds since the Unix - /// epoch. - public static DateTime FromUnixTimestamp(this float timestamp, bool isMillis = false) => - ((double)timestamp).FromUnixTimestamp(isMillis); - /// /// Converts the to a []. ///