diff options
author | Kevin Daughtridge <kevin@kdau.com> | 2020-04-13 23:29:56 -0700 |
---|---|---|
committer | Kevin Daughtridge <kevin@kdau.com> | 2020-04-14 00:19:20 -0700 |
commit | 7a60dc4ee9d32831f55fe066c20729ca8e9dc8a1 (patch) | |
tree | 28d1abec57e168ad0e7c8dfe63c906d3f51a1a33 | |
parent | 49c2ee517d3276b4547bb25ceda3ebf5e9707887 (diff) | |
download | SMAPI-7a60dc4ee9d32831f55fe066c20729ca8e9dc8a1.tar.gz SMAPI-7a60dc4ee9d32831f55fe066c20729ca8e9dc8a1.tar.bz2 SMAPI-7a60dc4ee9d32831f55fe066c20729ca8e9dc8a1.zip |
SDate: fixes to new methods
- FromWorldDate: replace with explicit operator SDate
- ToWorldDate: replace with explicit operator WorldDate
- ToLocaleString: use Utility.getDateStringFor directly
- FromDaysSinceStart: reinterpret exception to an appropriate one
-rw-r--r-- | src/SMAPI/Utilities/SDate.cs | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/src/SMAPI/Utilities/SDate.cs b/src/SMAPI/Utilities/SDate.cs index 301ea9d2..8cb55891 100644 --- a/src/SMAPI/Utilities/SDate.cs +++ b/src/SMAPI/Utilities/SDate.cs @@ -66,18 +66,18 @@ namespace StardewModdingAPI.Utilities return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.year, allowDayZero: true); } - /// <summary>Get the date equivalent to the given WorldDate.</summary> - /// <param name="worldDate">A date returned from a core game property or method.</param> - public static SDate FromWorldDate(WorldDate worldDate) - { - return new SDate(worldDate.DayOfMonth, worldDate.Season, worldDate.Year, allowDayZero: true); - } - /// <summary>Get the date falling the given number of days after 0 spring Y1.</summary> /// <param name="daysSinceStart">The number of days since 0 spring Y1.</param> public static SDate FromDaysSinceStart(int daysSinceStart) { - return new SDate(0, "spring", 1, allowDayZero: true).AddDays(daysSinceStart); + try + { + return new SDate(0, "spring", 1, allowDayZero: true).AddDays(daysSinceStart); + } + catch (ArithmeticException) + { + throw new ArgumentException($"Invalid daysSinceStart '{daysSinceStart}', must be at least 1."); + } } /// <summary>Get a new date with the given number of days added.</summary> @@ -118,13 +118,7 @@ namespace StardewModdingAPI.Utilities /// <summary>Get a string representation of the date in the current game locale.</summary> public string ToLocaleString() { - return this.ToWorldDate().Localize(); - } - - /// <summary>Get the date as an instance of the game's WorldDate class. This is intended for passing to core game methods.</summary> - public WorldDate ToWorldDate() - { - return new WorldDate(this.Year, this.Season, this.Day); + return Utility.getDateStringFor(this.Day, this.SeasonIndex, this.Year); } /**** @@ -153,6 +147,19 @@ namespace StardewModdingAPI.Utilities /**** ** Operators ****/ + /// <summary>Get the SDate equivalent to the given WorldDate.</summary> + /// <param name="worldDate">A date returned from a core game property or method.</param> + public static explicit operator SDate(WorldDate worldDate) + { + return new SDate(worldDate.DayOfMonth, worldDate.Season, worldDate.Year, allowDayZero: true); + } + + /// <summary>Get the SDate as an instance of the game's WorldDate class. This is intended for passing to core game methods.</summary> + public static explicit operator WorldDate(SDate date) + { + return new WorldDate(date.Year, date.Season, date.Day); + } + /// <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> |