Skip to content

Commit fdb1e71

Browse files
Optimize Time#to_s (#16042)
Avoid parsing the time format on every call.
1 parent 43c7dfc commit fdb1e71

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/time.cr

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,11 +1112,15 @@ struct Time
11121112
# When the location is `UTC`, the offset is replaced with the string `UTC`.
11131113
# Offset seconds are omitted if `0`.
11141114
def to_s(io : IO) : Nil
1115-
to_s(io, "%F %T ")
1115+
formatter = Format::Formatter.new(self, io)
1116+
formatter.year_month_day
1117+
io << " "
1118+
formatter.twenty_four_hour_time_with_seconds
11161119

11171120
if utc?
1118-
io << "UTC"
1121+
io << " UTC"
11191122
else
1123+
io << " "
11201124
zone.format(io)
11211125
end
11221126
end

0 commit comments

Comments
 (0)