Last active
June 26, 2022 07:55
-
-
Save jakewilliami/8a8a8e950be29f0532d6f847624a2e33 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Dates | |
function findfirst_tuesday(d::Date) | |
fd = dayofweek(firstdayofmonth(d)) | |
return Tuesday - fd + 1 + 7(fd > Tuesday) | |
end | |
findfirst_tuesday(m::Month, y::Year) = findfirst_tuesday(Date(m, y)) | |
function penultimate_tuesday_date(m::Month, y::Year) | |
ft = findfirst_tuesday(m, y) # first Tuesday | |
d = Date(Day(ft), m, y) | |
n_tuesdays = daysofweekinmonth(d) | |
pt = Day(ft + 7(n_tuesdays - 2)) # penultimate Tuesday | |
return Date(pt, m, y) | |
end | |
penultimate_tuesday_date(d::Date) = penultimate_tuesday_date(Month(d), Year(d)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ported and extended logic to PenultimateDays.jl!