The current function is:
Ruby:
def pbGetSeason
return (pbGetTimeNow.mon - 1) % 4
end
What this does is:
pbGetTimeNow
gets the current time
pbGetTimeNow.mon
extracts the month in the form of a number between 1 and 12, following the usual numbering of months
pbGetTimeNow.mon - 1
subtracts 1 to the month, but this makes sense only after explaining the next operation
(pbGetTimeNow.mon - 1) % 4
gets the remainder of the division of pbGetTimeNow.mon - 1
by 4.
Since Essentials wants a cycle of four seasons that last one month, starting from Spring in January, we have:
- January is month 1; subtract 1 to get 0, and get the remainder of the division of 0 by 4: it's 0, which corresponds to Spring (see the code of
pbGetSeasonName
)
- February is month 2; subtract 1 to get 1, and get the remainder of the division of 1 by 4: it's 1, which corresponds to Summer (see the code of
pbGetSeasonName
)
- Let's skip to, like, August; August is month 8; subtract 1 to get 7, and get the remainder of the division of 7 by 4: it's 3, which corresponds to Winter (see the code of
pbGetSeasonName
)
What you want is:
Winter = January, February, March; so pbGetSeason returns 3 (Winter) when the current month is 1, 2, or 3
Spring = April, May, June; so pbGetSeason returns 0 (Spring) when the current month is 4, 5, or 6
Summer = July, August, September: so pbGetSeason returns 1 (Summer) when the current month is 7, 8, or 9
Autumn = October, November, December; so pbGetSeason returns 2 (Autumn) when the current month is 10, 11, or 12
Following the previous definition of
pbGetSeason
, we want to:
- Get the current month with
pbGetTimeNow.mon
; we get a number between 1 and 12.
- If this number is 1 or 2 or 3, we want to return 3 (Winter)
- If this number is 4 or 5 or 6, we want to return 0 (Spring)
- If this number is 7 or 8 or 9, we want to return 1 (Summer)
- If this number is 10 or 11 or 12, we want to return 2 (Autumn)
One solution to do this is the use of case:
Ruby:
def pbGetSeason
case pbGetTimeNow.mon
when 1 # January
return 3 # Winter
when 2 # February
return 3 # Winter
when 3 # March
return 3 # Winter
when 4 # April
return 0 # Spring
when 5 # May
return 0 # Spring
when 6 # June
return 0 # Spring
when 7 # July
return 1 # Summer
when 8 # August
return 1 # Summer
when 9 # September
return 1 # Summer
when 10 # October
return 2 # Autumn
when 11 # November
return 2 # Autumn
else # when 12, December
return 2 # Autumn
end
end
Equivalently but more concisely:
Ruby:
def pbGetSeason
case pbGetTimeNow.mon
when 1, 2, 3 # January, February, March
return 3 # Winter
when 4, 5, 6 # April, May, June
return 0 # Spring
when 7, 8, 9 # July, August, September
return 1 # Summer
else # October, November, December
return 2 # Autumn
end
end
Or this one-liner if we are comfortable with mathematics:
Ruby:
def pbGetSeason
return ((pbGetTimeNow.mon / 4).to_i + 3) % 4
end
This gets the month, divides it by 4, gets the greatest integer below the result, adds 3 and gets the remainder of the division of this result by 4.
For January: the month is 1, we divide by 4 to get 0.25, the greatest integer below this is 0, we add 3 to get 3, and the remainder of the division of 3 by 4 is 3, so it's Winter.
For August: the month is 8, we divide by 4 to get 2.0, the greatest integer below this is 2, we add 3 to get 5, and the remainder of the division of 5 by 4 is 1, so it's Summer.
However, if you want to follow the official dates for the change of seasons, like Summer on the 20th of June, and so on, you should edit the first code with the cases and add a special case for the months that see a change of seasons.
Ruby:
def pbGetSeason
current_time = pbGetTimeNow
case current_time.mon
when 1 # January
return 3 # Winter
when 2 # February
return 3 # Winter
when 3 # March
# 20th of March is the start of Spring. Before, it's Winter.
if current_time.day < 20
return 3 # Winter
else
return 0 # Spring
end
when 4 # April
return 0 # Spring
when 5 # May
return 0 # Spring
when 6 # June
# 20th of June is the start of Summer. Before, it's Spring.
if current_time.day < 20
return 0 # Spring
else
return 1 # Summer
end
when 7 # July
return 1 # Summer
when 8 # August
return 1 # Summer
when 9 # September
# 22th of September is the start of Autumn. Before, it's Summer.
if current_time.day < 22
return 1 # Summer
else
return 2 # Autumn
end
when 10 # October
return 2 # Autumn
when 11 # November
return 2 # Autumn
else # when 12, December
# 21th of December is the start of Winter. Before, it's Autumn.
if current_time.day < 21
return 2 # Autumn
else
return 3 # Winter
end
end
end
Hope that helps.
PS: I realised I forgot there was a Southern Hemisphere lol. But I guess you can adapt this code.