Packages
ex_cldr_calendars
1.13.0-rc.1
2.4.4
2.4.3
2.4.2
2.4.1
2.4.0
2.3.1
2.3.0
2.2.0
2.1.1
2.1.0
2.0.0
1.26.4
1.26.3
1.26.2
1.26.1
1.26.0
1.25.2
1.25.1
1.25.0
1.24.2
1.24.1
1.24.0
retired
1.23.1
1.23.0
1.22.1
1.22.0
1.21.0
1.20.0
1.19.0
1.18.1
1.18.0
1.17.3
1.17.2
1.17.1
1.17.0
1.17.0-rc.3
1.17.0-rc.2
1.17.0-rc.1
1.17.0-rc.0
1.16.0
1.15.3
1.15.2
1.15.1
1.15.0
1.14.1
1.14.0
1.13.0
1.13.0-rc.1
1.13.0-rc.0
1.12.1
1.12.0
1.11.0
1.11.0-rc.0
1.10.1
1.10.0
1.9.0
1.8.1
1.8.0
1.8.0-rc.0
1.7.1
1.7.0
1.6.0
1.5.1
1.5.0
retired
1.4.0
retired
1.3.0
retired
1.2.0
retired
1.1.0
retired
1.0.0
retired
0.9.0
retired
0.8.0
retired
0.7.0
retired
0.6.0
retired
0.5.0
retired
0.4.1
0.4.0
retired
0.3.0
retired
0.2.0
retired
0.1.0
retired
Localized month- and week-based calendars and calendar functions based upon CLDR data.
Current section
Files
Jump to
Current section
Files
lib/cldr/calendar/sigils.ex
defmodule Cldr.Calendar.Sigils do
@moduledoc """
Implements the `~d` sigils to produce
dates, datetimes and naive datetimes.
"""
alias Cldr.Config
@doc """
Implements a ~d sigil for expressing dates.
Dates can be expressed in the following formats:
* `~d[yyyy-mm-dd]` which produces a date in the `Cldr.Calendar.Gregorian` calendar
* `~d[yyyy-Wmm-dd]` which produces a date in the `Cldr.Calendar.IsoWeek` calendar
* `~d[yyyy-mm-dd calendar]` which produces a date in the given month-based calendar
* `~d[yyyy-Wmm-dd calendar]` which produces a date in the given week-based calendar
* `~d[yyyy-mm-dd C.E Julian]` which produces a date in the Cldr.Calendar.Julian calendar
* `~d[yyyy-mm-dd B.C.E Julian]` which produces a date in the Cldr.Calendar.Julian calendar
## Examples
iex> import Cldr.Calendar.Sigils
iex> ~d[2019-01-01 Gregorian]
~d[2019-01-01 Gregorian]
iex> ~d[2019-W01-01]
~d[2019-W01-1 ISOWeek]
"""
defmacro sigil_d({:<<>>, _, [string]}, modifiers) do
do_sigil_d(string, modifiers)
|> Macro.escape()
end
def do_sigil_d(<<year::bytes-4, "-", month::bytes-2, "-", day::bytes-2>>, _) do
to_date(year, month, day, Cldr.Calendar.Gregorian)
end
def do_sigil_d(<<year::bytes-4, "-", month::bytes-2, "-", day::bytes-2, " C.E. Julian">>, _) do
to_date(year, month, day, Cldr.Calendar.Julian)
end
def do_sigil_d(<<year::bytes-4, "-", month::bytes-2, "-", day::bytes-2, " B.C.E. Julian">>, _) do
to_date("-" <> year, month, day, Calendar.Julian)
end
def do_sigil_d(
<<year::bytes-4, "-", month::bytes-2, "-", day::bytes-2, " ", calendar::binary>>,
_
) do
to_date(year, month, day, calendar)
end
def do_sigil_d(
<<"-", year::bytes-4, "-", month::bytes-2, "-", day::bytes-2, " ", calendar::binary>>,
_
) do
to_date("-" <> year, month, day, calendar)
end
def do_sigil_d(<<year::bytes-4, "-W", month::bytes-2, "-", day::bytes-2>>, _) do
to_date(year, month, day, Cldr.Calendar.ISOWeek)
end
def do_sigil_d(<<year::bytes-4, "-W", month::bytes-2, "-", day::bytes-1>>, _) do
to_date(year, month, day, Cldr.Calendar.ISOWeek)
end
def do_sigil_d(
<<year::bytes-4, "-W", month::bytes-2, "-", day::bytes-2, " ", calendar::binary>>,
_
) do
to_date(year, month, day, calendar)
end
def do_sigil_d(
<<year::bytes-4, "-W", month::bytes-2, "-", day::bytes-1, " ", calendar::binary>>,
_
) do
to_date(year, month, day, calendar)
end
defp to_date(year, month, day, calendar) do
[year, month, day] = Enum.map([year, month, day], &String.to_integer/1)
with {:ok, calendar} <- calendar_from_binary(calendar),
{:ok, date} <- Date.new(year, month, day, calendar) do
date
end
end
defp calendar_from_binary(calendar) do
inbuilt_calendar(calendar) ||
fiscal_calendar(calendar) ||
user_calendar(calendar) ||
calendar_error(calendar)
end
defp inbuilt_calendar(calendar) do
calendar = Module.concat(Cldr.Calendar, calendar)
get_calendar(calendar)
end
defp fiscal_calendar(calendar) do
calendar = Module.concat(Cldr.Calendar.FiscalYear, calendar)
get_calendar(calendar)
end
defp user_calendar(calendar) do
Module.concat("Elixir", calendar)
|> get_calendar
end
defp get_calendar(calendar) do
if Config.ensure_compiled?(calendar) and function_exported?(calendar, :cldr_calendar_type, 0) do
{:ok, calendar}
else
nil
end
end
defp calendar_error(calendar) do
{:error, {Cldr.UnknownCalendarError, calendar}}
end
end