Add FromUnixTime(Milli)Seconds for unsigned types

This commit is contained in:
Oliver Booth 2022-04-29 23:21:00 +01:00
parent cacdf148d5
commit 155a604812
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
6 changed files with 162 additions and 0 deletions

View File

@ -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()
{

View File

@ -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()
{

View File

@ -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()
{

View File

@ -9,6 +9,48 @@ namespace X10D.Time;
[CLSCompliant(false)]
public static class UInt16Extensions
{
/// <summary>
/// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="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.
/// </param>
/// <returns>A date and time value that represents the same moment in time as the Unix time.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="value" /> is less than -62,135,596,800,000.</para>
/// -or-
/// <para><paramref name="value" /> is greater than 253,402,300,799,999.</para>
/// </exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static DateTimeOffset FromUnixTimeMilliseconds(this ushort value)
{
return DateTimeOffset.FromUnixTimeMilliseconds(value);
}
/// <summary>
/// Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="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.
/// </param>
/// <returns>A date and time value that represents the same moment in time as the Unix time.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="value" /> is less than -62,135,596,800.</para>
/// -or-
/// <para><paramref name="value" /> is greater than 253,402,300,799.</para>
/// </exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static DateTimeOffset FromUnixTimeSeconds(this ushort value)
{
return DateTimeOffset.FromUnixTimeSeconds(value);
}
/// <summary>
/// Returns a value indicating whether the current integer, representing a year, is a leap year.
/// </summary>

View File

@ -9,6 +9,48 @@ namespace X10D.Time;
[CLSCompliant(false)]
public static class UInt32Extensions
{
/// <summary>
/// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="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.
/// </param>
/// <returns>A date and time value that represents the same moment in time as the Unix time.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="value" /> is less than -62,135,596,800,000.</para>
/// -or-
/// <para><paramref name="value" /> is greater than 253,402,300,799,999.</para>
/// </exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static DateTimeOffset FromUnixTimeMilliseconds(this uint value)
{
return DateTimeOffset.FromUnixTimeMilliseconds(value);
}
/// <summary>
/// Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="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.
/// </param>
/// <returns>A date and time value that represents the same moment in time as the Unix time.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="value" /> is less than -62,135,596,800.</para>
/// -or-
/// <para><paramref name="value" /> is greater than 253,402,300,799.</para>
/// </exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static DateTimeOffset FromUnixTimeSeconds(this uint value)
{
return DateTimeOffset.FromUnixTimeSeconds(value);
}
/// <summary>
/// Returns a value indicating whether the current integer, representing a year, is a leap year.
/// </summary>

View File

@ -9,6 +9,48 @@ namespace X10D.Time;
[CLSCompliant(false)]
public static class UInt64Extensions
{
/// <summary>
/// Converts a Unix time expressed as the number of milliseconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="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.
/// </param>
/// <returns>A date and time value that represents the same moment in time as the Unix time.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="value" /> is less than -62,135,596,800,000.</para>
/// -or-
/// <para><paramref name="value" /> is greater than 253,402,300,799,999.</para>
/// </exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static DateTimeOffset FromUnixTimeMilliseconds(this ulong value)
{
return DateTimeOffset.FromUnixTimeMilliseconds((long)value);
}
/// <summary>
/// Converts a Unix time expressed as the number of seconds that have elapsed since 1970-01-01T00:00:00Z to a
/// <see cref="DateTimeOffset" /> value.
/// </summary>
/// <param name="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.
/// </param>
/// <returns>A date and time value that represents the same moment in time as the Unix time.</returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="value" /> is less than -62,135,596,800.</para>
/// -or-
/// <para><paramref name="value" /> is greater than 253,402,300,799.</para>
/// </exception>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static DateTimeOffset FromUnixTimeSeconds(this ulong value)
{
return DateTimeOffset.FromUnixTimeSeconds((long)value);
}
/// <summary>
/// Returns a value indicating whether the current integer, representing a year, is a leap year.
/// </summary>