diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-04-27 16:30:41 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-04-27 16:30:41 -0400 |
commit | df6e745c6b842290338317ed1d3e969ee222998c (patch) | |
tree | 4b8b28ddd7d8b9fe381bc7902f7c19187a61d4be /src/SMAPI/Utilities/SDate.cs | |
parent | d0dad43e243864eb8bfdf46c853c5c7fba7c55ed (diff) | |
parent | f44151dbb47b82250955be7c25145d1774bec705 (diff) | |
download | SMAPI-df6e745c6b842290338317ed1d3e969ee222998c.tar.gz SMAPI-df6e745c6b842290338317ed1d3e969ee222998c.tar.bz2 SMAPI-df6e745c6b842290338317ed1d3e969ee222998c.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Utilities/SDate.cs')
-rw-r--r-- | src/SMAPI/Utilities/SDate.cs | 63 |
1 files changed, 62 insertions, 1 deletions
diff --git a/src/SMAPI/Utilities/SDate.cs b/src/SMAPI/Utilities/SDate.cs index 0ab37aa0..03230334 100644 --- a/src/SMAPI/Utilities/SDate.cs +++ b/src/SMAPI/Utilities/SDate.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using StardewModdingAPI.Framework; using StardewValley; namespace StardewModdingAPI.Utilities @@ -19,6 +20,9 @@ namespace StardewModdingAPI.Utilities /// <summary>The number of days in a season.</summary> private readonly int DaysInSeason = 28; + /// <summary>The core SMAPI translations.</summary> + internal static Translator Translations; + /********* ** Accessors @@ -29,6 +33,10 @@ namespace StardewModdingAPI.Utilities /// <summary>The season name.</summary> public string Season { get; } + /// <summary>The index of the season (where 0 is spring, 1 is summer, 2 is fall, and 3 is winter).</summary> + /// <remarks>This is used in some game calculations (e.g. seasonal game sprites) and methods (e.g. <see cref="Utility.getSeasonNameFromNumber"/>).</remarks> + public int SeasonIndex { get; } + /// <summary>The year.</summary> public int Year { get; } @@ -63,6 +71,30 @@ namespace StardewModdingAPI.Utilities return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.year, allowDayZero: true); } + /// <summary>Get a date from the 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 date from a game date instance.</summary> + /// <param name="date">The world date.</param> + public static SDate From(WorldDate date) + { + if (date == null) + return null; + + return new SDate(date.DayOfMonth, date.Season, date.Year, allowDayZero: true); + } + /// <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> @@ -92,12 +124,40 @@ namespace StardewModdingAPI.Utilities return new SDate(day, this.Seasons[seasonIndex], year); } - /// <summary>Get a string representation of the date. This is mainly intended for debugging or console messages.</summary> + /// <summary>Get a game date representation of the date.</summary> + public WorldDate ToWorldDate() + { + return new WorldDate(this.Year, this.Season, this.Day); + } + + /// <summary>Get an untranslated string representation of the date. This is mainly intended for debugging or console messages.</summary> public override string ToString() { return $"{this.Day:00} {this.Season} Y{this.Year}"; } + /// <summary>Get a translated string representation of the date in the current game locale.</summary> + /// <param name="withYear">Whether to get a string which includes the year number.</param> + public string ToLocaleString(bool withYear = true) + { + // get fallback translation from game + string fallback = Utility.getDateStringFor(this.Day, this.SeasonIndex, this.Year); + if (SDate.Translations == null) + return fallback; + + // get short format + string seasonName = Utility.getSeasonNameFromNumber(this.SeasonIndex); + return SDate.Translations + .Get(withYear ? "generic.date-with-year" : "generic.date", new + { + day = this.Day, + year = this.Year, + season = seasonName, + seasonLowercase = seasonName?.ToLower() + }) + .Default(fallback); + } + /**** ** IEquatable ****/ @@ -200,6 +260,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); |