Sha256: b52c7561ce5b19c8b2b2fcaed5d0d03e2d0c52d4fcd8634a07e6e18d9644813c

Contents?: true

Size: 1.57 KB

Versions: 327

Compression:

Stored size: 1.57 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

327 entries across 327 versions & 1 rubygems

Version Path
trackler-2.2.1.109 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.108 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.107 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.106 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.105 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.104 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.103 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.102 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.101 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.100 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.99 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.98 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.97 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.96 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.95 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.94 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.93 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.92 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.91 tracks/elixir/exercises/meetup/example.exs
trackler-2.2.1.90 tracks/elixir/exercises/meetup/example.exs