If each user owns their own data (i.e., indicates their time of year, and then enters their own information), then you can simply store the data with the season as part of this, however, I have a feeling that the script you are using is for general data for many users who define seasons differently.
You must be very careful to "normalize" the dates, since a leap year can cause unforeseen problems, that is, trying to set February 29 for a non-leap year will cause problems / exceptions.
I put it together from below, its C # unpredictably, but the concept will be the same. I really tested the code, but like psudo code, it can help.
public class SeasonChecker { public enum Season {Summer, Autumn, Winter, Spring}; private List<SeasonRange> _seasons = new List<SeasonRange>(); public void DefineSeason(Season season, DateTime starting, DateTime ending) { starting = starting.Date; ending = ending.Date; if(ending.Month < starting.Month) { // split into 2 DateTime tmp_ending = new DateTime(ending.Year, 12, 31); DateTime tmp_starting = new DateTime(starting.Year, 1, 1); SeasonRange r1 = new SeasonRange() { Season = season, Starting= tmp_starting, Ending = ending }; SeasonRange r2 = new SeasonRange() { Season = season, Starting= starting, Ending = tmp_ending }; this._seasons.Add(r1); this._seasons.Add(r2); } else { SeasonRange r1 = new SeasonRange() { Season = season, Starting= starting, Ending = ending }; this._seasons.Add(r1); } } public Season GetSeason(DateTime check) { foreach(SeasonRange range in _seasons) { if(range.InRange(check)) return range.Season; } throw new ArgumentOutOfRangeException("Does not fall into any season"); } private class SeasonRange { public DateTime Starting; public DateTime Ending; public Season Season; public bool InRange(DateTime test) { if(test.Month == Starting.Month) { if(test.Day >= Starting.Day) { return true; } } else if(test.Month == Ending.Month) { if(test.Day <= Ending.Day) { return true; } } else if(test.Month > Starting.Month && test.Month < Ending.Month) { return true; } return false; } } }
Please note that the code above makes the assumption that the season will not start and end in the same month - it is safe enough, I think!
source share