Skip to content

Commit 284a7d0

Browse files
committed
Improve error when NaiveDateTime given to DateTime.to_iso8601/2
Closes #6503.
1 parent a31549c commit 284a7d0

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

lib/elixir/lib/calendar/datetime.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,25 +326,25 @@ defmodule DateTime do
326326
@spec to_iso8601(Calendar.datetime, :extended | :basic ) :: String.t
327327
def to_iso8601(datetime, format \\ :extended)
328328

329+
def to_iso8601(_, format) when format not in [:extended, :basic] do
330+
raise ArgumentError, "DateTime.to_iso8601/2 expects format to be :extended or :basic, got: #{inspect format}"
331+
end
332+
329333
def to_iso8601(%{calendar: Calendar.ISO, year: year, month: month, day: day,
330334
hour: hour, minute: minute, second: second, microsecond: microsecond,
331-
time_zone: time_zone, zone_abbr: zone_abbr, utc_offset: utc_offset, std_offset: std_offset}, format) when format in [:extended, :basic] do
335+
time_zone: time_zone, zone_abbr: zone_abbr, utc_offset: utc_offset, std_offset: std_offset}, format) do
332336
Calendar.ISO.datetime_to_iso8601(year, month, day, hour, minute, second, microsecond,
333337
time_zone, zone_abbr, utc_offset, std_offset, format)
334338
end
335339

336340
def to_iso8601(%{calendar: _, year: _, month: _, day: _,
337341
hour: _, minute: _, second: _, microsecond: _,
338-
time_zone: _, zone_abbr: _, utc_offset: _, std_offset: _} = datetime, format) when format in [:extended, :basic] do
342+
time_zone: _, zone_abbr: _, utc_offset: _, std_offset: _} = datetime, format) do
339343
datetime
340344
|> convert!(Calendar.ISO)
341345
|> to_iso8601(format)
342346
end
343347

344-
def to_iso8601(_, format) do
345-
raise ArgumentError, "DateTime.to_iso8601/2 expects format to be :extended or :basic, got: #{inspect format}"
346-
end
347-
348348
@doc """
349349
Parses the extended "Date and time of day" format described by
350350
[ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).

0 commit comments

Comments
 (0)