From 155a604812a535bb02efe2efcaf2d4694b676a71 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 29 Apr 2022 23:21:00 +0100 Subject: [PATCH] Add FromUnixTime(Milli)Seconds for unsigned types --- X10D.Tests/src/Time/UInt16Tests.cs | 12 +++++++++ X10D.Tests/src/Time/UInt32Tests.cs | 12 +++++++++ X10D.Tests/src/Time/UInt64Tests.cs | 12 +++++++++ X10D/src/Time/UInt16Extensions.cs | 42 ++++++++++++++++++++++++++++++ X10D/src/Time/UInt32Extensions.cs | 42 ++++++++++++++++++++++++++++++ X10D/src/Time/UInt64Extensions.cs | 42 ++++++++++++++++++++++++++++++ 6 files changed, 162 insertions(+) diff --git a/X10D.Tests/src/Time/UInt16Tests.cs b/X10D.Tests/src/Time/UInt16Tests.cs index 89390d9..e63d596 100644 --- a/X10D.Tests/src/Time/UInt16Tests.cs +++ b/X10D.Tests/src/Time/UInt16Tests.cs @@ -7,6 +7,18 @@ namespace X10D.Tests.Time; [CLSCompliant(false)] public class UInt16Tests { + [TestMethod] + public void FromUnixTimeMilliseconds_ShouldBeEpoch_GivenZero() + { + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), ((ushort)0).FromUnixTimeMilliseconds()); + } + + [TestMethod] + public void FromUnixTimeSeconds_ShouldBeEpoch_GivenZero() + { + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), ((ushort)0).FromUnixTimeSeconds()); + } + [TestMethod] public void IsLeapYear_ShouldBeFalse_GivenMultipleOf100() { diff --git a/X10D.Tests/src/Time/UInt32Tests.cs b/X10D.Tests/src/Time/UInt32Tests.cs index 8036cd8..86eef87 100644 --- a/X10D.Tests/src/Time/UInt32Tests.cs +++ b/X10D.Tests/src/Time/UInt32Tests.cs @@ -7,6 +7,18 @@ namespace X10D.Tests.Time; [CLSCompliant(false)] public class UInt32Tests { + [TestMethod] + public void FromUnixTimeMilliseconds_ShouldBeEpoch_GivenZero() + { + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), 0U.FromUnixTimeMilliseconds()); + } + + [TestMethod] + public void FromUnixTimeSeconds_ShouldBeEpoch_GivenZero() + { + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), 0U.FromUnixTimeSeconds()); + } + [TestMethod] public void IsLeapYear_ShouldBeFalse_GivenMultipleOf100() { diff --git a/X10D.Tests/src/Time/UInt64Tests.cs b/X10D.Tests/src/Time/UInt64Tests.cs index 26d5ad2..9720f3a 100644 --- a/X10D.Tests/src/Time/UInt64Tests.cs +++ b/X10D.Tests/src/Time/UInt64Tests.cs @@ -7,6 +7,18 @@ namespace X10D.Tests.Time; [CLSCompliant(false)] public class UInt64Tests { + [TestMethod] + public void FromUnixTimeMilliseconds_ShouldBeEpoch_GivenZero() + { + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), 0UL.FromUnixTimeMilliseconds()); + } + + [TestMethod] + public void FromUnixTimeSeconds_ShouldBeEpoch_GivenZero() + { + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), 0UL.FromUnixTimeSeconds()); + } + [TestMethod] public void IsLeapYear_ShouldBeFalse_GivenMultipleOf100() { diff --git a/X10D/src/Time/UInt16Extensions.cs b/X10D/src/Time/UInt16Extensions.cs index 184de79..b0ee2c9 100644 --- a/X10D/src/Time/UInt16Extensions.cs +++ b/X10D/src/Time/UInt16Extensions.cs @@ -9,6 +9,48 @@ namespace X10D.Time; [CLSCompliant(false)] public static class UInt16Extensions { + /// + /// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a + /// value. + /// + /// + /// A Unix time, expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z (January 1, + /// 1970, at 12:00 AM UTC). For Unix times before this date, its value is negative. + /// + /// A date and time value that represents the same moment in time as the Unix time. + /// + /// is less than -62,135,596,800,000. + /// -or- + /// is greater than 253,402,300,799,999. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static DateTimeOffset FromUnixTimeMilliseconds(this ushort value) + { + return DateTimeOffset.FromUnixTimeMilliseconds(value); + } + + /// + /// Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a + /// value. + /// + /// + /// A Unix time, expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at + /// 12:00 AM UTC). For Unix times before this date, its value is negative. + /// + /// A date and time value that represents the same moment in time as the Unix time. + /// + /// is less than -62,135,596,800. + /// -or- + /// is greater than 253,402,300,799. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static DateTimeOffset FromUnixTimeSeconds(this ushort value) + { + return DateTimeOffset.FromUnixTimeSeconds(value); + } + /// /// Returns a value indicating whether the current integer, representing a year, is a leap year. /// diff --git a/X10D/src/Time/UInt32Extensions.cs b/X10D/src/Time/UInt32Extensions.cs index 7d16bbe..9a4b09f 100644 --- a/X10D/src/Time/UInt32Extensions.cs +++ b/X10D/src/Time/UInt32Extensions.cs @@ -9,6 +9,48 @@ namespace X10D.Time; [CLSCompliant(false)] public static class UInt32Extensions { + /// + /// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a + /// value. + /// + /// + /// A Unix time, expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z (January 1, + /// 1970, at 12:00 AM UTC). For Unix times before this date, its value is negative. + /// + /// A date and time value that represents the same moment in time as the Unix time. + /// + /// is less than -62,135,596,800,000. + /// -or- + /// is greater than 253,402,300,799,999. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static DateTimeOffset FromUnixTimeMilliseconds(this uint value) + { + return DateTimeOffset.FromUnixTimeMilliseconds(value); + } + + /// + /// Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a + /// value. + /// + /// + /// A Unix time, expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at + /// 12:00 AM UTC). For Unix times before this date, its value is negative. + /// + /// A date and time value that represents the same moment in time as the Unix time. + /// + /// is less than -62,135,596,800. + /// -or- + /// is greater than 253,402,300,799. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static DateTimeOffset FromUnixTimeSeconds(this uint value) + { + return DateTimeOffset.FromUnixTimeSeconds(value); + } + /// /// Returns a value indicating whether the current integer, representing a year, is a leap year. /// diff --git a/X10D/src/Time/UInt64Extensions.cs b/X10D/src/Time/UInt64Extensions.cs index d88ddb2..2dfb049 100644 --- a/X10D/src/Time/UInt64Extensions.cs +++ b/X10D/src/Time/UInt64Extensions.cs @@ -9,6 +9,48 @@ namespace X10D.Time; [CLSCompliant(false)] public static class UInt64Extensions { + /// + /// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a + /// value. + /// + /// + /// A Unix time, expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z (January 1, + /// 1970, at 12:00 AM UTC). For Unix times before this date, its value is negative. + /// + /// A date and time value that represents the same moment in time as the Unix time. + /// + /// is less than -62,135,596,800,000. + /// -or- + /// is greater than 253,402,300,799,999. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static DateTimeOffset FromUnixTimeMilliseconds(this ulong value) + { + return DateTimeOffset.FromUnixTimeMilliseconds((long)value); + } + + /// + /// Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a + /// value. + /// + /// + /// A Unix time, expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z (January 1, 1970, at + /// 12:00 AM UTC). For Unix times before this date, its value is negative. + /// + /// A date and time value that represents the same moment in time as the Unix time. + /// + /// is less than -62,135,596,800. + /// -or- + /// is greater than 253,402,300,799. + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] + public static DateTimeOffset FromUnixTimeSeconds(this ulong value) + { + return DateTimeOffset.FromUnixTimeSeconds((long)value); + } + /// /// Returns a value indicating whether the current integer, representing a year, is a leap year. ///