Skip to content

Commit c32705a

Browse files
volcovjosevalim
authored andcommitted
Run the code formatter on Date (#6888)
1 parent 4ba6053 commit c32705a

File tree

1 file changed

+63
-40
lines changed

1 file changed

+63
-40
lines changed

lib/elixir/lib/calendar/date.ex

+63-40
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ defmodule Date do
5353
@enforce_keys [:year, :month, :day]
5454
defstruct [:year, :month, :day, calendar: Calendar.ISO]
5555

56-
@type t :: %Date{year: Calendar.year, month: Calendar.month,
57-
day: Calendar.day, calendar: Calendar.calendar}
56+
@type t :: %Date{
57+
year: Calendar.year(),
58+
month: Calendar.month(),
59+
day: Calendar.day(),
60+
calendar: Calendar.calendar()
61+
}
5862

5963
@doc """
6064
Returns a range of dates.
@@ -86,7 +90,7 @@ defmodule Date do
8690
-366
8791
"""
8892

89-
@spec range(Calendar.date, Calendar.date) :: Date.Range.t
93+
@spec range(Calendar.date(), Calendar.date()) :: Date.Range.t()
9094
def range(%{calendar: calendar} = first, %{calendar: calendar} = last) do
9195
{first_days, _} = to_iso_days(first)
9296
{last_days, _} = to_iso_days(last)
@@ -95,12 +99,11 @@ defmodule Date do
9599
first: first,
96100
last: last,
97101
first_in_iso_days: first_days,
98-
last_in_iso_days: last_days,
102+
last_in_iso_days: last_days
99103
}
100104
end
101105

102-
def range(%{calendar: _, year: _, month: _, day: _},
103-
%{calendar: _, year: _, month: _, day: _}) do
106+
def range(%{calendar: _, year: _, month: _, day: _}, %{calendar: _, year: _, month: _, day: _}) do
104107
raise ArgumentError, "both dates must have matching calendars"
105108
end
106109

@@ -114,18 +117,18 @@ defmodule Date do
114117
true
115118
116119
"""
117-
@spec utc_today(Calendar.calendar) :: t
120+
@spec utc_today(Calendar.calendar()) :: t
118121
def utc_today(calendar \\ Calendar.ISO)
119122

120123
def utc_today(Calendar.ISO) do
121-
{:ok, {year, month, day}, _, _} = Calendar.ISO.from_unix(System.os_time, :native)
124+
{:ok, {year, month, day}, _, _} = Calendar.ISO.from_unix(System.os_time(), :native)
122125
%Date{year: year, month: month, day: day}
123126
end
124127

125128
def utc_today(calendar) do
126129
calendar
127-
|> DateTime.utc_now
128-
|> DateTime.to_date
130+
|> DateTime.utc_now()
131+
|> DateTime.to_date()
129132
end
130133

131134
@doc """
@@ -145,7 +148,7 @@ defmodule Date do
145148
true
146149
147150
"""
148-
@spec leap_year?(Calendar.date) :: boolean()
151+
@spec leap_year?(Calendar.date()) :: boolean()
149152
def leap_year?(date)
150153

151154
def leap_year?(%{calendar: calendar, year: year}) do
@@ -165,7 +168,7 @@ defmodule Date do
165168
29
166169
167170
"""
168-
@spec days_in_month(Calendar.date) :: Calendar.day
171+
@spec days_in_month(Calendar.date()) :: Calendar.day()
169172
def days_in_month(date)
170173

171174
def days_in_month(%{calendar: calendar, year: year, month: month}) do
@@ -193,7 +196,7 @@ defmodule Date do
193196
{:error, :invalid_date}
194197
195198
"""
196-
@spec new(Calendar.year, Calendar.month, Calendar.day) :: {:ok, t} | {:error, atom}
199+
@spec new(Calendar.year(), Calendar.month(), Calendar.day()) :: {:ok, t} | {:error, atom}
197200
def new(year, month, day, calendar \\ Calendar.ISO) do
198201
if calendar.valid_date?(year, month, day) do
199202
{:ok, %Date{year: year, month: month, day: day, calendar: calendar}}
@@ -213,7 +216,7 @@ defmodule Date do
213216
"2000-02-28"
214217
215218
"""
216-
@spec to_string(Calendar.date) :: String.t
219+
@spec to_string(Calendar.date()) :: String.t()
217220
def to_string(date)
218221

