Skip to content

Instantly share code, notes, and snippets.

@jakewilliami
Last active June 26, 2022 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakewilliami/8a8a8e950be29f0532d6f847624a2e33 to your computer and use it in GitHub Desktop.
Save jakewilliami/8a8a8e950be29f0532d6f847624a2e33 to your computer and use it in GitHub Desktop.
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))
@jakewilliami
Copy link
Author

Ported and extended logic to PenultimateDays.jl!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment