Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 54 additions & 8 deletions std/datetime/date.d
Original file line number Diff line number Diff line change
Expand Up @@ -8966,15 +8966,32 @@ public:


/++
Converts this $(LREF TimeOfDay) to a string with the format HHMMSS.
Converts this $(LREF TimeOfDay) to a string with the format `HHMMSS`.
If `writer` is set, the resulting string will be written directly to it.

Params:
writer = A `char` accepting $(REF_ALTTEXT output range, isOutputRange, std, range, primitives)
Returns:
A `string` when not using an output range; `void` otherwise.
+/
string toISOString() const @safe pure nothrow
{
import std.format : format;
import std.array : appender;
auto w = appender!string();
w.reserve(6);
toISOString(w);
return w.data;
}

/// ditto
void toISOString(W)(ref W writer) const
if (isOutputRange!(W, char))
{
import std.format : formattedWrite;
try
return format("%02d%02d%02d", _hour, _minute, _second);
formattedWrite(writer, "%02d%02d%02d", _hour, _minute, _second);
catch (Exception e)
assert(0, "format() threw.");
assert(0, "formattedWrite() threw.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off-topic

FYI there's a trick to avoid the red coverage here - from dlang/druntime#2057 (comment):

scope (failure) assert(0, "formattedWrite() threw.");
formattedWrite(writer, "%02d%02d%02d", _hour, _minute, _second);

Also I wonder whether formattedWrite can be made nothrow when the input string is passed in at CTFE and thus checked at compile-time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

///
Expand All @@ -8996,15 +9013,32 @@ public:


/++
Converts this $(LREF TimeOfDay) to a string with the format HH:MM:SS.
Converts this $(LREF TimeOfDay) to a string with the format `HH:MM:SS`.
If `writer` is set, the resulting string will be written directly to it.

Params:
writer = A `char` accepting $(REF_ALTTEXT output range, isOutputRange, std, range, primitives)
Returns:
A `string` when not using an output range; `void` otherwise.
+/
string toISOExtString() const @safe pure nothrow
{
import std.format : format;
import std.array : appender;
auto w = appender!string();
w.reserve(8);
toISOExtString(w);
return w.data;
}

/// ditto
void toISOExtString(W)(ref W writer) const
if (isOutputRange!(W, char))
{
import std.format : formattedWrite;
try
return format("%02d:%02d:%02d", _hour, _minute, _second);
formattedWrite(writer, "%02d:%02d:%02d", _hour, _minute, _second);
catch (Exception e)
assert(0, "format() threw.");
assert(0, "formattedWrite() threw.");
}

///
Expand Down Expand Up @@ -9047,12 +9081,24 @@ public:
`fromISOString` and `fromISOExtString`.

The format returned by toString may or may not change in the future.

Params:
writer = A `char` accepting $(REF_ALTTEXT output range, isOutputRange, std, range, primitives)
Returns:
A `string` when not using an output range; `void` otherwise.
+/
string toString() const @safe pure nothrow
{
return toISOExtString();
}

/// ditto
void toString(W)(ref W writer) const
if (isOutputRange!(W, char))
{
toISOExtString(writer);
}

@safe unittest
{
auto tod = TimeOfDay(12, 30, 33);
Expand Down