Sha256: 53c6ac6a85c9a7fcc18feb571300bc6afcee3b3c3122cdc72bd9116e81a8f45c

Contents?: true

Size: 1.6 KB

Versions: 69

Compression:

Stored size: 1.6 KB

Contents

defmodule Meetup do
  @moduledoc """
  Calculate meetup dates.
  """

  @type weekday ::
          :monday
          | :tuesday
          | :wednesday
          | :thursday
          | :friday
          | :saturday
          | :sunday

  @type schedule :: :first | :second | :third | :fourth | :last | :teenth

  @doc """
  Calculate a meetup date.

  The schedule is in which week (1..4, last or "teenth") the meetup date should
  fall.
  """
  @spec meetup(pos_integer, pos_integer, weekday, schedule) :: :calendar.date()
  def meetup(year, month, weekday, :last) do
    start_day = :calendar.last_day_of_the_month(year, month) - 6
    meetup_nth(year, month, start_day, weekday)
  end

  def meetup(year, month, weekday, schedule) do
    meetup_nth(year, month, schedule_start(schedule), weekday)
  end

  defp meetup_nth(year, month, start_day, weekday) do
    w = weekday_num(weekday)

    case w - :calendar.day_of_the_week(year, month, start_day) do
      n when n < 0 -> {year, month, start_day + n + 7}
      n -> {year, month, start_day + n}
    end
  end

  # Aliases for the day of week numbers from Erlang.
  #
  # Case matching is used because that's very fast in Elixir/Erlang.
  defp weekday_num(:monday), do: 1
  defp weekday_num(:tuesday), do: 2
  defp weekday_num(:wednesday), do: 3
  defp weekday_num(:thursday), do: 4
  defp weekday_num(:friday), do: 5
  defp weekday_num(:saturday), do: 6
  defp weekday_num(:sunday), do: 7

  defp schedule_start(:first), do: 1
  defp schedule_start(:second), do: 8
  defp schedule_start(:third), do: 15
  defp schedule_start(:fourth), do: 22
  defp schedule_start(:teenth), do: 13
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/meetup/example.exs