Skip to content

Commit

Permalink
Improved the Unix time logic in the "EssentialsTime" class
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
abjerner committed Feb 14, 2022
1 parent a6837d6 commit 32860ca
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
49 changes: 46 additions & 3 deletions src/Skybrud.Essentials/Time/EssentialsTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public class EssentialsTime : IComparable, IComparable<EssentialsTime>, ICompara
/// Gets an instance of <see cref="EssentialsTime"/> representing the start of the Unix Epoch (AKA <c>0</c> seconds).
/// </summary>
public static EssentialsTime Zero => FromUnixTimeSeconds(0);

/// <summary>
/// Returns the amount of seconds since the start of the Unix epoch - that is <c>1st of January, 1970 - 00:00:00 GMT</c>.
/// </summary>
public static long CurrentUnixTimeSeconds => (long) UnixTimeUtils.CurrentSeconds;

/// <summary>
/// Returns the amount of milliseconds since the start of the Unix epoch - that is <c>1st of January, 1970 - 00:00:00 GMT</c>.
/// </summary>
public static long CurrentUnixTimeMilliseconds => (long) UnixTimeUtils.CurrentMilliseconds;

#endregion

Expand Down Expand Up @@ -137,8 +147,19 @@ public class EssentialsTime : IComparable, IComparable<EssentialsTime>, ICompara
/// <summary>
/// Gets the UNIX timestamp (amount of seconds since the start of the Unix Epoch) for this <see cref="EssentialsTime"/>.
/// </summary>
[Obsolete("Use 'UnixTimeSeconds' instead.")]
public long UnixTimestamp => (long) UnixTimeUtils.ToSeconds(DateTimeOffset);

/// <summary>
/// Gets the UNIX timestamp in seconds (amount of time the start of the Unix Epoch) for this <see cref="EssentialsTime"/>.
/// </summary>
public long UnixTimeSeconds => (long) UnixTimeUtils.ToSeconds(DateTimeOffset);

/// <summary>
/// Gets the UNIX timestamp in milliseconds (amount of time the start of the Unix Epoch) for this <see cref="EssentialsTime"/>.
/// </summary>
public long UnixTimeMilliseconds => (long) UnixTimeUtils.ToMilliseconds(DateTimeOffset);

/// <summary>
/// Gets the time's offset from Coordinated Universal Time (UTC).
/// </summary>
Expand Down Expand Up @@ -177,17 +198,17 @@ public class EssentialsTime : IComparable, IComparable<EssentialsTime>, ICompara
/// <summary>
/// Gets whether the Unix timestamp of this <see cref="EssentialsTime"/> is <c>0</c>.
/// </summary>
public bool IsZero => UnixTimestamp == 0;
public bool IsZero => UnixTimeSeconds == 0;

/// <summary>
/// Gets whether the Unix timestamp of this <see cref="EssentialsTime"/> is less than <c>0</c>.
/// </summary>
public bool IsNegative => UnixTimestamp < 0;
public bool IsNegative => UnixTimeSeconds < 0;

/// <summary>
/// Gets whether the Unix timestamp of this <see cref="EssentialsTime"/> is greater than <c>0</c>.
/// </summary>
public bool IsPositive => UnixTimestamp > 0;
public bool IsPositive => UnixTimeSeconds > 0;

/// <summary>
/// Gets whether the date of this <see cref="EssentialsTime"/> matches the current day.
Expand Down Expand Up @@ -705,6 +726,28 @@ public EssentialsTime ToTimeZone(TimeZoneInfo timeZone) {
return new EssentialsTime(DateTimeOffset, timeZone);
}

/// <summary>
/// Returns the number of milliseconds that have elapsed since the start of the Unix epoch (<c>1st of January, 1970 - 00:00:00 GMT</c>).
/// </summary>
/// <returns>The number of milliseconds that have elapsed since the start of the Unix epoch (<c>1st of January, 1970 - 00:00:00 GMT</c>).</returns>
/// <seealso>
/// <cref>https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tounixtimeseconds?view=netstandard-2.0</cref>
/// </seealso>
public long ToUnixTimeSeconds() {
return (long) UnixTimeUtils.ToSeconds(DateTimeOffset);
}

/// <summary>
/// Returns the number of milliseconds that have elapsed since the start of the Unix epoch (<c>1st of January, 1970 - 00:00:00 GMT</c>).
/// </summary>
/// <returns>The number of milliseconds that have elapsed since the start of the Unix epoch (<c>1st of January, 1970 - 00:00:00 GMT</c>).</returns>
/// <seealso>
/// <cref>https://docs.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tounixtimemilliseconds?view=netstandard-2.0</cref>
/// </seealso>
public long ToUnixTimeMilliseconds() {
return (long) UnixTimeUtils.ToMilliseconds(DateTimeOffset);
}

/// <summary>
/// Gets a new instance of <see cref="EssentialsTime"/> representing the start of the day.
///
Expand Down
41 changes: 41 additions & 0 deletions src/UnitTestProject1/Time/UnixTime/UnixTimeTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");

}

}

Expand Down

0 comments on commit 32860ca

Please sign in to comment.