summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Utilities
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-06-18 20:55:12 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-06-18 20:55:12 -0400
commit3e50c90230bf4f7aa4efb69b3db47dddd1e43750 (patch)
tree21331eb67c678031ef6f6635dc77622445688ac9 /src/StardewModdingAPI/Utilities
parent0a8c07cc0773a1e3c109a3ccfa8b95896b7d75a8 (diff)
downloadSMAPI-3e50c90230bf4f7aa4efb69b3db47dddd1e43750.tar.gz
SMAPI-3e50c90230bf4f7aa4efb69b3db47dddd1e43750.tar.bz2
SMAPI-3e50c90230bf4f7aa4efb69b3db47dddd1e43750.zip
add IEquatable<SDate> interface to SDate (#307)
Diffstat (limited to 'src/StardewModdingAPI/Utilities')
-rw-r--r--src/StardewModdingAPI/Utilities/SDate.cs61
1 files changed, 35 insertions, 26 deletions
diff --git a/src/StardewModdingAPI/Utilities/SDate.cs b/src/StardewModdingAPI/Utilities/SDate.cs
index 5f7ff030..e0613491 100644
--- a/src/StardewModdingAPI/Utilities/SDate.cs
+++ b/src/StardewModdingAPI/Utilities/SDate.cs
@@ -5,7 +5,7 @@ using StardewValley;
namespace StardewModdingAPI.Utilities
{
/// <summary>Represents a Stardew Valley date.</summary>
- public class SDate
+ public class SDate : IEquatable<SDate>
{
/*********
** Properties
@@ -66,6 +66,12 @@ namespace StardewModdingAPI.Utilities
this.Year = year;
}
+ /// <summary>Get the current in-game date.</summary>
+ public static SDate Now()
+ {
+ return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.year);
+ }
+
/// <summary>Get a new date with the given number of days added.</summary>
/// <param name="offset">The number of days to add.</param>
/// <returns>Returns the resulting date.</returns>
@@ -108,15 +114,37 @@ namespace StardewModdingAPI.Utilities
return $"{this.Day:00} {this.Season} Y{this.Year}";
}
- /// <summary>Get the current in-game date.</summary>
- public static SDate Now()
+ /****
+ ** IEquatable
+ ****/
+ /// <summary>Get whether this instance is equal to another.</summary>
+ /// <param name="other">The other value to compare.</param>
+ public bool Equals(SDate other)
{
- return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.year);
+ return this == other;
}
- /*********
- ** Operator methods
- *********/
+ /// <summary>Get whether this instance is equal to another.</summary>
+ /// <param name="obj">The other value to compare.</param>
+ public override bool Equals(object obj)
+ {
+ return obj is SDate other && this == other;
+ }
+
+ /// <summary>Get a hash code which uniquely identifies a date.</summary>
+ public override int 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;
+ }
+
+ /****
+ ** Operators
+ ****/
/// <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>
@@ -166,25 +194,6 @@ namespace StardewModdingAPI.Utilities
return date?.GetHashCode() < other?.GetHashCode();
}
- /// <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 obj is SDate other && this == other;
- }
-
- /// <summary>Get a hash code which uniquely identifies a date.</summary>
- public override int 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