Skip to content

Commit 06b37c9

Browse files
authored
Merge pull request #6176 from JackStouffer/systime-tostring
Optimized fracSecsToISOString and added writer overload merged-on-behalf-of: Jonathan M Davis <[email protected]>
2 parents e8fe4e7 + a29b28d commit 06b37c9

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

std/datetime/systime.d

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10263,24 +10263,32 @@ private:
1026310263
/+
1026410264
Returns the given hnsecs as an ISO string of fractional seconds.
1026510265
+/
10266-
static string fracSecsToISOString(int hnsecs) @safe pure nothrow
10266+
string fracSecsToISOString(int hnsecs) @safe pure nothrow
1026710267
{
10268-
assert(hnsecs >= 0);
10269-
10268+
import std.array : appender;
10269+
auto w = appender!string();
1027010270
try
10271-
{
10272-
if (hnsecs == 0)
10273-
return "";
10271+
fracSecsToISOString(w, hnsecs);
10272+
catch (Exception e)
10273+
assert(0, "fracSecsToISOString() threw.");
10274+
return w.data;
10275+
}
1027410276

10275-
string isoString = format(".%07d", hnsecs);
10277+
void fracSecsToISOString(W)(ref W writer, int hnsecs)
10278+
{
10279+
import std.conv : toChars;
10280+
import std.range : padLeft;
1027610281

10277-
while (isoString[$ - 1] == '0')
10278-
isoString.popBack();
10282+
assert(hnsecs >= 0);
1027910283

10280-
return isoString;
10281-
}
10282-
catch (Exception e)
10283-
assert(0, "format() threw.");
10284+
if (hnsecs == 0)
10285+
return;
10286+
10287+
put(writer, '.');
10288+
auto chars = hnsecs.toChars.padLeft('0', 7);
10289+
while (chars.back == '0')
10290+
chars.popBack();
10291+
put(writer, chars);
1028410292
}
1028510293

1028610294
@safe unittest

0 commit comments

Comments
 (0)