summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-09-06 22:04:51 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-09-06 22:04:51 -0400
commitb2b3df08bcd2de9c9f9ca98f61a50f4c6d1fd6e5 (patch)
tree4b0a08423249bcb1b6bd3da33901014c607637e5 /src/StardewModdingAPI
parentd971514a3d316e1466db51f9709635ce6d4e6b91 (diff)
downloadSMAPI-b2b3df08bcd2de9c9f9ca98f61a50f4c6d1fd6e5.tar.gz
SMAPI-b2b3df08bcd2de9c9f9ca98f61a50f4c6d1fd6e5.tar.bz2
SMAPI-b2b3df08bcd2de9c9f9ca98f61a50f4c6d1fd6e5.zip
rewrite date calculation to handle edge cases
Diffstat (limited to 'src/StardewModdingAPI')
-rw-r--r--src/StardewModdingAPI/Utilities/SDate.cs58
1 files changed, 20 insertions, 38 deletions
diff --git a/src/StardewModdingAPI/Utilities/SDate.cs b/src/StardewModdingAPI/Utilities/SDate.cs
index d7631598..5073259d 100644
--- a/src/StardewModdingAPI/Utilities/SDate.cs
+++ b/src/StardewModdingAPI/Utilities/SDate.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Linq;
using StardewValley;
@@ -86,34 +86,27 @@ namespace StardewModdingAPI.Utilities
/// <exception cref="ArithmeticException">The offset would result in an invalid date (like year 0).</exception>
public SDate AddDays(int offset)
{
- // simple case
- int day = this.Day + offset;
- string season = this.Season;
- int year = this.Year;
+ // get new hash code
+ int hashCode = this.GetHashCode() + offset;
+ if (hashCode < 1)
+ throw new ArithmeticException($"Adding {offset} days to {this} would result in a date before 01 spring Y1.");
- // handle season transition
- if (day > this.DaysInSeason || day < 1)
- {
- // get season index
- int curSeasonIndex = this.GetSeasonIndex();
-
- // get season offset
- int seasonOffset = day / this.DaysInSeason;
- if (day < 1)
- seasonOffset -= 1;
-
- // get new date
- day = this.GetWrappedIndex(day, this.DaysInSeason);
- season = this.Seasons[this.GetWrappedIndex(curSeasonIndex + seasonOffset, this.Seasons.Length)];
- year += seasonOffset / this.Seasons.Length;
- }
+ // get day
+ int day = hashCode % 28;
+ if (day == 0)
+ day = 28;
- // validate
- if (year < 1)
- throw new ArithmeticException($"Adding {offset} days to {this} would result in invalid date {day:00} {season} {year}.");
+ // get season index
+ int seasonIndex = hashCode / 28;
+ if (seasonIndex > 0 && hashCode % 28 == 0)
+ seasonIndex -= 1;
+ seasonIndex %= 4;
- // return new date
- return new SDate(day, season, year);
+ // get year
+ int year = hashCode / (this.Seasons.Length * this.DaysInSeason) + 1;
+
+ // create date
+ 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>
@@ -142,7 +135,7 @@ namespace StardewModdingAPI.Utilities
/// <summary>Get a hash code which uniquely identifies a date.</summary>
public override int GetHashCode()
{
- // return the number of days since 01 spring Y1
+ // return the number of days since 01 spring Y1 (inclusively)
int yearIndex = this.Year - 1;
return
yearIndex * this.DaysInSeason * this.SeasonsInYear
@@ -239,16 +232,5 @@ namespace StardewModdingAPI.Utilities
throw new InvalidOperationException($"The current season '{this.Season}' wasn't recognised.");
return index;
}
-
- /// <summary>Get the real index in an array which should be treated as a two-way loop.</summary>
- /// <param name="index">The index in the looped array.</param>
- /// <param name="length">The number of elements in the array.</param>
- private int GetWrappedIndex(int index, int length)
- {
- int wrapped = index % length;
- if (wrapped < 0)
- wrapped += length;
- return wrapped;
- }
}
}