Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Added zero property to Duration, TickDuration, and FracSec.
Browse files Browse the repository at this point in the history
It makes it more pleasant to get those types with a value of zero that
way.
  • Loading branch information
jmdavis committed Nov 11, 2012
1 parent b412e3d commit f9c5f72
Showing 1 changed file with 43 additions and 13 deletions.
56 changes: 43 additions & 13 deletions src/core/time.d
Expand Up @@ -109,6 +109,17 @@ struct Duration

public:

/++
A $(D Duration) of $(D 0). It's shorter than doing something like
$(D dur!"seconds"(0)) and more explicit than $(D Duration.init).
+/
static @property @safe pure nothrow Duration zero() { return Duration(0); }

unittest
{
assert(zero == dur!"seconds"(0));
}

/++
Compares this $(D Duration) with the given $(D Duration).
Expand Down Expand Up @@ -1450,6 +1461,19 @@ struct TickDuration
static immutable TickDuration appOrigin;


/++
It's the same as $(D TickDuration(0)), but it's provided to be
consistent with $(D Duration) and $(D FracSec), which provide $(D zero)
properties.
+/
static @property @safe pure nothrow TickDuration zero() { return TickDuration(0); }

unittest
{
assert(zero == TickDuration(0));
}


@trusted shared static this()
{
version(Windows)
Expand Down Expand Up @@ -2303,6 +2327,18 @@ struct FracSec
{
public:

/++
A $(D FracSec) of $(D 0). It's shorter than doing something like
$(D FracSec.from!"msecs"(0)) and more explicit than $(D FracSec.init).
+/
static @property @safe pure nothrow FracSec zero() { return FracSec(0); }

unittest
{
assert(zero == FracSec.from!"msecs"(0));
}


/++
Create a $(D FracSec) from the given units ($(D "msecs"), $(D "usecs"),
or $(D "hnsecs")).
Expand All @@ -2322,7 +2358,9 @@ public:
units == "hnsecs" ||
units == "nsecs")
{
return FracSec(cast(int)convert!(units, "hnsecs")(value));
immutable hnsecs = cast(int)convert!(units, "hnsecs")(value);
_enforceValid(hnsecs);
return FracSec(hnsecs);
}

unittest
Expand Down Expand Up @@ -2368,10 +2406,7 @@ public:
FracSec opUnary(string op)() @safe const pure nothrow
if(op == "-")
{
try
return FracSec(-_hnsecs);
catch(Exception e)
assert(0, "FracSec's constructor threw.");
return FracSec(-_hnsecs);
}

unittest
Expand Down Expand Up @@ -2425,7 +2460,6 @@ public:
@property void msecs(int milliseconds) @safe pure
{
immutable hnsecs = cast(int)convert!("msecs", "hnsecs")(milliseconds);

_enforceValid(hnsecs);
_hnsecs = hnsecs;
}
Expand Down Expand Up @@ -2498,7 +2532,6 @@ public:
@property void usecs(int microseconds) @safe pure
{
immutable hnsecs = cast(int)convert!("usecs", "hnsecs")(microseconds);

_enforceValid(hnsecs);
_hnsecs = hnsecs;
}
Expand Down Expand Up @@ -2651,7 +2684,6 @@ public:
@property void nsecs(long nsecs) @safe pure
{
immutable hnsecs = cast(int)convert!("nsecs", "hnsecs")(nsecs);

_enforceValid(hnsecs);
_hnsecs = hnsecs;
}
Expand Down Expand Up @@ -2830,8 +2862,7 @@ private:
+/
static bool _valid(int hnsecs) @safe pure
{
enum second = convert!("seconds", "hnsecs")(1);

immutable second = convert!("seconds", "hnsecs")(1);
return hnsecs > -second && hnsecs < second;
}

Expand All @@ -2855,9 +2886,8 @@ private:
$(D TimeException) if the given hnsecs less than 0 or would result
in a $(D FracSec) not within the range (-1 second, 1 second).
+/
@safe pure this(int hnsecs)
@safe pure nothrow this(int hnsecs)
{
_enforceValid(hnsecs);
_hnsecs = hnsecs;
}

Expand Down Expand Up @@ -3289,7 +3319,7 @@ version(unittest) void _assertThrown(T : Throwable = Exception, E)
{
immutable tail = msg.length == 0 ? "." : ": " ~ msg;

throw new AssertError("assertThrown() failed: No " ~ E.stringof ~ " was thrown" ~ tail, file, line);
throw new AssertError("assertThrown() failed: No " ~ T.stringof ~ " was thrown" ~ tail, file, line);
}
}

Expand Down

0 comments on commit f9c5f72

Please sign in to comment.