diff options
Diffstat (limited to 'src/StardewModdingAPI')
-rw-r--r-- | src/StardewModdingAPI/Utilities/SDate.cs | 61 |
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 |