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

Commit

Permalink
Merge pull request #1401 from CyberShadow/pull-20151002-151147
Browse files Browse the repository at this point in the history
fix Issue 15137 - core.time: Support Duration/Duration and Duration%Duration
  • Loading branch information
jmdavis committed Oct 3, 2015
2 parents e49df27 + e02d47c commit 8ddfb8d
Showing 1 changed file with 76 additions and 9 deletions.
85 changes: 76 additions & 9 deletions src/core/time.d
Original file line number Diff line number Diff line change
Expand Up @@ -519,13 +519,14 @@ public:


/++
Adds or subtracts two durations.
Adds, subtracts or calculates the modulo of two durations.
The legal types of arithmetic for $(D Duration) using this operator are
$(TABLE
$(TR $(TD Duration) $(TD +) $(TD Duration) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD -) $(TD Duration) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD %) $(TD Duration) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD +) $(TD TickDuration) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD -) $(TD TickDuration) $(TD -->) $(TD Duration))
)
Expand All @@ -534,9 +535,8 @@ public:
rhs = The duration to add to or subtract from this $(D Duration).
+/
Duration opBinary(string op, D)(D rhs) const nothrow @nogc
if((op == "+" || op == "-") &&
(is(_Unqual!D == Duration) ||
is(_Unqual!D == TickDuration)))
if(((op == "+" || op == "-" || op == "%") && is(_Unqual!D == Duration)) ||
((op == "+" || op == "-") && is(_Unqual!D == TickDuration)))
{
static if(is(_Unqual!D == Duration))
return Duration(mixin("_hnsecs " ~ op ~ " rhs._hnsecs"));
Expand All @@ -552,23 +552,31 @@ public:
{
assert((cast(D)Duration(5)) + (cast(E)Duration(7)) == Duration(12));
assert((cast(D)Duration(5)) - (cast(E)Duration(7)) == Duration(-2));
assert((cast(D)Duration(5)) % (cast(E)Duration(7)) == Duration(5));
assert((cast(D)Duration(7)) + (cast(E)Duration(5)) == Duration(12));
assert((cast(D)Duration(7)) - (cast(E)Duration(5)) == Duration(2));
assert((cast(D)Duration(7)) % (cast(E)Duration(5)) == Duration(2));

assert((cast(D)Duration(5)) + (cast(E)Duration(-7)) == Duration(-2));
assert((cast(D)Duration(5)) - (cast(E)Duration(-7)) == Duration(12));
assert((cast(D)Duration(5)) % (cast(E)Duration(-7)) == Duration(5));
assert((cast(D)Duration(7)) + (cast(E)Duration(-5)) == Duration(2));
assert((cast(D)Duration(7)) - (cast(E)Duration(-5)) == Duration(12));
assert((cast(D)Duration(7)) % (cast(E)Duration(-5)) == Duration(2));

assert((cast(D)Duration(-5)) + (cast(E)Duration(7)) == Duration(2));
assert((cast(D)Duration(-5)) - (cast(E)Duration(7)) == Duration(-12));
assert((cast(D)Duration(-5)) % (cast(E)Duration(7)) == Duration(-5));
assert((cast(D)Duration(-7)) + (cast(E)Duration(5)) == Duration(-2));
assert((cast(D)Duration(-7)) - (cast(E)Duration(5)) == Duration(-12));
assert((cast(D)Duration(-7)) % (cast(E)Duration(5)) == Duration(-2));

assert((cast(D)Duration(-5)) + (cast(E)Duration(-7)) == Duration(-12));
assert((cast(D)Duration(-5)) - (cast(E)Duration(-7)) == Duration(2));
assert((cast(D)Duration(-5)) % (cast(E)Duration(7)) == Duration(-5));
assert((cast(D)Duration(-7)) + (cast(E)Duration(-5)) == Duration(-12));
assert((cast(D)Duration(-7)) - (cast(E)Duration(-5)) == Duration(-2));
assert((cast(D)Duration(-7)) % (cast(E)Duration(5)) == Duration(-2));
}

foreach(T; _TypeTuple!(TickDuration, const TickDuration, immutable TickDuration))
Expand Down Expand Up @@ -649,14 +657,15 @@ public:


/++
Adds or subtracts two durations as well as assigning the result to this
$(D Duration).
Adds, subtracts or calculates the modulo of two durations as well as
assigning the result to this $(D Duration).
The legal types of arithmetic for $(D Duration) using this operator are
$(TABLE
$(TR $(TD Duration) $(TD +) $(TD Duration) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD -) $(TD Duration) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD %) $(TD Duration) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD +) $(TD TickDuration) $(TD -->) $(TD Duration))
$(TR $(TD Duration) $(TD -) $(TD TickDuration) $(TD -->) $(TD Duration))
)
Expand All @@ -665,9 +674,8 @@ public:
rhs = The duration to add to or subtract from this $(D Duration).
+/
ref Duration opOpAssign(string op, D)(in D rhs) nothrow @nogc
if((op == "+" || op == "-") &&
(is(_Unqual!D == Duration) ||
is(_Unqual!D == TickDuration)))
if(((op == "+" || op == "-" || op == "%") && is(_Unqual!D == Duration)) ||
((op == "+" || op == "-") && is(_Unqual!D == TickDuration)))
{
static if(is(_Unqual!D == Duration))
mixin("_hnsecs " ~ op ~ "= rhs._hnsecs;");
Expand Down Expand Up @@ -699,23 +707,31 @@ public:
{
test1!"+="(Duration(5), (cast(E)Duration(7)), Duration(12));
test1!"-="(Duration(5), (cast(E)Duration(7)), Duration(-2));
test1!"%="(Duration(5), (cast(E)Duration(7)), Duration(5));
test1!"+="(Duration(7), (cast(E)Duration(5)), Duration(12));
test1!"-="(Duration(7), (cast(E)Duration(5)), Duration(2));
test1!"%="(Duration(7), (cast(E)Duration(5)), Duration(2));

test1!"+="(Duration(5), (cast(E)Duration(-7)), Duration(-2));
test1!"-="(Duration(5), (cast(E)Duration(-7)), Duration(12));
test1!"%="(Duration(5), (cast(E)Duration(-7)), Duration(5));
test1!"+="(Duration(7), (cast(E)Duration(-5)), Duration(2));
test1!"-="(Duration(7), (cast(E)Duration(-5)), Duration(12));
test1!"%="(Duration(7), (cast(E)Duration(-5)), Duration(2));

test1!"+="(Duration(-5), (cast(E)Duration(7)), Duration(2));
test1!"-="(Duration(-5), (cast(E)Duration(7)), Duration(-12));
test1!"%="(Duration(-5), (cast(E)Duration(7)), Duration(-5));
test1!"+="(Duration(-7), (cast(E)Duration(5)), Duration(-2));
test1!"-="(Duration(-7), (cast(E)Duration(5)), Duration(-12));
test1!"%="(Duration(-7), (cast(E)Duration(5)), Duration(-2));

test1!"+="(Duration(-5), (cast(E)Duration(-7)), Duration(-12));
test1!"-="(Duration(-5), (cast(E)Duration(-7)), Duration(2));
test1!"%="(Duration(-5), (cast(E)Duration(-7)), Duration(-5));
test1!"+="(Duration(-7), (cast(E)Duration(-5)), Duration(-12));
test1!"-="(Duration(-7), (cast(E)Duration(-5)), Duration(-2));
test1!"%="(Duration(-7), (cast(E)Duration(-5)), Duration(-2));
}

foreach(T; _TypeTuple!(TickDuration, const TickDuration, immutable TickDuration))
Expand Down Expand Up @@ -754,7 +770,12 @@ public:
}


