@@ -1092,7 +1092,36 @@ defmodule DateTime do
1092
1092
@ spec to_iso8601 ( Calendar . datetime ( ) , :basic | :extended , nil | integer ( ) ) :: String . t ( )
1093
1093
def to_iso8601 ( datetime , format \\ :extended , offset \\ nil )
1094
1094
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 )
1096
1125
when format in [ :extended , :basic ] do
1097
1126
% {
1098
1127
year: year ,
@@ -1107,35 +1136,60 @@ defmodule DateTime do
1107
1136
std_offset: std_offset
1108
1137
} = datetime
1109
1138
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
+ ]
1112
1143
end
1113
1144
1114
- def to_iso8601 (
1145
+ def to_iso8601_iodata (
1115
1146
% { calendar: Calendar.ISO , microsecond: { _ , precision } , time_zone: "Etc/UTC" } = datetime ,
1116
1147
format ,
1117
1148
0
1118
1149
)
1119
1150
when format in [ :extended , :basic ] do
1120
1151
{ year , month , day , hour , minute , second , { microsecond , _ } } = shift_by_offset ( datetime , 0 )
1121
1152
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
+ ]
1124
1166
end
1125
1167
1126
- def to_iso8601 ( % { calendar: Calendar.ISO } = datetime , format , offset )
1168
+ def to_iso8601_iodata ( % { calendar: Calendar.ISO } = datetime , format , offset )
1127
1169
when format in [ :extended , :basic ] do
1128
1170
{ _ , precision } = datetime . microsecond
1129
1171
{ year , month , day , hour , minute , second , { microsecond , _ } } = shift_by_offset ( datetime , offset )
1130
1172
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
+ ]
1133
1186
end
1134
1187
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
1136
1190
datetime
1137
1191
|> convert! ( Calendar.ISO )
1138
- |> to_iso8601 ( format , offset )
1192
+ |> to_iso8601_iodata ( format , offset )
1139
1193
end
1140
1194
1141
1195
defp shift_by_offset ( % { calendar: calendar } = datetime , offset ) do
@@ -1148,10 +1202,12 @@ defmodule DateTime do
1148
1202
|> calendar . naive_datetime_from_iso_days ( )
1149
1203
end
1150
1204
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
+ ]
1155
1211
end
1156
1212
1157
1213
@ doc """
0 commit comments