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