Skip to content

Commit 590c8ae

Browse files
committed
initial changes
1 parent 984620a commit 590c8ae

File tree

6 files changed

+122
-30
lines changed

6 files changed

+122
-30
lines changed

lib/elixir/lib/calendar/date.ex

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,15 +409,24 @@ defmodule Date do
409409
@spec to_iso8601(Calendar.date(), :extended | :basic) :: String.t()
410410
def to_iso8601(date, format \\ :extended)
411411

412-
def to_iso8601(%{calendar: Calendar.ISO} = date, format) when format in [:basic, :extended] do
412+
def to_iso8601(date, format) do
413+
to_iso8601_iodata(date, format)
414+
|> IO.iodata_to_binary()
415+
end
416+
417+
@spec to_iso8601_iodata(Calendar.date(), :extended | :basic) :: iodata
418+
def to_iso8601_iodata(date, format \\ :extended)
419+
420+
def to_iso8601_iodata(%{calendar: Calendar.ISO} = date, format)
421+
when format in [:basic, :extended] do
413422
%{year: year, month: month, day: day} = date
414-
Calendar.ISO.date_to_string(year, month, day, format)
423+
Calendar.ISO.date_to_iodata(year, month, day, format)
415424
end
416425

417-
def to_iso8601(%{calendar: _} = date, format) when format in [:basic, :extended] do
426+
def to_iso8601_iodata(%{calendar: _} = date, format) when format in [:basic, :extended] do
418427
date
419428
|> convert!(Calendar.ISO)
420-
|> to_iso8601()
429+
|> to_iso8601_iodata()
421430
end
422431

423432
@doc """

lib/elixir/lib/calendar/datetime.ex

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,36 @@ 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(datetime, format, offset) do
1096+
to_iso8601_iodata(datetime, format, offset)
1097+
|> IO.iodata_to_binary()
1098+
end
1099+
1100+
@spec to_iso8601_iodata(Calendar.datetime(), :basic | :extended, nil | integer()) :: iodata
1101+
def to_iso8601_iodata(datetime, format \\ :extended, offset \\ nil)
1102+
1103+
def to_iso8601_iodata(%{calendar: Calendar.ISO} = datetime, format, nil)
1104+
when format in [:extended, :basic] do
1105+
%{
1106+
year: year,
1107+
month: month,
1108+
day: day,
1109+
hour: hour,
1110+
minute: minute,
1111+
second: second,
1112+
microsecond: microsecond,
1113+
time_zone: time_zone,
1114+
utc_offset: utc_offset,
1115+
std_offset: std_offset
1116+
} = datetime
1117+
1118+
[
1119+
datetime_to_iodata(year, month, day, hour, minute, second, microsecond, format),
1120+
Calendar.ISO.offset_to_iodata(utc_offset, std_offset, time_zone, format)
1121+
]
1122+
end
1123+
1124+
def to_iso8601_iodata(%{calendar: Calendar.ISO} = datetime, format, nil)
10961125
when format in [:extended, :basic] do
10971126
%{
10981127
year: year,
@@ -1107,35 +1136,60 @@ defmodule DateTime do
11071136
std_offset: std_offset
11081137
} = datetime
11091138

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)
1139+
[
1140+
datetime_to_iodata(year, month, day, hour, minute, second, microsecond, format),
1141+
Calendar.ISO.offset_to_iodata(utc_offset, std_offset, time_zone, format)
1142+
]
11121143
end
11131144

1114-
def to_iso8601(
1145+
def to_iso8601_iodata(
11151146
%{calendar: Calendar.ISO, microsecond: {_, precision}, time_zone: "Etc/UTC"} = datetime,
11161147
format,
11171148
0
11181149
)
11191150
when format in [:extended, :basic] do
11201151
{year, month, day, hour, minute, second, {microsecond, _}} = shift_by_offset(datetime, 0)
11211152

1122-
datetime_to_string(year, month, day, hour, minute, second, {microsecond, precision}, format) <>
1123-
"Z"
1153+
[
1154+
datetime_to_iodata(
1155+
year,
1156+
month,
1157+
day,
1158+
hour,
1159+
minute,
1160+
second,
1161+
{microsecond, precision},
1162+
format
1163+
),
1164+
?Z
1165+
]
11241166
end
11251167

1126-
def to_iso8601(%{calendar: Calendar.ISO} = datetime, format, offset)
1168+
def to_iso8601_iodata(%{calendar: Calendar.ISO} = datetime, format, offset)
11271169
when format in [:extended, :basic] do
11281170
{_, precision} = datetime.microsecond
11291171
{year, month, day, hour, minute, second, {microsecond, _}} = shift_by_offset(datetime, offset)
11301172

1131-
datetime_to_string(year, month, day, hour, minute, second, {microsecond, precision}, format) <>
1132-
Calendar.ISO.offset_to_string(offset, 0, nil, format)
1173+
[
1174+
datetime_to_iodata(
1175+
year,
1176+
month,
1177+
day,
1178+
hour,
1179+
minute,
1180+
second,
1181+
{microsecond, precision},
1182+
format
1183+
),
1184+
Calendar.ISO.offset_to_iodata(offset, 0, nil, format)
1185+
]
11331186
end
11341187

1135-
def to_iso8601(%{calendar: _} = datetime, format, offset) when format in [:extended, :basic] do
1188+
def to_iso8601_iodata(%{calendar: _} = datetime, format, offset)
1189+
when format in [:extended, :basic] do
11361190
datetime
11371191
|> convert!(Calendar.ISO)
1138-
|> to_iso8601(format, offset)
1192+
|> to_iso8601_iodata(format, offset)
11391193
end
11401194

11411195
defp shift_by_offset(%{calendar: calendar} = datetime, offset) do
@@ -1148,10 +1202,12 @@ defmodule DateTime do
11481202
|> calendar.naive_datetime_from_iso_days()
11491203
end
11501204

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)
1205+
defp datetime_to_iodata(year, month, day, hour, minute, second, microsecond, format) do
1206+
[
1207+
Calendar.ISO.date_to_iodata(year, month, day, format),
1208+
?T,
1209+
Calendar.ISO.time_to_iodata(hour, minute, second, microsecond, format)
1210+
]
11551211
end
11561212

11571213
@doc """

