Skip to content

Commit dc45abe

Browse files
committed
refcator datetime modules
1 parent 984620a commit dc45abe

File tree

2 files changed

+77
-34
lines changed

2 files changed

+77
-34
lines changed

lib/elixir/lib/calendar/datetime.ex

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,8 +1092,21 @@ defmodule DateTime do
10921092
@spec to_iso8601(Calendar.datetime(), :basic | :extended, nil | integer()) :: String.t()
10931093
def to_iso8601(datetime, format \\ :extended, offset \\ nil)
10941094

1095-
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format, nil)
1095+
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format, offset) do
1096+
datetime
1097+
|> to_iso8601_iodata(format, offset)
1098+
|> IO.iodata_to_binary()
1099+
end
1100+
1101+
def to_iso8601(%{calendar: _} = datetime, format, offset)
10961102
when format in [:extended, :basic] do
1103+
datetime
1104+
|> convert!(Calendar.ISO)
1105+
|> to_iso8601(format, offset)
1106+
end
1107+
1108+
defp to_iso8601_iodata(datetime, format, nil)
1109+
when format in [:extended, :basic] do
10971110
%{
10981111
year: year,
10991112
month: month,
@@ -1107,35 +1120,53 @@ defmodule DateTime do
11071120
std_offset: std_offset
11081121
} = datetime
11091122

1110-
datetime_to_string(year, month, day, hour, minute, second, microsecond, format) <>
1111-
Calendar.ISO.offset_to_string(utc_offset, std_offset, time_zone, format)
1123+
[
1124+
datetime_to_iodata(year, month, day, hour, minute, second, microsecond, format),
1125+
Calendar.ISO.offset_to_iodata(utc_offset, std_offset, time_zone, format)
1126+
]
11121127
end
11131128

1114-
def to_iso8601(
1115-
%{calendar: Calendar.ISO, microsecond: {_, precision}, time_zone: "Etc/UTC"} = datetime,
1116-
format,
1117-
0
1118-
)
1119-
when format in [:extended, :basic] do
1129+
defp to_iso8601_iodata(
1130+
%{microsecond: {_, precision}, time_zone: "Etc/UTC"} = datetime,
1131+
format,
1132+
0
1133+
)
1134+
when format in [:extended, :basic] do
11201135
{year, month, day, hour, minute, second, {microsecond, _}} = shift_by_offset(datetime, 0)
11211136

1122-
datetime_to_string(year, month, day, hour, minute, second, {microsecond, precision}, format) <>
1123-
"Z"
1137+
[
1138+
datetime_to_iodata(
1139+
year,
1140+
month,
1141+
day,
1142+
hour,
1143+
minute,
1144+
second,
1145+
{microsecond, precision},
1146+
format
1147+
),
1148+
?Z
1149+
]
11241150
end
11251151

1126-
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format, offset)
1127-
when format in [:extended, :basic] do
1152+
defp to_iso8601_iodata(datetime, format, offset)
1153+
when format in [:extended, :basic] do
11281154
{_, precision} = datetime.microsecond
11291155
{year, month, day, hour, minute, second, {microsecond, _}} = shift_by_offset(datetime, offset)
11301156

1131-
datetime_to_string(year, month, day, hour, minute, second, {microsecond, precision}, format) <>
1132-
Calendar.ISO.offset_to_string(offset, 0, nil, format)
1133-
end
1134-
1135-
def to_iso8601(%{calendar: _} = datetime, format, offset) when format in [:extended, :basic] do
1136-
datetime
1137-
|> convert!(Calendar.ISO)
1138-
|> to_iso8601(format, offset)
1157+
[
1158+
datetime_to_iodata(
1159+
year,
1160+
month,
1161+
day,
1162+
hour,
1163+
minute,
1164+
second,
1165+
{microsecond, precision},
1166+
format
1167+
),
1168+
Calendar.ISO.offset_to_iodata(offset, 0, nil, format)
1169+
]
11391170
end
11401171

11411172
defp shift_by_offset(%{calendar: calendar} = datetime, offset) do
@@ -1148,10 +1179,12 @@ defmodule DateTime do
11481179
|> calendar.naive_datetime_from_iso_days()
11491180
end
11501181

1151-
defp datetime_to_string(year, month, day, hour, minute, second, microsecond, format) do
1152-
Calendar.ISO.date_to_string(year, month, day, format) <>
1153-
"T" <>
1154-
Calendar.ISO.time_to_string(hour, minute, second, microsecond, format)
1182+
defp datetime_to_iodata(year, month, day, hour, minute, second, microsecond, format) do
1183+
[
1184+
Calendar.ISO.date_to_iodata(year, month, day, format),
1185+
?T,
1186+
Calendar.ISO.time_to_iodata(hour, minute, second, microsecond, format)
1187+
]
11551188
end
11561189

11571190
@doc """

lib/elixir/lib/calendar/naive_datetime.ex

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,21 @@ defmodule NaiveDateTime do
928928
@spec to_iso8601(Calendar.naive_datetime(), :basic | :extended) :: String.t()
929929
def to_iso8601(naive_datetime, format \\ :extended)
930930

931-
def to_iso8601(%{calendar: Calendar.ISO} = naive_datetime, format)
931+
def to_iso8601(%{calendar: Calendar.ISO} = naive_datetime, format) do
932+
naive_datetime
933+
|> to_iso8601_iodata(format)
934+
|> IO.iodata_to_binary()
935+
end
936+
937+
def to_iso8601(%{calendar: _} = naive_datetime, format)
932938
when format in [:basic, :extended] do
939+
naive_datetime
940+
|> convert!(Calendar.ISO)
941+
|> to_iso8601(format)
942+
end
943+
944+
defp to_iso8601_iodata(naive_datetime, format)
945+
when format in [:basic, :extended] do
933946
%{
934947
year: year,
935948
month: month,
@@ -940,14 +953,11 @@ defmodule NaiveDateTime do
940953
microsecond: microsecond
941954
} = naive_datetime
942955

943-
Calendar.ISO.date_to_string(year, month, day, format) <>
944-
"T" <> Calendar.ISO.time_to_string(hour, minute, second, microsecond, format)
945-
end
946-
947-
def to_iso8601(%{calendar: _} = naive_datetime, format) when format in [:basic, :extended] do
948-
naive_datetime
949-
|> convert!(Calendar.ISO)
950-
|> to_iso8601(format)
956+
[
957+
Calendar.ISO.date_to_iodata(year, month, day, format),
958+
?T,
959+
Calendar.ISO.time_to_iodata(hour, minute, second, microsecond, format)
960+
]
951961
end
952962

953963
@doc """

0 commit comments

Comments
 (0)