// Note: opBinary!"*" and opBinary!"/" have to be implemented separately
// because opBinary!"/" may throw TimeException, and opBinary!"*" is nothrow

/++
Multiplies the duration by an integer value.
The legal types of arithmetic for $(D Duration) using this operator
overload are
Expand Down Expand Up @@ -794,6 +815,9 @@ public:


/++
Multiplies the duration by an integer value as well as
assigning the result to this $(D Duration).
The legal types of arithmetic for $(D Duration) using this operator
overload are
Expand Down Expand Up @@ -846,6 +870,8 @@ public:


/++
Divides the duration by an integer value.
The legal types of arithmetic for $(D Duration) using this operator
overload are
Expand Down Expand Up @@ -897,6 +923,9 @@ public:


/++
Divides the duration by an integer value as well as
assigning the result to this $(D Duration).
The legal types of arithmetic for $(D Duration) using this operator
overload are
Expand Down Expand Up @@ -954,6 +983,44 @@ public:
}


/++
Divides two durations.
The legal types of arithmetic for $(D Duration) using this operator are
$(TABLE
$(TR $(TD Duration) $(TD /) $(TD Duration) $(TD -->) $(TD long))
)
Params:
rhs = The duration to divide this $(D Duration) by.
+/
long opBinary(string op)(Duration rhs) const nothrow @nogc
if(op == "/")
{
return _hnsecs / rhs._hnsecs;
}

unittest
{
assert(Duration(5) / Duration(7) == 0);
assert(Duration(7) / Duration(5) == 1);
assert(Duration(8) / Duration(4) == 2);

assert(Duration(5) / Duration(-7) == 0);
assert(Duration(7) / Duration(-5) == -1);
assert(Duration(8) / Duration(-4) == -2);

assert(Duration(-5) / Duration(7) == 0);
assert(Duration(-7) / Duration(5) == -1);
assert(Duration(-8) / Duration(4) == -2);

assert(Duration(-5) / Duration(-7) == 0);
assert(Duration(-7) / Duration(-5) == 1);
assert(Duration(-8) / Duration(-4) == 2);
}


/++
Multiplies an integral value and a $(D Duration).
Expand Down

0 comments on commit 8ddfb8d

Please sign in to comment.