lib/elixir/lib/calendar/duration.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ defmodule Duration do
532532
sign,
533533
Integer.to_string(second),
534534
?.,
535-
ms |> Integer.to_string() |> String.pad_leading(6, "0") |> binary_part(0, p)
535+
Calendar.ISO.microseconds_to_iodata(ms, p)
536536
]
537537
end
538538

lib/elixir/lib/calendar/naive_datetime.ex

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,15 @@ 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(naive_datetime, format) do
932+
to_iso8601_iodata(naive_datetime, format)
933+
|> IO.iodata_to_binary()
934+
end
935+
936+
@spec to_iso8601_iodata(Calendar.naive_datetime(), :basic | :extended) :: iodata
937+
def to_iso8601_iodata(naive_datetime, format \\ :extended)
938+
939+
def to_iso8601_iodata(%{calendar: Calendar.ISO} = naive_datetime, format)
932940
when format in [:basic, :extended] do
933941
%{
934942
year: year,
@@ -940,14 +948,18 @@ defmodule NaiveDateTime do
940948
microsecond: microsecond
941949
} = naive_datetime
942950

943-
Calendar.ISO.date_to_string(year, month, day, format) <>
944-
"T" <> Calendar.ISO.time_to_string(hour, minute, second, microsecond, format)
951+
[
952+
Calendar.ISO.date_to_iodata(year, month, day, format),
953+
?T,
954+
Calendar.ISO.time_to_iodata(hour, minute, second, microsecond, format)
955+
]
945956
end
946957

947-
def to_iso8601(%{calendar: _} = naive_datetime, format) when format in [:basic, :extended] do
958+
def to_iso8601_iodata(%{calendar: _} = naive_datetime, format)
959+
when format in [:basic, :extended] do
948960
naive_datetime
949961
|> convert!(Calendar.ISO)
950-
|> to_iso8601(format)
962+
|> to_iso8601_iodata(format)
951963
end
952964

953965
@doc """

lib/elixir/lib/calendar/time.ex

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,20 +313,29 @@ defmodule Time do
313313
def to_iso8601(time, format \\ :extended)
314314

315315
def to_iso8601(%{calendar: Calendar.ISO} = time, format) when format in [:extended, :basic] do
316+
to_iso8601_iodata(time, format)
317+
|> IO.iodata_to_binary()
318+
end
319+
320+
@spec to_iso8601_iodata(Calendar.time(), :extended | :basic) :: iodata
321+
def to_iso8601_iodata(time, format \\ :extended)
322+
323+
def to_iso8601_iodata(%{calendar: Calendar.ISO} = time, format)
324+
when format in [:extended, :basic] do
316325
%{
317326
hour: hour,
318327
minute: minute,
319328
second: second,
320329
microsecond: microsecond
321330
} = time
322331

323-
Calendar.ISO.time_to_string(hour, minute, second, microsecond, format)
332+
Calendar.ISO.time_to_iodata(hour, minute, second, microsecond, format)
324333
end
325334

326-
def to_iso8601(%{calendar: _} = time, format) when format in [:extended, :basic] do
335+
def to_iso8601_iodata(%{calendar: _} = time, format) when format in [:extended, :basic] do
327336
time
328337
|> convert!(Calendar.ISO)
329-
|> to_iso8601(format)
338+
|> to_iso8601_iodata(format)
330339
end
331340

332341
@doc """

lib/elixir/lib/json.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,15 @@ defimpl JSON.Encoder, for: Map do
150150
end
151151
end
152152

153-
defimpl JSON.Encoder, for: [Date, Time, NaiveDateTime, DateTime, Duration] do
153+
defimpl JSON.Encoder, for: Duration do
154154
def encode(value, _encoder) do
155-
[?", @for.to_iso8601(value), ?"]
155+
[?", Duration.to_iso8601(value), ?"]
156+
end
157+
end
158+
159+
defimpl JSON.Encoder, for: [Date, Time, NaiveDateTime, DateTime] do
160+
def encode(value, _encoder) do
161+
[?", @for.to_iso8601_iodata(value), ?"]
156162
end
157163
end
158164

0 commit comments

Comments
 (0)