219222
def to_string(%{calendar: calendar, year: year, month: month, day: day}) do
@@ -236,15 +239,14 @@ defmodule Date do
236239
{:error, :invalid_date}
237240
238241
"""
239-
@spec from_iso8601(String.t) :: {:ok, t} | {:error, atom}
242+
@spec from_iso8601(String.t()) :: {:ok, t} | {:error, atom}
240243
def from_iso8601(string, calendar \\ Calendar.ISO)
241244

242245
def from_iso8601(<<year::4-bytes, ?-, month::2-bytes, ?-, day::2-bytes>>, calendar) do
243246
with {year, ""} <- Integer.parse(year),
244247
{month, ""} <- Integer.parse(month),
245248
{day, ""} <- Integer.parse(day) do
246-
with {:ok, date} <- new(year, month, day, Calendar.ISO),
247-
do: convert(date, calendar)
249+
with {:ok, date} <- new(year, month, day, Calendar.ISO), do: convert(date, calendar)
248250
else
249251
_ -> {:error, :invalid_format}
250252
end
@@ -267,13 +269,14 @@ defmodule Date do
267269
iex> Date.from_iso8601!("2015:01:23")
268270
** (ArgumentError) cannot parse "2015:01:23" as date, reason: :invalid_format
269271
"""
270-
@spec from_iso8601!(String.t) :: t
272+
@spec from_iso8601!(String.t()) :: t
271273
def from_iso8601!(string, calendar \\ Calendar.ISO) do
272274
case from_iso8601(string, calendar) do
273275
{:ok, value} ->
274276
value
277+
275278
{:error, reason} ->
276-
raise ArgumentError, "cannot parse #{inspect string} as date, reason: #{inspect reason}"
279+
raise ArgumentError, "cannot parse #{inspect(string)} as date, reason: #{inspect(reason)}"
277280
end
278281
end
279282

