From 32860ca22013de13c3e0d4f2e60cd25f1a040e82 Mon Sep 17 00:00:00 2001 From: Anders Bjerner Date: Mon, 14 Feb 2022 15:24:24 +0100 Subject: [PATCH] Improved the Unix time logic in the "EssentialsTime" class - Introduced new "CurrentUnixTimeSeconds" static property - Introduced new "CurrentUnixTimeMilliseconds" static property - Marked "UnixTimestamp" property as obsolete - Introduced new "UnixTimeSeconds" property - Introduced new "UnixTimeMilliseconds" property - Introduced new "ToUnixTimeSeconds" methodty - Introduced new "ToUnixTimeMilliseconds" method --- src/Skybrud.Essentials/Time/EssentialsTime.cs | 49 +++++++++++++++++-- .../Time/UnixTime/UnixTimeTests.cs | 41 ++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/src/Skybrud.Essentials/Time/EssentialsTime.cs b/src/Skybrud.Essentials/Time/EssentialsTime.cs index a4f7474..47ff83d 100644 --- a/src/Skybrud.Essentials/Time/EssentialsTime.cs +++ b/src/Skybrud.Essentials/Time/EssentialsTime.cs @@ -48,6 +48,16 @@ public class EssentialsTime : IComparable, IComparable, ICompara /// Gets an instance of representing the start of the Unix Epoch (AKA 0 seconds). /// public static EssentialsTime Zero => FromUnixTimeSeconds(0); + + /// + /// Returns the amount of seconds since the start of the Unix epoch - that is 1st of January, 1970 - 00:00:00 GMT. + /// + public static long CurrentUnixTimeSeconds => (long) UnixTimeUtils.CurrentSeconds; + + /// + /// Returns the amount of milliseconds since the start of the Unix epoch - that is 1st of January, 1970 - 00:00:00 GMT. + /// + public static long CurrentUnixTimeMilliseconds => (long) UnixTimeUtils.CurrentMilliseconds; #endregion @@ -137,8 +147,19 @@ public class EssentialsTime : IComparable, IComparable, ICompara /// /// Gets the UNIX timestamp (amount of seconds since the start of the Unix Epoch) for this . /// + [Obsolete("Use 'UnixTimeSeconds' instead.")] public long UnixTimestamp => (long) UnixTimeUtils.ToSeconds(DateTimeOffset); + /// + /// Gets the UNIX timestamp in seconds (amount of time the start of the Unix Epoch) for this . + /// + public long UnixTimeSeconds => (long) UnixTimeUtils.ToSeconds(DateTimeOffset); + + /// + /// Gets the UNIX timestamp in milliseconds (amount of time the start of the Unix Epoch) for this . + /// + public long UnixTimeMilliseconds => (long) UnixTimeUtils.ToMilliseconds(DateTimeOffset); + /// /// Gets the time's offset from Coordinated Universal Time (UTC). /// @@ -177,17 +198,17 @@ public class EssentialsTime : IComparable, IComparable, ICompara /// /// Gets whether the Unix timestamp of this is 0. /// - public bool IsZero => UnixTimestamp == 0; + public bool IsZero => UnixTimeSeconds == 0; /// /// Gets whether the Unix timestamp of this is less than 0. /// - public bool IsNegative => UnixTimestamp < 0; + public bool IsNegative => UnixTimeSeconds < 0; /// /// Gets whether the Unix timestamp of this is greater than 0. /// - public bool IsPositive => UnixTimestamp > 0; + public bool IsPositive => UnixTimeSeconds > 0; /// /// Gets whether the date of this matches the current day. @@ -705,6 +726,28 @@ public EssentialsTime ToTimeZone(TimeZoneInfo timeZone) { return new EssentialsTime(DateTimeOffset, timeZone); } + /// + /// Returns the number of milliseconds that have elapsed since the start of the Unix epoch (1st of January, 1970 - 00:00:00 GMT). + /// + /// The number of milliseconds that have elapsed since the start of the Unix epoch (1st of January, 1970 - 00:00:00 GMT). + /// + /// https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tounixtimeseconds?view=netstandard-2.0 + /// + public long ToUnixTimeSeconds() { + return (long) UnixTimeUtils.ToSeconds(DateTimeOffset); + } + + /// + /// Returns the number of milliseconds that have elapsed since the start of the Unix epoch (1st of January, 1970 - 00:00:00 GMT). + /// + /// The number of milliseconds that have elapsed since the start of the Unix epoch (1st of January, 1970 - 00:00:00 GMT). + /// + /// https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tounixtimemilliseconds?view=netstandard-2.0 + /// + public long ToUnixTimeMilliseconds() { + return (long) UnixTimeUtils.ToMilliseconds(DateTimeOffset); + } + /// /// Gets a new instance of representing the start of the day. /// diff --git a/src/UnitTestProject1/Time/UnixTime/UnixTimeTests.cs b/src/UnitTestProject1/Time/UnixTime/UnixTimeTests.cs index cca294f..54a8401 100644 --- a/src/UnitTestProject1/Time/UnixTime/UnixTimeTests.cs +++ b/src/UnitTestProject1/Time/UnixTime/UnixTimeTests.cs @@ -1,4 +1,5 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using Skybrud.Essentials.Time; using Skybrud.Essentials.Time.Iso8601; using Skybrud.Essentials.Time.UnixTime; using System; @@ -263,6 +264,46 @@ public void ToMillisecondsDateTimeOffset() { Assert.AreEqual(1643886000123, result2); } + + [TestMethod] + public void GetCurrentSeconds() { + + // DateTimeOffset offers similar logic, but only in newer versions of .NET, which is why we're not using it + // internally. But insce it's available here in the test project, we can use it for validating our own values :D + + // NOTICE: Working with current time is always a bit tricky, so if we're unlucky, "result1" may be a second + // ahead of the other result. This is not an error! + + long result1 = DateTimeOffset.Now.ToUnixTimeSeconds(); + long result2 = (long) UnixTimeUtils.CurrentSeconds; + long result3 = EssentialsTime.CurrentUnixTimeSeconds; + long result4 = EssentialsTime.Now.UnixTimeSeconds; + + Assert.AreEqual(result1, result2, "#2"); + Assert.AreEqual(result1, result3, "#3"); + Assert.AreEqual(result1, result4, "#4"); + + } + + [TestMethod] + public void GetCurrentMilliseconds() { + + // DateTimeOffset offers similar logic, but only in newer versions of .NET, which is why we're not using it + // internally. But insce it's available here in the test project, we can use it for validating our own values :D + + // NOTICE: Working with current time is always a bit tricky, so if we're unlucky, "result1" may be a second + // ahead of the other result. This is not an error! + + long result1 = DateTimeOffset.Now.ToUnixTimeMilliseconds(); + long result2 = (long) UnixTimeUtils.CurrentMilliseconds; + long result3 = EssentialsTime.CurrentUnixTimeMilliseconds; + long result4 = EssentialsTime.Now.UnixTimeMilliseconds; + + Assert.AreEqual(result1, result2, "#2"); + Assert.AreEqual(result1, result3, "#3"); + Assert.AreEqual(result1, result4, "#4"); + + } }