summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Utilities/SDate.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/StardewModdingAPI/Utilities/SDate.cs')
-rw-r--r--src/StardewModdingAPI/Utilities/SDate.cs180
1 files changed, 56 insertions, 124 deletions
diff --git a/src/StardewModdingAPI/Utilities/SDate.cs b/src/StardewModdingAPI/Utilities/SDate.cs
index fdeffe80..5f7ff030 100644
--- a/src/StardewModdingAPI/Utilities/SDate.cs
+++ b/src/StardewModdingAPI/Utilities/SDate.cs
@@ -13,6 +13,9 @@ namespace StardewModdingAPI.Utilities
/// <summary>The internal season names in order.</summary>
private readonly string[] Seasons = { "spring", "summer", "fall", "winter" };
+ /// <summary>The number of seasons in a year.</summary>
+ private int SeasonsInYear => this.Seasons.Length;
+
/// <summary>The number of days in a season.</summary>
private readonly int DaysInSeason = 28;
@@ -77,10 +80,8 @@ namespace StardewModdingAPI.Utilities
// handle season transition
if (day > this.DaysInSeason || day < 1)
{
- // get current season index
- int curSeasonIndex = Array.IndexOf(this.Seasons, this.Season);
- if (curSeasonIndex == -1)
- throw new InvalidOperationException($"The current season '{this.Season}' wasn't recognised.");
+ // get season index
+ int curSeasonIndex = this.GetSeasonIndex();
// get season offset
int seasonOffset = day / this.DaysInSeason;
@@ -94,7 +95,7 @@ namespace StardewModdingAPI.Utilities
}
// validate
- if(year < 1)
+ if (year < 1)
throw new ArithmeticException($"Adding {offset} days to {this} would result in invalid date {day:00} {season} {year}.");
// return new date
@@ -116,157 +117,88 @@ namespace StardewModdingAPI.Utilities
/*********
** Operator methods
*********/
-
- /// <summary>
- /// Equality operator. Tests the date being equal to each other
- /// </summary>
- /// <param name="s1">The first date being compared</param>
- /// <param name="s2">The second date being compared</param>
+ /// <summary>Get whether one date is equal to another.</summary>
+ /// <param name="date">The base date to compare.</param>
+ /// <param name="other">The other date to compare.</param>
/// <returns>The equality of the dates</returns>
- public static bool operator ==(SDate s1, SDate s2)
+ public static bool operator ==(SDate date, SDate other)
{
- if (s1.Day == s2.Day && s1.Year == s2.Year && s1.Season == s2.Season)
- return true;
- else
- return false;
+ return date?.GetHashCode() == other?.GetHashCode();
}
- /// <summary>
- /// Inequality operator. Tests the date being not equal to each other
- /// </summary>
- /// <param name="s1">The first date being compared</param>
- /// <param name="s2">The second date being compared</param>
- /// <returns>The inequality of the dates</returns>
- public static bool operator !=(SDate s1, SDate s2)
+ /// <summary>Get whether one date is not equal to another.</summary>
+ /// <param name="date">The base date to compare.</param>
+ /// <param name="other">The other date to compare.</param>
+ public static bool operator !=(SDate date, SDate other)
{
- if (s1.Day == s2.Day && s1.Year == s2.Year && s1.Season == s2.Season)
- return false;
- else
- return true;
+ return date?.GetHashCode() != other?.GetHashCode();
}
- /// <summary>
- /// Less than operator. Tests the date being less than to each other
- /// </summary>
- /// <param name="s1">The first date being compared</param>
- /// <param name="s2">The second date being compared</param>
- /// <returns>If the dates are less than</returns>
- public static bool operator >(SDate s1, SDate s2)
+ /// <summary>Get whether one date is more than another.</summary>
+ /// <param name="date">The base date to compare.</param>
+ /// <param name="other">The other date to compare.</param>
+ public static bool operator >(SDate date, SDate other)
{
- if (s1.Year > s2.Year)
- return true;
- else if (s1.Year == s2.Year)
- {
- if (s1.Season == "winter" && s2.Season != "winter")
- return true;
- else if (s1.Season == s2.Season && s1.Day > s2.Day)
- return true;
- if (s1.Season == "fall" && (s2.Season == "summer" || s2.Season == "spring"))
- return true;
- if (s1.Season == "summer" && s2.Season == "spring")
- return true;
- }
-
- return false;
+ return date?.GetHashCode() > other?.GetHashCode();
}
- /// <summary>
- /// Less or equal than operator. Tests the date being less than or equal to each other
- /// </summary>
- /// <param name="s1">The first date being compared</param>
- /// <param name="s2">The second date being compared</param>
- /// <returns>If the dates are less than or equal than</returns>
- public static bool operator >=(SDate s1, SDate s2)
+ /// <summary>Get whether one date is more than or equal to another.</summary>
+ /// <param name="date">The base date to compare.</param>
+ /// <param name="other">The other date to compare.</param>
+ public static bool operator >=(SDate date, SDate other)
{
- if (s1.Year > s2.Year)
- return true;
- else if (s1.Year == s2.Year)
- {
- if (s1.Season == "winter" && s2.Season != "winter")
- return true;
- else if (s1.Season == s2.Season && s1.Day >= s2.Day)
- return true;
- if (s1.Season == "fall" && (s2.Season == "summer" || s2.Season == "spring"))
- return true;
- if (s1.Season == "summer" && s2.Season == "spring")
- return true;
- }
-
- return false;
+ return date?.GetHashCode() >= other?.GetHashCode();
}
- /// <summary>
- /// Greater or equal than operator. Tests the date being greater than or equal to each other
- /// </summary>
- /// <param name="s1">The first date being compared</param>
- /// <param name="s2">The second date being compared</param>
- /// <returns>If the dates are greater than or equal than</returns>
- public static bool operator <=(SDate s1, SDate s2)
+ /// <summary>Get whether one date is less than or equal to another.</summary>
+ /// <param name="date">The base date to compare.</param>
+ /// <param name="other">The other date to compare.</param>
+ public static bool operator <=(SDate date, SDate other)
{
- if (s1.Year < s2.Year)
- return true;
- else if (s1.Year == s2.Year)
- {
- if (s1.Season == s2.Season && s1.Day <= s2.Day)
- return true;
- else if (s1.Season == "spring" && s2.Season != "spring")
- return true;
- if (s1.Season == "summer" && (s2.Season == "fall" || s2.Season == "winter"))
- return true;
- if (s1.Season == "fall" && s2.Season == "winter")
- return true;
- }
-
- return false;
+ return date?.GetHashCode() <= other?.GetHashCode();
}
- /// <summary>
- /// Greater than operator. Tests the date being greater than to each other
- /// </summary>
- /// <param name="s1">The first date being compared</param>
- /// <param name="s2">The second date being compared</param>
- /// <returns>If the dates are greater than</returns>
- public static bool operator <(SDate s1, SDate s2)
+ /// <summary>Get whether one date is less than another.</summary>
+ /// <param name="date">The base date to compare.</param>
+ /// <param name="other">The other date to compare.</param>
+ public static bool operator <(SDate date, SDate other)
{
- if (s1.Year < s2.Year)
- return true;
- else if (s1.Year == s2.Year)
- {
- if (s1.Season == s2.Season && s1.Day < s2.Day)
- return true;
- else if (s1.Season == "spring" && s2.Season != "spring")
- return true;
- if (s1.Season == "summer" && (s2.Season == "fall" || s2.Season == "winter"))
- return true;
- if (s1.Season == "fall" && s2.Season == "winter")
- return true;
- }
-
- return false;
+ return date?.GetHashCode() < other?.GetHashCode();
}
- /// <summary>
- /// Overrides the equals function.
- /// </summary>
+ /// <summary>Overrides the equals function.</summary>
/// <param name="obj">Object being compared.</param>
/// <returns>The equalaity of the object.</returns>
public override bool Equals(object obj)
{
- return base.Equals(obj);
+ return obj is SDate other && this == other;
}
- /// <summary>
- /// This returns the hashcode of the object
- /// </summary>
- /// <returns>The hashcode of the object.</returns>
+ /// <summary>Get a hash code which uniquely identifies a date.</summary>
public override int GetHashCode()
{
- return base.GetHashCode();
+ // return the number of days since 01 spring Y1
+ int yearIndex = this.Year - 1;
+ return
+ yearIndex * this.DaysInSeason * this.SeasonsInYear
+ + this.GetSeasonIndex() * this.DaysInSeason
+ + this.Day;
}
+
/*********
** Private methods
*********/
+ /// <summary>Get the current season index.</summary>
+ /// <exception cref="InvalidOperationException">The current season wasn't recognised.</exception>
+ private int GetSeasonIndex()
+ {
+ int index = Array.IndexOf(this.Seasons, this.Season);
+ if (index == -1)
+ throw new InvalidOperationException($"The current season '{this.Season}' wasn't recognised.");
+ return index;
+ }
+
/// <summary>Get the real index in an array which should be treated as a two-way loop.</summary>
/// <param name="index">The index in the looped array.</param>
/// <param name="length">The number of elements in the array.</param>