I needed to have a more flexible approach. My seasons are stored in the database, so I can't just compare day/month.
But if you try to think of an equation to compare the start/end date of a season to a specific day, it's not as easy as it seems since the newyear always appears to bite you in the neck. To get around this I decided to split the season that crosses the newyear into 2 seperate seasons. Then you can easily just compare day/month with math logic and determine the correct season.
Approach in C#
public class SeasonChecker
{
public SeasonChecker()
{
}
public Task<TSeason> FindSeasonAsync<TSeason>(IEnumerable<TSeason> seasons, DateTime date) where TSeason : class, ISeason
{
var result = seasons
.Select(s =>
{
// Find season that crosses newyear
if (s.Start.Year == s.End.Year)
{
return new[] {
new {
OriginalSeason = s,
// Remap the season to the year 2000
ProcessableSeason = new InternalSeason {
Name = s.Name,
Start = new DateTime(2000, s.Start.Month, s.Start.Day),
End = new DateTime(2000, s.End.Month, s.End.Day)
}
}
};
}
else
{
// If the season crosses the newyear, split the season
return new[] {
new {
OriginalSeason = s,
// Remap the season to the year 2000
ProcessableSeason = new InternalSeason {
Name = s.Name,
Start = new DateTime(2000, s.Start.Month, s.Start.Day),
End = new DateTime(2000, 12, 31)
}
},
new {
OriginalSeason = s,
// Remap the season to the year 2000
ProcessableSeason = new InternalSeason {
Name = s.Name,
Start = new DateTime(2000, 1, 1),
End = new DateTime(2000, s.End.Month, s.End.Day)
}
}
};
}
})
.SelectMany(s => s)
.FirstOrDefault(s =>
// Now we can easily compare the dates.
DateTime.Compare(s.ProcessableSeason.Start, new DateTime(2000, date.Month, date.Day)) <= 0 &&
DateTime.Compare(new DateTime(2000, date.Month, date.Day), s.ProcessableSeason.End) <= 0
)?.OriginalSeason;
return Task.FromResult(result);
}
}
public interface ISeason
{
string Name { get; set; }
DateTime Start { get; set; }
DateTime End { get; set; }
}
internal class InternalSeason : ISeason
{
public string Name { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
This LINQ expression is entirely convertible to SQL, so you can apply it to a DbSet
.
Approach in SQL
DECLARE @datum DATE;
SET @datum = '2020-12-21';
SELECT
Season.SeasonId,
Season.DateFrom,
Season.DateUntil,
CASE
WHEN YEAR(Season.DateFrom) = YEAR(Season.DateUntil)
THEN
CASE
WHEN
(
-- Season.DateFrom (2000) < NOW (2000)
MONTH(Season.DateFrom) < MONTH(CAST(@datum AS SQL_DATE))
OR (
MONTH(Season.DateFrom) = MONTH(CAST(@datum AS SQL_DATE))
AND
DAYOFMONTH(Season.DateFrom) <= DAYOFMONTH(CAST(@datum AS SQL_DATE))
)
)
AND
(
-- NOW (2000) < Season.DateUntil (2000)
MONTH(CAST(@datum AS SQL_DATE)) < MONTH(Season.DateUntil)
OR (
MONTH(CAST(@datum AS SQL_DATE)) = MONTH(Season.DateUntil)
AND
DAYOFMONTH(CAST(@datum AS SQL_DATE)) <= DAYOFMONTH(Season.DateUntil)
)
)
THEN TRUE
ELSE FALSE
END
ELSE
CASE
-- ( Season.DateFrom (2000) < NOW (2000) < 31/12) OR (1/1/2000 < NOW (2000) < Season.DateUntil (2000) )
WHEN
-- Season.DateFrom (2000) < NOW (2000) < 31/12/2000)
(
(
-- Season.DateFrom (2000) <= NOW (2000)
MONTH(Season.DateFrom) < MONTH(CAST(@datum AS SQL_DATE))
OR (
MONTH(Season.DateFrom) = MONTH(CAST(@datum AS SQL_DATE))
AND
DAYOFMONTH(Season.DateFrom) <= DAYOFMONTH(CAST(@datum AS SQL_DATE))
)
)
AND
(
-- NOW (2000) <= 31/12)
MONTH(CAST(@datum AS SQL_DATE)) < 12
OR (
MONTH(CAST(@datum AS SQL_DATE)) = 12
AND
DAYOFMONTH(CAST(@datum AS SQL_DATE)) <= 31
)
)
)
OR
-- 1/1/2000 < NOW (2000) < Season.DateUntil (2000)
(
(
-- 1/1/2000 < NOW (2000)
1 < MONTH(CAST(@datum AS SQL_DATE))
OR (
1 = MONTH(CAST(@datum AS SQL_DATE))
AND
1 <= DAYOFMONTH(CAST(@datum AS SQL_DATE))
)
)
AND
(
-- NOW (2000) < Season.DateUntil (2000)
MONTH(CAST(@datum AS SQL_DATE)) < MONTH(Season.DateUntil)
OR (
MONTH(CAST(@datum AS SQL_DATE)) = MONTH(Season.DateUntil)
AND
DAYOFMONTH(CAST(@datum AS SQL_DATE)) <= DAYOFMONTH(Season.DateUntil)
)
)
)
THEN TRUE
ELSE FALSE
END
END
AS CurrentSeason
FROM Season