diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-04-15 18:36:45 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-15 18:36:45 -0400 |
commit | 01b970c84a565ff264f0482a3c544c609886912e (patch) | |
tree | e8b2830861a7d85a463477dbe2ff8d24b452de33 /src/SMAPI/Utilities | |
parent | 97821362daeaa3dd34e3728680760d44043825be (diff) | |
parent | 421bcfcd3e1dcd79c4ab9f9c44c300126ce01fe9 (diff) | |
download | SMAPI-01b970c84a565ff264f0482a3c544c609886912e.tar.gz SMAPI-01b970c84a565ff264f0482a3c544c609886912e.tar.bz2 SMAPI-01b970c84a565ff264f0482a3c544c609886912e.zip |
Merge pull request #709 from kdau/develop
SDate: Add WorldDate conversions and features
Diffstat (limited to 'src/SMAPI/Utilities')
-rw-r--r-- | src/SMAPI/Utilities/SDate.cs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/SMAPI/Utilities/SDate.cs b/src/SMAPI/Utilities/SDate.cs index 0ab37aa0..8cb55891 100644 --- a/src/SMAPI/Utilities/SDate.cs +++ b/src/SMAPI/Utilities/SDate.cs @@ -29,6 +29,9 @@ namespace StardewModdingAPI.Utilities /// <summary>The season name.</summary> public string Season { get; } + /// <summary>The season index.</summary> + public int SeasonIndex { get; } + /// <summary>The year.</summary> public int Year { get; } @@ -63,6 +66,20 @@ namespace StardewModdingAPI.Utilities return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.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) + { + 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> /// <param name="offset">The number of days to add.</param> /// <returns>Returns the resulting date.</returns> @@ -98,6 +115,12 @@ namespace StardewModdingAPI.Utilities return $"{this.Day:00} {this.Season} Y{this.Year}"; } + /// <summary>Get a string representation of the date in the current game locale.</summary> + public string ToLocaleString() + { + return Utility.getDateStringFor(this.Day, this.SeasonIndex, this.Year); + } + /**** ** IEquatable ****/ @@ -124,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> @@ -200,6 +236,7 @@ namespace StardewModdingAPI.Utilities // initialize this.Day = day; this.Season = season; + this.SeasonIndex = this.GetSeasonIndex(season); this.Year = year; this.DayOfWeek = this.GetDayOfWeek(day); this.DaysSinceStart = this.GetDaysSinceStart(day, season, year); |