Skip to content

Commit

Permalink
Removed the shared Constants class
Browse files Browse the repository at this point in the history
  • Loading branch information
dejanfajfar committed Nov 11, 2023
1 parent da25f2f commit 7c4ca59
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 59 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 11 additions & 7 deletions Epoch.net.Test/EpochTime_Ctor_Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EpochTimeValueException>(() =>
new EpochTime(Constants.MAX_VALUE_DATETIME.AddSeconds(1)));
new EpochTime(EpochTime.MAX_DATETIME.AddSeconds(1)));
Assert.ThrowsException<EpochTimeValueException>(() =>
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);

Expand Down
6 changes: 2 additions & 4 deletions Epoch.net.Test/EpochTime_Operators_Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ 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()
{
Assert.AreEqual(1000.ToEpochTime(), Valid_EpochTime + Valid_EpochTime);

// Adding over the integer range throws an Exception
Assert.ThrowsException<EpochTimeValueException>(() => Max_EpochTime + Valid_EpochTime);
Assert.ThrowsException<EpochTimeValueException>(() => EpochTime.MAX + Valid_EpochTime);
}

[TestMethod]
Expand All @@ -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<EpochTimeValueException>(() => Min_EpochTime - Valid_EpochTime);
Assert.ThrowsException<EpochTimeValueException>(() => EpochTime.MIN - Valid_EpochTime);
}
}
}
4 changes: 2 additions & 2 deletions Epoch.net.Test/IntExtensionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions Epoch.net.Test/LongEpochTime_Ctor_Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions Epoch.net.Test/LongExtensionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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<EpochTimeValueException>(() => ((long)Constants.MIN_VALUE_INT * 1000 - 1000).ToEpochTimestamp());
Assert.ThrowsException<EpochTimeValueException>(() => ((long)EpochTime.MIN_VALUE * 1000 - 1000).ToEpochTimestamp());
}
}
}
14 changes: 0 additions & 14 deletions Epoch.net/Constants.cs

This file was deleted.

6 changes: 3 additions & 3 deletions Epoch.net/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class DateTimeExtensions
/// <remarks>The LongEpochTimestamp is the number of milliseconds since 1970-01-01T00:00Z</remarks>
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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -76,6 +76,6 @@ public static LongEpochTime ToLongEpochTime(this DateTime dateTime)
/// <returns>True if the <see cref="DateTime"/> is in a valid <see cref="EpochTime"/> range, False if not</returns>
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;
}
}
54 changes: 52 additions & 2 deletions Epoch.net/EpochTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,39 @@ static EpochTime()
TimeProvider = new DefaultTimeProvider();
}

/// <summary>
/// Provides a new instance of a <see cref="EpochTime"/> with the maximal possible value
/// </summary>
public static EpochTime MAX => new EpochTime(MAX_VALUE);
/// <summary>
/// Provides a new instance of a <see cref="EpochTime"/> with the minimal possible value
/// </summary>
public static EpochTime MIN => new EpochTime(MIN_VALUE);
/// <summary>
/// The minimal raw epoch value
/// </summary>
public const int MIN_VALUE = -2147483648;
/// <summary>
/// The maximal raw epoch value
/// </summary>
public const int MAX_VALUE = 2147483647;
/// <summary>
/// The maximal <see cref="DateTime"/> value that a <see cref="EpochTime"/> can take
/// </summary>
public static readonly DateTime MAX_DATETIME = new(2038, 1, 19,3,14,07, DateTimeKind.Utc); // 3:14:07 Tuesday, 19 January 2038 UTC
/// <summary>
/// The minimal <see cref="DateTime"/> value that a <see cref="EpochTime"/> can take
/// </summary>
public static readonly DateTime MIN_DATETIME = new(1901, 12, 13, 20, 45, 52, DateTimeKind.Utc); // 20:45:52 Friday, 13 December 1901 UTC

/// <summary>
/// The default value of <see cref="LongEpochTime"/>
/// </summary>
/// <remarks>
/// Thursday, January 1, 1970 12:00:00 AM GMT
/// </remarks>
public static EpochTime Default => new(0);

private static IDateTimeProvider TimeProvider { get; set; }

#region Constructors
Expand Down Expand Up @@ -164,23 +197,40 @@ public EpochTime Add(TimeSpan span)

#region Operators

/// <summary>
/// Implements the + operator between two <see cref="EpochTime"/> instances
/// </summary>
/// <param name="operand1">The augend</param>
/// <param name="operand2">The addend</param>
/// <returns>The sum of the two <see cref="EpochTime"/></returns>
/// <exception cref="EpochTimeValueException">
/// If the result is not a valid <see cref="EpochTime"/>
/// </exception>
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);
}

return new EpochTime(Convert.ToInt32(epochSum));
}

