Best way to account for leap years in scripts involving seasons?

I currently have a script where the season gets picked based on the current day of year, using os.date. This is the current state of the code:

    if CurrentDay >= 79 and CurrentDay < 172 then
		Season = "Spring"
	elseif CurrentDay >= 172 and CurrentDay < 265 then
		Season = "Summer"
	elseif CurrentDay >= 265 and CurrentDay < 355 then
		Season = "Autumn"
	elseif CurrentDay < 79 or CurrentDay >= 355 then
		Season = "Winter"

However, this method raises issues when considering the fact that leap years add an extra day; for the remainder of the year, each season would start one day earlier than it’s supposed to.

I can’t decide what the most efficient way would be to implement some extra code to account for leap years. Can anyone give me some pointers in the right direction?

Thank you in advance.

os.date("*t") will give you a table with all the date info split into convenient fields. You could check the month and day, for instance.

See os for a full list of fields (it’s at the bottom of the os.date entry).

You mean like, check in a specific month to see if the day is higher or lower than a specific value?

For instance, if I wanted to check if it was springtime, I’d check within the month of March to see if the day number was either <, or >= 20?

Yeah like

local WINTER = 0
local SPRING = 1
local AUTUMN = 2
local SUMMER = 3

local function GetSeason(time)
  local date = os.date("*t", time)
  local m = date.month
  local d = date.day
  if m <= 3 and d <= 20 then
    return WINTER
  elseif m <= 6 and d <= 20 then
    return SPRING
  elseif m <= 9 and d <= 20 then
    return SUMMER
  elseif m <= 12 and d <= 20 then
    return AUTUMN
  else
    return WINTER
  end
end

Edit: made the 21s into 20s

2 Likes