Getting the third Friday of the month

When I’ll have an access to my computer I’ll write full code, but idea is the following

  1. Calculate day of the week for the first day of the month e.g. “2021-03-01”
  2. Calculate the distance to the nearest Friday (number in range 0:6)
  3. Add 14
  4. Add result to the first day of the month

UPD:

function thirdfriday(dt)
    dt = floor(Date(dt), Month)
    dt |> dayofweek |> 
        (x -> 5 - x < 0 ? 12 - x : 5 - x) |> 
        (x -> x + 14) |> 
        (x -> dt + Day(x))
end

julia> dr = Date(2021):Month(1):Date(2022);
julia> thirdfriday.(dr)
13-element Vector{Date}:
 2021-01-15
 2021-02-19
 2021-03-19
 2021-04-16
 2021-05-21
 2021-06-18
 2021-07-16
 2021-08-20
 2021-09-17
 2021-10-15
 2021-11-19
 2021-12-17
 2022-01-21

It should be efficient, I suppose, since it adds no allocations, pure arithmetic.

It was interesting to read Dates documentation, there are interesting tonext and toprev functions as well as filter. So, if you are not afraid of iterations, at least they can be written in a very compact form

julia> tonext(x -> (dayofweek(x) == 5) && (dayofweekofmonth(x) == 3), Date("2020-03-01"))
2020-03-20

julia> dr = Date(2021):Day(1):Date(2022)
Date("2021-01-01"):Day(1):Date("2022-01-01")

julia> filter(dr) do x
           (dayofweek(x) == 5) && (dayofweekofmonth(x) == 3)
       end
12-element Vector{Date}:
 2021-01-15
 2021-02-19
 2021-03-19
 2021-04-16
 2021-05-21
 2021-06-18
 2021-07-16
 2021-08-20
 2021-09-17
 2021-10-15
 2021-11-19
 2021-12-17
5 Likes