/// <summary>
/// Implements the - operator between two <see cref="EpochTime"/> instances
/// </summary>
/// <param name="operand1">The minuend</param>
/// <param name="operand2">The subtrahend</param>
/// <returns>The Difference between the two <see cref="EpochTime"/> instances</returns>
/// <exception cref="EpochTimeValueException">
/// If the result is not a valid <see cref="EpochTime"/></exception>
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);
}
Expand Down
50 changes: 39 additions & 11 deletions Epoch.net/EpochTimeValueException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,57 @@

namespace Epoch.net;

/// <summary>
/// Denotes the error state of a <seealso cref="EpochTime"/>
/// </summary>
public class EpochTimeValueException : Exception
{
private const string DefaultErrorMessage = "The provided value could not be represented in a Epoch";
private const string DateTimeErrorMessage = "The DateTime {0} is not in a Epoch range";
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";


/// <summary>
/// Creates a new instance of the <see cref="EpochTimeValueException"/>
/// </summary>
public EpochTimeValueException()
: base(DefaultErrorMessage){}

: base(DefaultErrorMessage) { }

/// <summary>
/// Creates a new instance of the <see cref="EpochTimeValueException"/> with the given error message
/// </summary>
/// <param name="message">The Error message to assigne to the new instance of the <see cref="EpochTimeValueException"/></param>
public EpochTimeValueException(string message)
:base(message) {}

: base(message) { }

/// <summary>
/// Creates a new instance of the <see cref="EpochTimeValueException"/> with a given <see cref="DateTime"/>
/// </summary>
/// <param name="value">The <see cref="DateTime"/> that is not in a valid Epoch range</param>
/// <remarks>
/// It is assumed that the provided <see cref="DateTime"/> is not in a valid Epoch range.
/// </remarks>
public EpochTimeValueException(DateTime value)
:base(string.Format(DateTimeErrorMessage, value)){}
: base(string.Format(DateTimeErrorMessage, value)) { }

/// <summary>
/// Creates a new instance of the <see cref="EpochTimeValueException"/> with a given <see cref="TimeSpan"/>
/// </summary>
/// <param name="value">The <see cref="TimeSpan"/> that is not in a valid Epoch range</param>
/// <remarks>
/// It is assumed that the <see cref="TimeSpan"/> is not in a valid Epoch range
/// </remarks>
public EpochTimeValueException(TimeSpan value)
:base(string.Format(TimeStampErrorMessage, value)){}

public EpochTimeValueException(EpochTime value)
:base(string.Format(EpochTimeErrorMessage, value)){}
: base(string.Format(TimeStampErrorMessage, value)) { }

/// <summary>
/// Creates a new instance of the <see cref="EpochTimeValueException"/> with a given <see cref="long"/> value
/// </summary>
/// <param name="value">The <see cref="long"/> that is not in a valid Epoch range</param>
/// <remarks>
/// It is assumed that the <see cref="long"/> value is not in a valid Epoch range
/// </remarks>
public EpochTimeValueException(long value)
:base(string.Format(LongErrorMessage, value)){}
: base(string.Format(LongErrorMessage, value)) { }
}
2 changes: 1 addition & 1 deletion Epoch.net/IntExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class IntExtensions
/// <returns>A <see cref="DateTime"/> initialized to the given offset in seconds from 1970-01-01T00:00Z</returns>
public static DateTime ToDateTime(this int epoch)
{
return Constants.UnixEpoch.AddSeconds(epoch);
return LongEpochTime.DefaultDateTime.AddSeconds(epoch);
}

/// <summary>
Expand Down
14 changes: 14 additions & 0 deletions Epoch.net/LongEpochTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,22 @@ public sealed class LongEpochTime

static LongEpochTime() => timeProvider = new DefaultTimeProvider();

/// <summary>
/// The default value of <see cref="LongEpochTime"/>
/// </summary>
/// <remarks>
/// Thursday, January 1, 1970 12:00:00 AM GMT
/// </remarks>
public static LongEpochTime Default => new(0);

public static DateTime DefaultDateTime => new(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

Check warning on line 22 in Epoch.net/LongEpochTime.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'LongEpochTime.DefaultDateTime'

public const long MAX_VALUE = 922337203685477;

Check warning on line 24 in Epoch.net/LongEpochTime.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'LongEpochTime.MAX_VALUE'
public const long MIN_VALUE = -922337203685477;

Check warning on line 25 in Epoch.net/LongEpochTime.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'LongEpochTime.MIN_VALUE'

public static LongEpochTime MIN => new LongEpochTime(MIN_VALUE);

Check warning on line 27 in Epoch.net/LongEpochTime.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'LongEpochTime.MIN'
public static LongEpochTime MAX => new LongEpochTime(MAX_VALUE);

Check warning on line 28 in Epoch.net/LongEpochTime.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'LongEpochTime.MAX'

/// <summary>
/// Initializes a new instance of <see cref="LongEpochTime"/>
/// </summary>
Expand Down
Loading

0 comments on commit 7c4ca59

Please sign in to comment.