@@ -300,7 +303,7 @@ defmodule Date do
300303
"2000-02-28"
301304
302305
"""
303-
@spec to_iso8601(Calendar.date, :extended | :basic) :: String.t
306+
@spec to_iso8601(Calendar.date(), :extended | :basic) :: String.t()
304307
def to_iso8601(date, format \\ :extended) when format in [:basic, :extended] do
305308
%{year: year, month: month, day: day} = convert!(date, Calendar.ISO)
306309
Calendar.ISO.date_to_iso8601(year, month, day, format)
@@ -322,7 +325,7 @@ defmodule Date do
322325
{2000, 1, 1}
323326
324327
"""
325-
@spec to_erl(Calendar.date) :: :calendar.date
328+
@spec to_erl(Calendar.date()) :: :calendar.date()
326329
def to_erl(date) do
327330
%{year: year, month: month, day: day} = convert!(date, Calendar.ISO)
328331
{year, month, day}
@@ -343,12 +346,11 @@ defmodule Date do
343346
{:error, :invalid_date}
344347
345348
"""
346-
@spec from_erl(:calendar.date) :: {:ok, t} | {:error, atom}
349+
@spec from_erl(:calendar.date()) :: {:ok, t} | {:error, atom}
347350
def from_erl(tuple, calendar \\ Calendar.ISO)
348351

349352
def from_erl({year, month, day}, calendar) do
350-
with {:ok, date} <- new(year, month, day, Calendar.ISO),
351-
do: convert(date, calendar)
353+
with {:ok, date} <- new(year, month, day, Calendar.ISO), do: convert(date, calendar)
352354
end
353355

354356
@doc """
@@ -362,13 +364,15 @@ defmodule Date do
362364
** (ArgumentError) cannot convert {2000, 13, 1} to date, reason: :invalid_date
363365
364366
"""
365-
@spec from_erl!(:calendar.date) :: t
367+
@spec from_erl!(:calendar.date()) :: t
366368
def from_erl!(tuple) do
367369
case from_erl(tuple) do
368370
{:ok, value} ->
369371
value
372+
370373
{:error, reason} ->
371-
raise ArgumentError, "cannot convert #{inspect tuple} to date, reason: #{inspect reason}"
374+
raise ArgumentError,
375+
"cannot convert #{inspect(tuple)} to date, reason: #{inspect(reason)}"
372376
end
373377
end
374378

@@ -395,15 +399,20 @@ defmodule Date do
395399
:eq
396400
397401
"""
398-
@spec compare(Calendar.date, Calendar.date) :: :lt | :eq | :gt
399-
def compare(%{calendar: calendar, year: year1, month: month1, day: day1},
400-
%{calendar: calendar, year: year2, month: month2, day: day2}) do
402+
@spec compare(Calendar.date(), Calendar.date()) :: :lt | :eq | :gt
403+
def compare(%{calendar: calendar, year: year1, month: month1, day: day1}, %{
404+
calendar: calendar,
405+
year: year2,
406+
month: month2,
407+
day: day2
408+
}) do
401409
case {{year1, month1, day1}, {year2, month2, day2}} do
402410
{first, second} when first > second -> :gt
403411
{first, second} when first < second -> :lt
404412
_ -> :eq
405413
end
406414
end
415+
407416
def compare(date1, date2) do
408417
if Calendar.compatible_calendars?(date1.calendar, date2.calendar) do
409418
case {to_iso_days(date1), to_iso_days(date2)} do
@@ -413,7 +422,7 @@ defmodule Date do
413422
end
414423
else
415424
raise ArgumentError, """
416-
cannot compare #{inspect date1} with #{inspect date2}.
425+
cannot compare #{inspect(date1)} with #{inspect(date2)}.
417426
418427
This comparison would be ambiguous as their calendars have incompatible day rollover moments.
419428
Specify an exact time of day (using `DateTime`s) to resolve this ambiguity
@@ -439,16 +448,19 @@ defmodule Date do
439448
{:ok, %Date{calendar: Calendar.Holocene, year: 12000, month: 1, day: 1}}
440449
441450
"""
442-
@spec convert(Calendar.date, Calendar.calendar) :: {:ok, t} | {:error, :incompatible_calendars}
451+
@spec convert(Calendar.date(), Calendar.calendar()) ::
452+
{:ok, t} | {:error, :incompatible_calendars}
443453
def convert(%{calendar: calendar, year: year, month: month, day: day}, calendar) do
444454
{:ok, %Date{calendar: calendar, year: year, month: month, day: day}}
445455
end
456+
446457
def convert(%{calendar: calendar} = date, target_calendar) do
447458
if Calendar.compatible_calendars?(calendar, target_calendar) do
448459
result_date =
449460
date
450461
|> to_iso_days()
451462
|> from_iso_days(target_calendar)
463+
452464
{:ok, result_date}
453465
else
454466
{:error, :incompatible_calendars}
@@ -469,13 +481,17 @@ defmodule Date do
469481
%Date{calendar: Calendar.Holocene, year: 12000, month: 1, day: 1}
470482
471483
"""
472-
@spec convert!(Calendar.date, Calendar.calendar) :: t
484+
@spec convert!(Calendar.date(), Calendar.calendar()) :: t
473485
def convert!(date, calendar) do
474486
case convert(date, calendar) do
475487
{:ok, value} ->
476488
value
489+
477490
{:error, reason} ->
478-
raise ArgumentError, "cannot convert #{inspect date} to target calendar #{inspect calendar}, reason: #{inspect reason}"
491+
raise ArgumentError,
492+
"cannot convert #{inspect(date)} to target calendar #{inspect(calendar)}, reason: #{
493+
inspect(reason)
494+
}"
479495
end
480496
end
481497

@@ -496,7 +512,7 @@ defmodule Date do
496512
~D[2000-01-03]
497513
498514
"""
499-
@spec add(Calendar.date, integer()) :: t
515+
@spec add(Calendar.date(), integer()) :: t
500516
def add(%{calendar: calendar} = date, days) do
501517
{iso_days, fraction} = to_iso_days(date)
502518
from_iso_days({iso_days + days, fraction}, calendar)
@@ -520,9 +536,13 @@ defmodule Date do
520536
-2
521537
522538
"""
523-
@spec diff(Calendar.date, Calendar.date) :: integer
524-
def diff(%{calendar: Calendar.ISO, year: year1, month: month1, day: day1},
525-
%{calendar: Calendar.ISO, year: year2, month: month2, day: day2}) do
539+
@spec diff(Calendar.date(), Calendar.date()) :: integer
540+
def diff(%{calendar: Calendar.ISO, year: year1, month: month1, day: day1}, %{
541+
calendar: Calendar.ISO,
542+
year: year2,
543+
month: month2,
544+
day: day2
545+
}) do
526546
Calendar.ISO.date_to_iso_days(year1, month1, day1) -
527547
Calendar.ISO.date_to_iso_days(year2, month2, day2)
528548
end
@@ -533,13 +553,15 @@ defmodule Date do
533553
{days2, _} = to_iso_days(date2)
534554
days1 - days2
535555
else
536-
raise ArgumentError, "cannot calculate the difference between #{inspect date1} and #{inspect date2} because their calendars are not compatible and thus the result would be ambiguous"
556+
raise ArgumentError,
557+
"cannot calculate the difference between #{inspect(date1)} and #{inspect(date2)} because their calendars are not compatible and thus the result would be ambiguous"
537558
end
538559
end
539560

540561
defp to_iso_days(%{calendar: Calendar.ISO, year: year, month: month, day: day}) do
541-
{Calendar.ISO.date_to_iso_days(year, month, day), {0, 86400000000}}
562+
{Calendar.ISO.date_to_iso_days(year, month, day), {0, 86_400_000_000}}
542563
end
564+
543565
defp to_iso_days(%{calendar: calendar, year: year, month: month, day: day}) do
544566
calendar.naive_datetime_to_iso_days(year, month, day, 0, 0, 0, {0, 0})
545567
end
@@ -548,6 +570,7 @@ defmodule Date do
548570
{year, month, day} = Calendar.ISO.date_from_iso_days(days)
549571
%Date{year: year, month: month, day: day, calendar: Calendar.ISO}
550572
end
573+
551574
defp from_iso_days(iso_days, target_calendar) do
552575
{year, month, day, _, _, _, _} = target_calendar.naive_datetime_from_iso_days(iso_days)
553576
%Date{year: year, month: month, day: day, calendar: target_calendar}
@@ -570,7 +593,7 @@ defmodule Date do
570593
2
571594
572595
"""
573-
@spec day_of_week(Calendar.date) :: non_neg_integer()
596+
@spec day_of_week(Calendar.date()) :: non_neg_integer()
574597
def day_of_week(date)
575598

576599
def day_of_week(%{calendar: calendar, year: year, month: month, day: day}) do

0 commit comments

Comments
 (0)