Best practice to ignore year dates for date ranges

I need to model some information about the seasons, and I need to track them based on the start and end dates of the agnostic year. That is, I need to allow users to define the summer as between, say, May 15 and September 10, and do this for all years.

I will need to do a big check like isThisDateInSeason). All date manipulation functions (for example, date, calendar) work only with valid dates, that is, with information about the year.

Are there any recommendations on how to do this? I can come up with a bunch of hacker ways to do this (e.g. store the month and day of the month or store dates and just bring them to the base year so I can compare), but it seems like there might be a better way,

I am writing this in Java or Groovy.

Did the Joda-Time library help ? I have no experience with him, but he looks much more flexible.

I found this question on how to determine the season from a date, but this applies to months, and I need more flexibility to include dates.

+4
source share
3 answers

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!

+4
source

I think you will have to roll your own DateRange class, although this is such a common problem, you would hope that there is already a smart user there that does this. As a general point when dealing with seasons, you must also consider geography, which will make life very difficult. In Oz, we are the opposite of the United States, so the summer runs from November to February.

+2
source

Where is the data? If it is in a database, I would consider creating a table for dates (for example, measuring time in OLAP). You can then calculate the column for your seasons, as well as for fiscal quarters, in some examples of time measurement. (this assumes that your season does not change, but has fixed dates).

All checks such as β€œif the time is in the season” will be pre-built, which will increase the cost of calculating the season at the time of tuning, and not at run time.

Edit: Just looked at your comment about custom season dates. This will work anyway, since you can make a connection in the database, including measuring the time, which might be easier to work with the data set, and then in Java.

+2
source

All Articles