From 7c4ca59364b1a805354a12a26e7b8dacabb906fb Mon Sep 17 00:00:00 2001 From: Dejan FAJFAR Date: Sat, 11 Nov 2023 21:01:15 +0100 Subject: [PATCH] Removed the shared Constants class --- CHANGELOG.md | 8 ++- Epoch.net.Test/EpochTime_Ctor_Fixture.cs | 18 ++++--- Epoch.net.Test/EpochTime_Operators_Fixture.cs | 6 +-- Epoch.net.Test/IntExtensionsFixture.cs | 4 +- Epoch.net.Test/LongEpochTime_Ctor_Fixture.cs | 8 +-- Epoch.net.Test/LongExtensionsFixture.cs | 8 +-- Epoch.net/Constants.cs | 14 ----- Epoch.net/DateTimeExtensions.cs | 6 +-- Epoch.net/EpochTime.cs | 54 ++++++++++++++++++- Epoch.net/EpochTimeValueException.cs | 50 +++++++++++++---- Epoch.net/IntExtensions.cs | 2 +- Epoch.net/LongEpochTime.cs | 14 +++++ Epoch.net/LongEpochTimeValueException.cs | 10 ++++ Epoch.net/LongExtensions.cs | 6 +-- Epoch.net/TimeSpanExtensions.cs | 4 +- 15 files changed, 153 insertions(+), 59 deletions(-) delete mode 100644 Epoch.net/Constants.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 86ba106..2f2046c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,18 +4,22 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). -## [3.2] +## [4.0] ### Changed - Updated to .net version 7 - Moved build to github actions -- +- Updated code formatting to new standard ### Added - DateOnly extensions - TimeOnly extensions +### Removed + +- The Constants static class + ## [3.1] ### Added diff --git a/Epoch.net.Test/EpochTime_Ctor_Fixture.cs b/Epoch.net.Test/EpochTime_Ctor_Fixture.cs index 6053efc..89c245a 100644 --- a/Epoch.net.Test/EpochTime_Ctor_Fixture.cs +++ b/Epoch.net.Test/EpochTime_Ctor_Fixture.cs @@ -15,26 +15,30 @@ public void Int() { Assert.AreEqual(0, new EpochTime(0).Epoch); - Assert.AreEqual(Constants.MAX_VALUE_INT, new EpochTime(Constants.MAX_VALUE_INT).Epoch); - Assert.AreEqual(Constants.MIN_VALUE_INT, new EpochTime(Constants.MIN_VALUE_INT).Epoch); + Assert.AreEqual(EpochTime.MAX_VALUE, EpochTime.MAX.Epoch); + Assert.AreEqual(EpochTime.MIN_VALUE, EpochTime.MIN.Epoch); + + Assert.AreEqual(EpochTime.MIN, new EpochTime(EpochTime.MIN_VALUE)); + Assert.AreEqual(EpochTime.MAX, new EpochTime(EpochTime.MAX_VALUE)); } [TestMethod] public void DateTime() { Assert.AreEqual(0, new EpochTime(0).Epoch); + Assert.AreEqual(0, EpochTime.Default.Epoch); Assert.AreEqual(ValidEpochTimestamp, new EpochTime(ValidDateTime).Epoch); - Assert.AreEqual(Constants.MIN_VALUE_INT, new EpochTime(Constants.MIN_VALUE_DATETIME).Epoch); - Assert.AreEqual(Constants.MAX_VALUE_INT, new EpochTime(Constants.MAX_VALUE_DATETIME).Epoch); + Assert.AreEqual(EpochTime.MIN, new EpochTime(EpochTime.MIN_DATETIME)); + Assert.AreEqual(EpochTime.MAX, new EpochTime(EpochTime.MAX_DATETIME)); Assert.ThrowsException(() => - new EpochTime(Constants.MAX_VALUE_DATETIME.AddSeconds(1))); + new EpochTime(EpochTime.MAX_DATETIME.AddSeconds(1))); Assert.ThrowsException(() => - new EpochTime(Constants.MIN_VALUE_DATETIME.AddSeconds(-1))); + new EpochTime(EpochTime.MIN_DATETIME.AddSeconds(-1))); } [TestMethod] - public void EpochTime() + public void EpochTime_Test() { Assert.AreEqual(ValidEpochTimestamp, new EpochTime(ValidEpochTime).Epoch); diff --git a/Epoch.net.Test/EpochTime_Operators_Fixture.cs b/Epoch.net.Test/EpochTime_Operators_Fixture.cs index be28e53..000dddb 100644 --- a/Epoch.net.Test/EpochTime_Operators_Fixture.cs +++ b/Epoch.net.Test/EpochTime_Operators_Fixture.cs @@ -7,8 +7,6 @@ namespace Epoch.net.Test public class EpochTime_Operators_Fixture { private readonly EpochTime Valid_EpochTime = new EpochTime(500); - private readonly EpochTime Max_EpochTime = new EpochTime(Constants.MAX_VALUE_INT); - private readonly EpochTime Min_EpochTime = new EpochTime(Constants.MIN_VALUE_INT); [TestMethod] public void Addition() @@ -16,7 +14,7 @@ public void Addition() Assert.AreEqual(1000.ToEpochTime(), Valid_EpochTime + Valid_EpochTime); // Adding over the integer range throws an Exception - Assert.ThrowsException(() => Max_EpochTime + Valid_EpochTime); + Assert.ThrowsException(() => EpochTime.MAX + Valid_EpochTime); } [TestMethod] @@ -25,7 +23,7 @@ public void Substration() Assert.AreEqual(Valid_EpochTime, 1000.ToEpochTime() - Valid_EpochTime); // Subtracting over the integer range throws an Exception - Assert.ThrowsException(() => Min_EpochTime - Valid_EpochTime); + Assert.ThrowsException(() => EpochTime.MIN - Valid_EpochTime); } } } \ No newline at end of file diff --git a/Epoch.net.Test/IntExtensionsFixture.cs b/Epoch.net.Test/IntExtensionsFixture.cs index 98c433f..3a5702a 100644 --- a/Epoch.net.Test/IntExtensionsFixture.cs +++ b/Epoch.net.Test/IntExtensionsFixture.cs @@ -19,8 +19,8 @@ public void ToDateTime() Assert.AreEqual(After_1970_DateTime, After_1970_EpochTimestamp.ToDateTime()); Assert.AreEqual(Before_1970_DateTime, Before_1970_EpochTimestamp.ToDateTime()); - Assert.AreEqual(Constants.MAX_VALUE_DATETIME, Constants.MAX_VALUE_INT.ToDateTime()); - Assert.AreEqual(Constants.MIN_VALUE_DATETIME, Constants.MIN_VALUE_INT.ToDateTime()); + Assert.AreEqual(EpochTime.MAX_DATETIME, EpochTime.MAX_VALUE.ToDateTime()); + Assert.AreEqual(EpochTime.MIN_DATETIME, EpochTime.MIN_VALUE.ToDateTime()); } [TestMethod] diff --git a/Epoch.net.Test/LongEpochTime_Ctor_Fixture.cs b/Epoch.net.Test/LongEpochTime_Ctor_Fixture.cs index 6f56739..171c900 100644 --- a/Epoch.net.Test/LongEpochTime_Ctor_Fixture.cs +++ b/Epoch.net.Test/LongEpochTime_Ctor_Fixture.cs @@ -26,20 +26,20 @@ public void TimeSpan() Assert.AreEqual(ValidLongEpochTimestamp, new LongEpochTime(System.TimeSpan.FromMilliseconds(ValidLongEpochTimestamp)).Epoch); - Assert.AreEqual(Constants.MAX_VALUE_LONG, new LongEpochTime(System.TimeSpan.MaxValue).Epoch); - Assert.AreEqual(Constants.MIN_VALUE_LONG, new LongEpochTime(System.TimeSpan.MinValue).Epoch); + Assert.AreEqual(LongEpochTime.MAX, new LongEpochTime(System.TimeSpan.MaxValue)); + Assert.AreEqual(LongEpochTime.MIN, new LongEpochTime(System.TimeSpan.MinValue)); } [TestMethod] public void DateTime() { - Assert.AreEqual(0L, new LongEpochTime(Constants.UnixEpoch).Epoch); + Assert.AreEqual(0L, LongEpochTime.Default.Epoch); Assert.AreEqual(ValidLongEpochTimestamp, new LongEpochTime(ValidDateTime).Epoch); } [TestMethod] - public void LongEpochTime() + public void LongEpochTimeTest() { Assert.AreEqual(ValidLongEpochTimestamp, new LongEpochTime(ValidLongEpochTime).Epoch); } diff --git a/Epoch.net.Test/LongExtensionsFixture.cs b/Epoch.net.Test/LongExtensionsFixture.cs index ff78712..efdb0b6 100644 --- a/Epoch.net.Test/LongExtensionsFixture.cs +++ b/Epoch.net.Test/LongExtensionsFixture.cs @@ -26,7 +26,7 @@ public void IsValidEpochTimestamp() [TestMethod] public void ToDateTime() { - Assert.AreEqual(Constants.UnixEpoch, 0L.ToDateTime()); + Assert.AreEqual(LongEpochTime.DefaultDateTime, 0L.ToDateTime()); Assert.AreEqual(PositiveDateTime, PositiveLongEpochTimestamp.ToDateTime()); Assert.AreEqual(NegativeDateTime, NegativeLongEpochTimestamp.ToDateTime()); @@ -46,12 +46,12 @@ public void ToEpochTimestamp() { Assert.AreEqual(0, 0L.ToEpochTimestamp()); - Assert.AreEqual(Constants.MAX_VALUE_INT, ((long)Constants.MAX_VALUE_INT * 1000).ToEpochTimestamp()); - Assert.AreEqual(Constants.MIN_VALUE_INT, ((long)Constants.MIN_VALUE_INT * 1000).ToEpochTimestamp()); + Assert.AreEqual(EpochTime.MAX_VALUE, ((long) EpochTime.MAX_VALUE * 1000).ToEpochTimestamp()); + Assert.AreEqual(EpochTime.MIN_VALUE, ((long)EpochTime.MIN_VALUE * 1000).ToEpochTimestamp()); Assert.AreEqual(PositiveDateTime.ToEpochTimestamp(), PositiveLongEpochTimestamp.ToEpochTimestamp()); - Assert.ThrowsException(() => ((long)Constants.MIN_VALUE_INT * 1000 - 1000).ToEpochTimestamp()); + Assert.ThrowsException(() => ((long)EpochTime.MIN_VALUE * 1000 - 1000).ToEpochTimestamp()); } } } \ No newline at end of file diff --git a/Epoch.net/Constants.cs b/Epoch.net/Constants.cs deleted file mode 100644 index a3287d1..0000000 --- a/Epoch.net/Constants.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; - -namespace Epoch.net; - -public static class Constants -{ - public static readonly DateTime UnixEpoch = new(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); - public const int MIN_VALUE_INT = -2147483648; - public const int MAX_VALUE_INT = 2147483647; - public static readonly DateTime MAX_VALUE_DATETIME = new(2038, 1, 19,3,14,07, DateTimeKind.Utc); // 3:14:07 Tuesday, 19 January 2038 UTC - public static readonly DateTime MIN_VALUE_DATETIME = new(1901, 12, 13, 20, 45, 52, DateTimeKind.Utc); // 20:45:52 Friday, 13 December 1901 UTC - public const long MAX_VALUE_LONG = 922337203685477; - public const long MIN_VALUE_LONG = -922337203685477; -} \ No newline at end of file diff --git a/Epoch.net/DateTimeExtensions.cs b/Epoch.net/DateTimeExtensions.cs index 998f37c..25a14f8 100644 --- a/Epoch.net/DateTimeExtensions.cs +++ b/Epoch.net/DateTimeExtensions.cs @@ -15,7 +15,7 @@ public static class DateTimeExtensions /// The LongEpochTimestamp is the number of milliseconds since 1970-01-01T00:00Z public static long ToLongEpochTimestamp(this DateTime dateTime) { - var timeSinceDisco = TimeZoneInfo.ConvertTimeToUtc(dateTime.ToUniversalTime()) - Constants.UnixEpoch; + var timeSinceDisco = TimeZoneInfo.ConvertTimeToUtc(dateTime.ToUniversalTime()) - LongEpochTime.DefaultDateTime; return Convert.ToInt64(timeSinceDisco.TotalMilliseconds); } @@ -36,7 +36,7 @@ public static int ToEpochTimestamp(this DateTime dateTime) throw new EpochTimeValueException(dateTime); } - var timeSinceDisco = TimeZoneInfo.ConvertTimeToUtc(dateTime.ToUniversalTime()) - Constants.UnixEpoch; + var timeSinceDisco = TimeZoneInfo.ConvertTimeToUtc(dateTime.ToUniversalTime()) - LongEpochTime.DefaultDateTime; return Convert.ToInt32(timeSinceDisco.TotalSeconds); } @@ -76,6 +76,6 @@ public static LongEpochTime ToLongEpochTime(this DateTime dateTime) /// True if the is in a valid range, False if not public static bool IsValidEpochTime(this DateTime dateTime) { - return dateTime >= Constants.MIN_VALUE_DATETIME && dateTime <= Constants.MAX_VALUE_DATETIME; + return dateTime >= EpochTime.MIN_DATETIME && dateTime <= EpochTime.MAX_DATETIME; } } \ No newline at end of file diff --git a/Epoch.net/EpochTime.cs b/Epoch.net/EpochTime.cs index 89967b5..ae18ddc 100644 --- a/Epoch.net/EpochTime.cs +++ b/Epoch.net/EpochTime.cs @@ -12,6 +12,39 @@ static EpochTime() TimeProvider = new DefaultTimeProvider(); } + /// + /// Provides a new instance of a with the maximal possible value + /// + public static EpochTime MAX => new EpochTime(MAX_VALUE); + /// + /// Provides a new instance of a with the minimal possible value + /// + public static EpochTime MIN => new EpochTime(MIN_VALUE); + /// + /// The minimal raw epoch value + /// + public const int MIN_VALUE = -2147483648; + /// + /// The maximal raw epoch value + /// + public const int MAX_VALUE = 2147483647; + /// + /// The maximal value that a can take + /// + public static readonly DateTime MAX_DATETIME = new(2038, 1, 19,3,14,07, DateTimeKind.Utc); // 3:14:07 Tuesday, 19 January 2038 UTC + /// + /// The minimal value that a can take + /// + public static readonly DateTime MIN_DATETIME = new(1901, 12, 13, 20, 45, 52, DateTimeKind.Utc); // 20:45:52 Friday, 13 December 1901 UTC + + /// + /// The default value of + /// + /// + /// Thursday, January 1, 1970 12:00:00 AM GMT + /// + public static EpochTime Default => new(0); + private static IDateTimeProvider TimeProvider { get; set; } #region Constructors @@ -164,11 +197,20 @@ public EpochTime Add(TimeSpan span) #region Operators + /// + /// Implements the + operator between two instances + /// + /// The augend + /// The addend + /// The sum of the two + /// + /// If the result is not a valid + /// public static EpochTime operator +(EpochTime operand1, EpochTime operand2) { var epochSum = operand1.Epoch + (long)operand2.Epoch; - if (!epochSum.IsValidEpochTimestamp()) + if (epochSum.IsValidEpochTimestamp() is false) { throw new EpochTimeValueException(epochSum); } @@ -176,11 +218,19 @@ public EpochTime Add(TimeSpan span) return new EpochTime(Convert.ToInt32(epochSum)); } + /// + /// Implements the - operator between two instances + /// + /// The minuend + /// The subtrahend + /// The Difference between the two instances + /// + /// If the result is not a valid public static EpochTime operator -(EpochTime operand1, EpochTime operand2) { var epochSub = operand1.Epoch - (long)operand2.Epoch; - if (!epochSub.IsValidEpochTimestamp()) + if (epochSub.IsValidEpochTimestamp() is false) { throw new EpochTimeValueException(epochSub); } diff --git a/Epoch.net/EpochTimeValueException.cs b/Epoch.net/EpochTimeValueException.cs index 1736a88..bfbe8ac 100644 --- a/Epoch.net/EpochTimeValueException.cs +++ b/Epoch.net/EpochTimeValueException.cs @@ -2,6 +2,9 @@ namespace Epoch.net; +/// +/// Denotes the error state of a +/// public class EpochTimeValueException : Exception { private const string DefaultErrorMessage = "The provided value could not be represented in a Epoch"; @@ -9,22 +12,47 @@ public class EpochTimeValueException : Exception private const string TimeStampErrorMessage = "The Timespan {0} is not in a Epoch range"; private const string EpochTimeErrorMessage = "The EpochTime {0} is not in a Epoch range"; private const string LongErrorMessage = "The number {0} is not in a Epoch range"; - + + /// + /// Creates a new instance of the + /// public EpochTimeValueException() - : base(DefaultErrorMessage){} - + : base(DefaultErrorMessage) { } + + /// + /// Creates a new instance of the with the given error message + /// + /// The Error message to assigne to the new instance of the public EpochTimeValueException(string message) - :base(message) {} - + : base(message) { } + + /// + /// Creates a new instance of the with a given + /// + /// The that is not in a valid Epoch range + /// + /// It is assumed that the provided is not in a valid Epoch range. + /// public EpochTimeValueException(DateTime value) - :base(string.Format(DateTimeErrorMessage, value)){} + : base(string.Format(DateTimeErrorMessage, value)) { } + /// + /// Creates a new instance of the with a given + /// + /// The that is not in a valid Epoch range + /// + /// It is assumed that the is not in a valid Epoch range + /// public EpochTimeValueException(TimeSpan value) - :base(string.Format(TimeStampErrorMessage, value)){} - - public EpochTimeValueException(EpochTime value) - :base(string.Format(EpochTimeErrorMessage, value)){} + : base(string.Format(TimeStampErrorMessage, value)) { } + /// + /// Creates a new instance of the with a given value + /// + /// The that is not in a valid Epoch range + /// + /// It is assumed that the value is not in a valid Epoch range + /// public EpochTimeValueException(long value) - :base(string.Format(LongErrorMessage, value)){} + : base(string.Format(LongErrorMessage, value)) { } } \ No newline at end of file diff --git a/Epoch.net/IntExtensions.cs b/Epoch.net/IntExtensions.cs index 09ab406..6c9884f 100644 --- a/Epoch.net/IntExtensions.cs +++ b/Epoch.net/IntExtensions.cs @@ -14,7 +14,7 @@ public static class IntExtensions /// A initialized to the given offset in seconds from 1970-01-01T00:00Z public static DateTime ToDateTime(this int epoch) { - return Constants.UnixEpoch.AddSeconds(epoch); + return LongEpochTime.DefaultDateTime.AddSeconds(epoch); } /// diff --git a/Epoch.net/LongEpochTime.cs b/Epoch.net/LongEpochTime.cs index ad4f1c5..598774d 100644 --- a/Epoch.net/LongEpochTime.cs +++ b/Epoch.net/LongEpochTime.cs @@ -11,8 +11,22 @@ public sealed class LongEpochTime static LongEpochTime() => timeProvider = new DefaultTimeProvider(); + /// + /// The default value of + /// + /// + /// Thursday, January 1, 1970 12:00:00 AM GMT + /// public static LongEpochTime Default => new(0); + public static DateTime DefaultDateTime => new(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); + + public const long MAX_VALUE = 922337203685477; + public const long MIN_VALUE = -922337203685477; + + public static LongEpochTime MIN => new LongEpochTime(MIN_VALUE); + public static LongEpochTime MAX => new LongEpochTime(MAX_VALUE); + /// /// Initializes a new instance of /// diff --git a/Epoch.net/LongEpochTimeValueException.cs b/Epoch.net/LongEpochTimeValueException.cs index af0d2de..0d3e253 100644 --- a/Epoch.net/LongEpochTimeValueException.cs +++ b/Epoch.net/LongEpochTimeValueException.cs @@ -2,10 +2,20 @@ namespace Epoch.net; +/// +/// Denotes the error state of a +/// public class LongEpochTimeValueException : Exception { private const string TimeSpanErrorMessage = "The provided TimeSpan {0} does not conform to the LongEpochTime range"; + /// + /// Creates a new instance of the with the given + /// + /// The that is not inside the valid range + /// + /// It is assumed that the is not in a valid value range + /// public LongEpochTimeValueException(TimeSpan value) :base(string.Format(TimeSpanErrorMessage, value)) { diff --git a/Epoch.net/LongExtensions.cs b/Epoch.net/LongExtensions.cs index 07a42ac..62d8d51 100644 --- a/Epoch.net/LongExtensions.cs +++ b/Epoch.net/LongExtensions.cs @@ -12,11 +12,11 @@ public static class LongExtensions /// /// The LongEpochTimestamp /// - /// True if the EpochTimestamp is in a valid EpochTime range, False if not + /// True if the EpochTimestamp is in a valid range, False if not /// public static bool IsValidEpochTimestamp(this long epoch) { - return epoch >= Constants.MIN_VALUE_INT && epoch <= Constants.MAX_VALUE_INT; + return epoch >= EpochTime.MIN_VALUE && epoch <= EpochTime.MAX_VALUE; } /// @@ -26,7 +26,7 @@ public static bool IsValidEpochTimestamp(this long epoch) /// A representation of the given LongEpochTimestamp public static DateTime ToDateTime(this long value) { - return Constants.UnixEpoch.AddMilliseconds(value); + return LongEpochTime.DefaultDateTime.AddMilliseconds(value); } /// diff --git a/Epoch.net/TimeSpanExtensions.cs b/Epoch.net/TimeSpanExtensions.cs index 3317c62..b9e62b9 100644 --- a/Epoch.net/TimeSpanExtensions.cs +++ b/Epoch.net/TimeSpanExtensions.cs @@ -78,7 +78,7 @@ public static bool IsValidEpochTime(this TimeSpan timeSpan) { var totalSeconds = timeSpan.TotalSeconds; - return totalSeconds >= Constants.MIN_VALUE_INT && totalSeconds <= Constants.MAX_VALUE_INT; + return totalSeconds >= EpochTime.MIN_VALUE && totalSeconds <= EpochTime.MAX_VALUE; } /// @@ -92,6 +92,6 @@ public static bool IsValidLongEpochTime(this TimeSpan timeSpan) { var totalMilliseconds = timeSpan.TotalMilliseconds; - return totalMilliseconds >= Constants.MIN_VALUE_LONG && totalMilliseconds <= Constants.MAX_VALUE_LONG; + return totalMilliseconds >= LongEpochTime.MIN_VALUE && totalMilliseconds <= LongEpochTime.MAX_VALUE; } } \ No newline at end of file