@@ -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 """
0 commit comments