Skip to content

Commit dadb13d

Browse files
authored
Merge pull request #6071 from JackStouffer/timeofday-tostring
Work On Issue 10828 - Add output range versions of toString functions to std.datetime.TimeOfDay merged-on-behalf-of: Jack Stouffer <jack@jackstouffer.com>
2 parents aa7b6c0 + d5528a6 commit dadb13d

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

std/datetime/date.d

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8969,15 +8969,32 @@ public:
89698969

89708970

89718971
/++
8972-
Converts this $(LREF TimeOfDay) to a string with the format HHMMSS.
8972+
Converts this $(LREF TimeOfDay) to a string with the format `HHMMSS`.
8973+
If `writer` is set, the resulting string will be written directly to it.
8974+
8975+
Params:
8976+
writer = A `char` accepting $(REF_ALTTEXT output range, isOutputRange, std, range, primitives)
8977+
Returns:
8978+
A `string` when not using an output range; `void` otherwise.
89738979
+/
89748980
string toISOString() const @safe pure nothrow
89758981
{
8976-
import std.format : format;
8982+
import std.array : appender;
8983+
auto w = appender!string();
8984+
w.reserve(6);
8985+
toISOString(w);
8986+
return w.data;
8987+
}
8988+
8989+
/// ditto
8990+
void toISOString(W)(ref W writer) const
8991+
if (isOutputRange!(W, char))
8992+
{
8993+
import std.format : formattedWrite;
89778994
try
8978-
return format("%02d%02d%02d", _hour, _minute, _second);
8995+
formattedWrite(writer, "%02d%02d%02d", _hour, _minute, _second);
89798996
catch (Exception e)
8980-
assert(0, "format() threw.");
8997+
assert(0, "formattedWrite() threw.");
89818998
}
89828999

89839000
///
@@ -8999,15 +9016,32 @@ public:
89999016

90009017

90019018
/++
9002-
Converts this $(LREF TimeOfDay) to a string with the format HH:MM:SS.
9019+
Converts this $(LREF TimeOfDay) to a string with the format `HH:MM:SS`.
9020+
If `writer` is set, the resulting string will be written directly to it.
9021+
9022+
Params:
9023+
writer = A `char` accepting $(REF_ALTTEXT output range, isOutputRange, std, range, primitives)
9024+
Returns:
9025+
A `string` when not using an output range; `void` otherwise.
90039026
+/
90049027
string toISOExtString() const @safe pure nothrow
90059028
{
9006-
import std.format : format;
9029+
import std.array : appender;
9030+
auto w = appender!string();
9031+
w.reserve(8);
9032+
toISOExtString(w);
9033+
return w.data;
9034+
}
9035+
9036+
/// ditto
9037+
void toISOExtString(W)(ref W writer) const
9038+
if (isOutputRange!(W, char))
9039+
{
9040+
import std.format : formattedWrite;
90079041
try
9008-
return format("%02d:%02d:%02d", _hour, _minute, _second);
9042+
formattedWrite(writer, "%02d:%02d:%02d", _hour, _minute, _second);
90099043
catch (Exception e)
9010-
assert(0, "format() threw.");
9044+
assert(0, "formattedWrite() threw.");
90119045
}
90129046

90139047
///
@@ -9050,12 +9084,24 @@ public:
90509084
`fromISOString` and `fromISOExtString`.
90519085
90529086
The format returned by toString may or may not change in the future.
9087+
9088+
Params:
9089+
writer = A `char` accepting $(REF_ALTTEXT output range, isOutputRange, std, range, primitives)
9090+
Returns:
9091+
A `string` when not using an output range; `void` otherwise.
90539092
+/
90549093
string toString() const @safe pure nothrow
90559094
{
90569095
return toISOExtString();
90579096
}
90589097

9098+
/// ditto
9099+
void toString(W)(ref W writer) const
9100+
if (isOutputRange!(W, char))
9101+
{
9102+
toISOExtString(writer);
9103+
}
9104+
90599105
@safe unittest
90609106
{
90619107
auto tod = TimeOfDay(12, 30, 33);

0 commit comments

Comments
 (0)