View source code
Display the source code in core/time.d from which this
page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a
Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
core.time.Duration.toString
- multiple declarations
Function Duration.toString
Converts this Duration
to a string
.
void toString(SinkT)
(
scope SinkT sink
) const scope;
The string is meant to be human readable, not machine parseable (e.g.
whether there is an 's'
on the end of the unit name usually depends on
whether it's plural or not, and empty units are not included unless the
Duration is zero
). Any code needing a specific string format should
use total
or split
to get the units needed to create the desired
string format and create the string itself.
The format returned by toString may or may not change in the future.
Parameters
Name | Description |
---|---|
sink | A sink object, expected to be a delegate or aggregate
implementing opCall that accepts a scope const(char)[]
as argument. |
Function Duration.toString
Returns the total number of the given units in this Duration
.
So, unlike split
, it does not strip out the larger units.
string toString() pure nothrow @safe const;
Example
writeln(dur!"weeks"(12) .total!"weeks"); // 12
writeln(dur!"weeks"(12) .total!"days"); // 84
writeln(dur!"days"(13) .total!"weeks"); // 1
writeln(dur!"days"(13) .total!"days"); // 13
writeln(dur!"hours"(49) .total!"days"); // 2
writeln(dur!"hours"(49) .total!"hours"); // 49
writeln(dur!"nsecs"(2007) .total!"hnsecs"); // 20
writeln(dur!"nsecs"(2007) .total!"nsecs"); // 2000
Example
writeln(Duration .zero .toString()); // "0 hnsecs"
writeln(weeks(5) .toString()); // "5 weeks"
writeln(days(2) .toString()); // "2 days"
writeln(hours(1) .toString()); // "1 hour"
writeln(minutes(19) .toString()); // "19 minutes"
writeln(seconds(42) .toString()); // "42 secs"
writeln(msecs(42) .toString()); // "42 ms"
writeln(usecs(27) .toString()); // "27 μs"
writeln(hnsecs(5) .toString()); // "5 hnsecs"
writeln(seconds(121) .toString()); // "2 minutes and 1 sec"
assert((minutes(5) + seconds(3) + usecs(4)) .toString() ==
"5 minutes, 3 secs, and 4 μs");
writeln(seconds(-42) .toString()); // "-42 secs"
writeln(usecs(-5239492) .toString()); // "-5 secs, -239 ms, and -492 μs"
Authors
Jonathan M Davis and Kato Shoichi
License
Copyright © 1999-2024 by the D Language Foundation | Page generated by ddox.