diff --git a/changelog/druntime.tickduration.dd b/changelog/druntime.tickduration.dd new file mode 100644 index 000000000000..6c25f77497c8 --- /dev/null +++ b/changelog/druntime.tickduration.dd @@ -0,0 +1,4 @@ +`core.time.TickDuration` has been removed. + +The `TickDuration` type was deprecated in 2.104.0, and is now removed from +druntime. Instead, you should use `core.time.MonoTime` diff --git a/druntime/src/core/time.d b/druntime/src/core/time.d index aa8a4d7a9490..1459ab00f860 100644 --- a/druntime/src/core/time.d +++ b/druntime/src/core/time.d @@ -18,8 +18,6 @@ $(LEADINGROW Types) $(TR $(TDNW $(LREF Duration)) $(TD Represents a duration of time of weeks or less (kept internally as hnsecs). (e.g. 22 days or 700 seconds).)) - $(TR $(TDNW $(LREF TickDuration)) $(TD $(RED DEPRECATED) Represents a duration of time in - system clock ticks, using the highest precision that the system provides.)) $(TR $(TDNW $(LREF MonoTime)) $(TD Represents a monotonic timestamp in system clock ticks, using the highest precision that the system provides.)) $(LEADINGROW Functions) @@ -37,22 +35,14 @@ $(BOOKTABLE Conversions, $(TR $(TH ) $(TH From $(LREF Duration)) - $(TH From $(LREF TickDuration)) $(TH From units) ) $(TR $(TD $(B To $(LREF Duration))) $(TD -) - $(TD $(D tickDuration.)$(REF_SHORT to, std,conv)$(D !Duration())) $(TD $(D dur!"msecs"(5)) or $(D 5.msecs())) ) - $(TR $(TD $(B To $(LREF TickDuration))) - $(TD $(D duration.)$(REF_SHORT to, std,conv)$(D !TickDuration())) - $(TD -) - $(TD $(D TickDuration.from!"msecs"(msecs))) - ) $(TR $(TD $(B To units)) $(TD $(D duration.total!"days")) - $(TD $(D tickDuration.msecs)) $(TD $(D convert!("days", "msecs")(msecs))) )) @@ -741,12 +731,6 @@ public: return Duration(mixin("_hnsecs " ~ op ~ " rhs._hnsecs")); } - deprecated Duration opBinary(string op)(const TickDuration rhs) const nothrow @nogc - if (op == "+" || op == "-") - { - return Duration(mixin("_hnsecs " ~ op ~ " rhs.hnsecs")); - } - version (CoreUnittest) unittest { alias Types = AliasSeq!(Duration, const Duration, immutable Duration); @@ -785,89 +769,6 @@ public: } } - version (CoreUnittest) deprecated unittest - { - foreach (D; AliasSeq!(Duration, const Duration, immutable Duration)) - { - foreach (T; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - assertApprox((cast(D)Duration(5)) + cast(T)TickDuration.from!"usecs"(7), Duration(70), Duration(80)); - assertApprox((cast(D)Duration(5)) - cast(T)TickDuration.from!"usecs"(7), Duration(-70), Duration(-60)); - assertApprox((cast(D)Duration(7)) + cast(T)TickDuration.from!"usecs"(5), Duration(52), Duration(62)); - assertApprox((cast(D)Duration(7)) - cast(T)TickDuration.from!"usecs"(5), Duration(-48), Duration(-38)); - - assertApprox((cast(D)Duration(5)) + cast(T)TickDuration.from!"usecs"(-7), Duration(-70), Duration(-60)); - assertApprox((cast(D)Duration(5)) - cast(T)TickDuration.from!"usecs"(-7), Duration(70), Duration(80)); - assertApprox((cast(D)Duration(7)) + cast(T)TickDuration.from!"usecs"(-5), Duration(-48), Duration(-38)); - assertApprox((cast(D)Duration(7)) - cast(T)TickDuration.from!"usecs"(-5), Duration(52), Duration(62)); - - assertApprox((cast(D)Duration(-5)) + cast(T)TickDuration.from!"usecs"(7), Duration(60), Duration(70)); - assertApprox((cast(D)Duration(-5)) - cast(T)TickDuration.from!"usecs"(7), Duration(-80), Duration(-70)); - assertApprox((cast(D)Duration(-7)) + cast(T)TickDuration.from!"usecs"(5), Duration(38), Duration(48)); - assertApprox((cast(D)Duration(-7)) - cast(T)TickDuration.from!"usecs"(5), Duration(-62), Duration(-52)); - - assertApprox((cast(D)Duration(-5)) + cast(T)TickDuration.from!"usecs"(-7), Duration(-80), Duration(-70)); - assertApprox((cast(D)Duration(-5)) - cast(T)TickDuration.from!"usecs"(-7), Duration(60), Duration(70)); - assertApprox((cast(D)Duration(-7)) + cast(T)TickDuration.from!"usecs"(-5), Duration(-62), Duration(-52)); - assertApprox((cast(D)Duration(-7)) - cast(T)TickDuration.from!"usecs"(-5), Duration(38), Duration(48)); - } - } - } - - - /++ - $(RED TickDuration is Deprecated) - - Adds or subtracts two durations. - - The legal types of arithmetic for $(D Duration) using this operator are - - $(TABLE - $(TR $(TD TickDuration) $(TD +) $(TD Duration) $(TD -->) $(TD Duration)) - $(TR $(TD TickDuration) $(TD -) $(TD Duration) $(TD -->) $(TD Duration)) - ) - - Params: - lhs = The $(D TickDuration) to add to this $(D Duration) or to - subtract this $(D Duration) from. - +/ - deprecated Duration opBinaryRight(string op, D)(D lhs) const nothrow @nogc - if ((op == "+" || op == "-") && - is(immutable D == immutable TickDuration)) - { - return Duration(mixin("lhs.hnsecs " ~ op ~ " _hnsecs")); - } - - version (CoreUnittest) deprecated unittest - { - foreach (D; AliasSeq!(Duration, const Duration, immutable Duration)) - { - foreach (T; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - assertApprox((cast(T)TickDuration.from!"usecs"(7)) + cast(D)Duration(5), Duration(70), Duration(80)); - assertApprox((cast(T)TickDuration.from!"usecs"(7)) - cast(D)Duration(5), Duration(60), Duration(70)); - assertApprox((cast(T)TickDuration.from!"usecs"(5)) + cast(D)Duration(7), Duration(52), Duration(62)); - assertApprox((cast(T)TickDuration.from!"usecs"(5)) - cast(D)Duration(7), Duration(38), Duration(48)); - - assertApprox((cast(T)TickDuration.from!"usecs"(-7)) + cast(D)Duration(5), Duration(-70), Duration(-60)); - assertApprox((cast(T)TickDuration.from!"usecs"(-7)) - cast(D)Duration(5), Duration(-80), Duration(-70)); - assertApprox((cast(T)TickDuration.from!"usecs"(-5)) + cast(D)Duration(7), Duration(-48), Duration(-38)); - assertApprox((cast(T)TickDuration.from!"usecs"(-5)) - cast(D)Duration(7), Duration(-62), Duration(-52)); - - assertApprox((cast(T)TickDuration.from!"usecs"(7)) + (cast(D)Duration(-5)), Duration(60), Duration(70)); - assertApprox((cast(T)TickDuration.from!"usecs"(7)) - (cast(D)Duration(-5)), Duration(70), Duration(80)); - assertApprox((cast(T)TickDuration.from!"usecs"(5)) + (cast(D)Duration(-7)), Duration(38), Duration(48)); - assertApprox((cast(T)TickDuration.from!"usecs"(5)) - (cast(D)Duration(-7)), Duration(52), Duration(62)); - - assertApprox((cast(T)TickDuration.from!"usecs"(-7)) + cast(D)Duration(-5), Duration(-80), Duration(-70)); - assertApprox((cast(T)TickDuration.from!"usecs"(-7)) - cast(D)Duration(-5), Duration(-70), Duration(-60)); - assertApprox((cast(T)TickDuration.from!"usecs"(-5)) + cast(D)Duration(-7), Duration(-62), Duration(-52)); - assertApprox((cast(T)TickDuration.from!"usecs"(-5)) - cast(D)Duration(-7), Duration(-48), Duration(-38)); - } - } - } - - /++ Adds, subtracts or calculates the modulo of two durations as well as assigning the result to this $(D Duration). @@ -890,13 +791,6 @@ public: return this; } - deprecated ref Duration opOpAssign(string op)(const TickDuration rhs) nothrow @nogc - if (op == "+" || op == "-") - { - mixin("_hnsecs " ~ op ~ "= rhs.hnsecs;"); - return this; - } - version (CoreUnittest) unittest { static void test1(string op, E)(Duration actual, in E rhs, Duration expected, size_t line = __LINE__) @@ -950,49 +844,6 @@ public: } } - version (CoreUnittest) deprecated unittest - { - static void test2(string op, E) - (Duration actual, in E rhs, Duration lower, Duration upper, size_t line = __LINE__) - { - assertApprox(mixin("actual " ~ op ~ " rhs"), lower, upper, "op failed", line); - assertApprox(actual, lower, upper, "op assign failed", line); - } - - foreach (T; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - test2!"+="(Duration(5), cast(T)TickDuration.from!"usecs"(7), Duration(70), Duration(80)); - test2!"-="(Duration(5), cast(T)TickDuration.from!"usecs"(7), Duration(-70), Duration(-60)); - test2!"+="(Duration(7), cast(T)TickDuration.from!"usecs"(5), Duration(52), Duration(62)); - test2!"-="(Duration(7), cast(T)TickDuration.from!"usecs"(5), Duration(-48), Duration(-38)); - - test2!"+="(Duration(5), cast(T)TickDuration.from!"usecs"(-7), Duration(-70), Duration(-60)); - test2!"-="(Duration(5), cast(T)TickDuration.from!"usecs"(-7), Duration(70), Duration(80)); - test2!"+="(Duration(7), cast(T)TickDuration.from!"usecs"(-5), Duration(-48), Duration(-38)); - test2!"-="(Duration(7), cast(T)TickDuration.from!"usecs"(-5), Duration(52), Duration(62)); - - test2!"+="(Duration(-5), cast(T)TickDuration.from!"usecs"(7), Duration(60), Duration(70)); - test2!"-="(Duration(-5), cast(T)TickDuration.from!"usecs"(7), Duration(-80), Duration(-70)); - test2!"+="(Duration(-7), cast(T)TickDuration.from!"usecs"(5), Duration(38), Duration(48)); - test2!"-="(Duration(-7), cast(T)TickDuration.from!"usecs"(5), Duration(-62), Duration(-52)); - - test2!"+="(Duration(-5), cast(T)TickDuration.from!"usecs"(-7), Duration(-80), Duration(-70)); - test2!"-="(Duration(-5), cast(T)TickDuration.from!"usecs"(-7), Duration(60), Duration(70)); - test2!"+="(Duration(-7), cast(T)TickDuration.from!"usecs"(-5), Duration(-62), Duration(-52)); - test2!"-="(Duration(-7), cast(T)TickDuration.from!"usecs"(-5), Duration(38), Duration(48)); - } - - foreach (D; AliasSeq!(const Duration, immutable Duration)) - { - foreach (E; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - D lhs = D(120); - E rhs = E(120); - static assert(!__traits(compiles, lhs += rhs), D.stringof ~ " " ~ E.stringof); - } - } - } - /++ Multiplies or divides the duration by an integer value. @@ -1238,52 +1089,6 @@ public: } } - - /++ - $(RED TickDuration is Deprecated) - - Returns a $(LREF TickDuration) with the same number of hnsecs as this - $(D Duration). - Note that the conventional way to convert between $(D Duration) and - $(D TickDuration) is using $(REF to, std,conv), e.g.: - $(D duration.to!TickDuration()) - +/ - deprecated TickDuration opCast(T)() const nothrow @nogc - if (is(immutable T == immutable TickDuration)) - { - return TickDuration.from!"hnsecs"(_hnsecs); - } - - version (CoreUnittest) deprecated unittest - { - foreach (D; AliasSeq!(Duration, const Duration, immutable Duration)) - { - foreach (units; AliasSeq!("seconds", "msecs", "usecs", "hnsecs")) - { - enum unitsPerSec = convert!("seconds", units)(1); - - if (TickDuration.ticksPerSec >= unitsPerSec) - { - foreach (T; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - auto t = TickDuration.from!units(1); - assertApprox(cast(T)cast(D)dur!units(1), t - TickDuration(1), t + TickDuration(1), units); - t = TickDuration.from!units(2); - assertApprox(cast(T)cast(D)dur!units(2), t - TickDuration(1), t + TickDuration(1), units); - } - } - else - { - auto t = TickDuration.from!units(1); - assert(t.to!(units, long)() == 0, units); - t = TickDuration.from!units(1_000_000); - assert(t.to!(units, long)() >= 900_000, units); - assert(t.to!(units, long)() <= 1_100_000, units); - } - } - } - } - /++ Allow Duration to be used as a boolean. Returns: `true` if this duration is non-zero. @@ -1833,139 +1638,6 @@ version (CoreUnittest) @safe pure nothrow @nogc unittest assert(buffer[0 .. len] == "-2 weeks, -2 secs, -90 ms, and -3 hnsecs"); } -/++ - $(RED TickDuration is DEPRECATED) - - Converts a $(D TickDuration) to the given units as either an integral - value or a floating point value. - - Params: - units = The units to convert to. Accepts $(D "seconds") and smaller - only. - T = The type to convert to (either an integral type or a - floating point type). - - td = The TickDuration to convert - +/ -deprecated("TickDuration has been deprecated, please use Duration or MonoTime instead") -T to(string units, T, D)(D td) @safe pure nothrow @nogc - if (is(immutable D == immutable TickDuration) && - (units == "seconds" || - units == "msecs" || - units == "usecs" || - units == "hnsecs" || - units == "nsecs")) -{ - static if (__traits(isIntegral, T) && T.sizeof >= 4) - { - enum unitsPerSec = convert!("seconds", units)(1); - - return cast(T) convClockFreq(td.length, TickDuration.ticksPerSec, unitsPerSec); - } - else static if (__traits(isFloating, T)) - { - static if (units == "seconds") - return td.length / cast(T)TickDuration.ticksPerSec; - else - { - enum unitsPerSec = convert!("seconds", units)(1); - - return cast(T) (td.length / - (TickDuration.ticksPerSec / cast(real) unitsPerSec)); - } - } - else - static assert(0, "Incorrect template constraint."); -} - -/// -deprecated unittest -{ - auto t = TickDuration.from!"seconds"(1000); - - long tl = to!("seconds",long)(t); - assert(tl == 1000); - - import core.stdc.math : fabs; - double td = to!("seconds",double)(t); - assert(fabs(td - 1000) < 0.001); -} - -deprecated unittest -{ - void testFun(string U)() { - auto t1v = 1000; - auto t2v = 333; - - auto t1 = TickDuration.from!U(t1v); - auto t2 = TickDuration.from!U(t2v); - - auto _str(F)(F val) - { - static if (is(F == int) || is(F == long)) - return signedToTempString(val); - else - return unsignedToTempString(val); - } - - foreach (F; AliasSeq!(int,uint,long,ulong,float,double,real)) - { - F t1f = to!(U,F)(t1); - F t2f = to!(U,F)(t2); - auto t12d = t1 / t2v; - auto t12m = t1 - t2; - F t3f = to!(U,F)(t12d); - F t4f = to!(U,F)(t12m); - - - static if (is(F == float) || is(F == double) || is(F == real)) - { - assert((t1f - cast(F)t1v) <= 3.0, - F.stringof ~ " " ~ U ~ " " ~ doubleToString(t1f) ~ " " ~ - doubleToString(cast(F)t1v) - ); - assert((t2f - cast(F)t2v) <= 3.0, - F.stringof ~ " " ~ U ~ " " ~ doubleToString(t2f) ~ " " ~ - doubleToString(cast(F)t2v) - ); - assert(t3f - (cast(F)t1v) / (cast(F)t2v) <= 3.0, - F.stringof ~ " " ~ U ~ " " ~ doubleToString(t3f) ~ " " ~ - doubleToString((cast(F)t1v)/(cast(F)t2v)) - ); - assert(t4f - (cast(F)(t1v - t2v)) <= 3.0, - F.stringof ~ " " ~ U ~ " " ~ doubleToString(t4f) ~ " " ~ - doubleToString(cast(F)(t1v - t2v)) - ); - } - else - { - // even though this should be exact math it is not as internal - // in "to" floating point is used - assert(_abs(t1f) - _abs(cast(F)t1v) <= 3, - F.stringof ~ " " ~ U ~ " " ~ _str(t1f) ~ " " ~ - _str(cast(F)t1v) - ); - assert(_abs(t2f) - _abs(cast(F)t2v) <= 3, - F.stringof ~ " " ~ U ~ " " ~ _str(t2f) ~ " " ~ - _str(cast(F)t2v) - ); - assert(_abs(t3f) - _abs((cast(F)t1v) / (cast(F)t2v)) <= 3, - F.stringof ~ " " ~ U ~ " " ~ _str(t3f) ~ " " ~ - _str((cast(F)t1v) / (cast(F)t2v)) - ); - assert(_abs(t4f) - _abs((cast(F)t1v) - (cast(F)t2v)) <= 3, - F.stringof ~ " " ~ U ~ " " ~ _str(t4f) ~ " " ~ - _str((cast(F)t1v) - (cast(F)t2v)) - ); - } - } - } - - testFun!"seconds"(); - testFun!"msecs"(); - testFun!"usecs"(); -} - /++ These allow you to construct a $(D Duration) from the given time units with the given length. @@ -2830,687 +2502,6 @@ unittest assert(nsecsToTicks(nsecs) == ticks); } - - -/++ - $(RED Warning: TickDuration is deprecated. Please use - $(LREF MonoTime) for the cases where a monotonic timestamp is needed - and $(LREF Duration) when a duration is needed, rather than using - TickDuration.) - - Represents a duration of time in system clock ticks. - - The system clock ticks are the ticks of the system clock at the highest - precision that the system provides. - +/ -deprecated("TickDuration has been deprecated, please use Duration or MonoTime instead") -struct TickDuration -{ -deprecated: - private static TickDuration TDRvalueOf(TickDuration td) - { - return td; - } - /++ - The number of ticks that the system clock has in one second. - - If $(D ticksPerSec) is $(D 0), then then $(D TickDuration) failed to - get the value of $(D ticksPerSec) on the current system, and - $(D TickDuration) is not going to work. That would be highly abnormal - though. - +/ - static immutable long ticksPerSec; - - - /++ - The tick of the system clock (as a $(D TickDuration)) when the - application started. - +/ - static immutable TickDuration appOrigin; - - - static @property @safe pure nothrow @nogc - { - /++ - It's the same as $(D TickDuration(0)), but it's provided to be - consistent with $(D Duration), which provides a $(D zero) property. - +/ - TickDuration zero() { return TickDuration(0); } - - /++ - Largest $(D TickDuration) possible. - +/ - TickDuration max() { return TickDuration(long.max); } - - /++ - Most negative $(D TickDuration) possible. - +/ - TickDuration min() { return TickDuration(long.min); } - } - - version (CoreUnittest) unittest - { - assert((zero == TickDuration(0)) == true); - assert((TickDuration.max == TickDuration(long.max)) == true); - assert((TickDuration.min == TickDuration(long.min)) == true); - assert((TickDuration.min < TickDuration.zero) == true); - assert((TickDuration.zero < TickDuration.max) == true); - assert((TickDuration.min < TickDuration.max) == true); - assert((TickDuration.min - TickDuration(1) == TickDuration.max) == true); - assert((TickDuration.max + TickDuration(1) == TickDuration.min) == true); - } - - - static pragma(crt_constructor) void time_initializer() - { - version (Windows) - { - if (QueryPerformanceFrequency(cast(long*)&ticksPerSec) == 0) - ticksPerSec = 0; - } - else version (Darwin) - { - ticksPerSec = machTicksPerSecond(); - } - else version (Posix) - { - static if (is(typeof(clock_gettime))) - { - timespec ts; - - if (clock_getres(CLOCK_MONOTONIC, &ts) != 0) - ticksPerSec = 0; - else - { - //For some reason, on some systems, clock_getres returns - //a resolution which is clearly wrong (it's a millisecond - //or worse, but the time is updated much more frequently - //than that). In such cases, we'll just use nanosecond - //resolution. - ticksPerSec = ts.tv_nsec >= 1000 ? 1_000_000_000 - : 1_000_000_000 / ts.tv_nsec; - } - } - else - ticksPerSec = 1_000_000; - } - else - static assert(0, "Unsupported platform"); - - if (ticksPerSec != 0) - appOrigin = TickDuration.currSystemTick; - } - - version (CoreUnittest) unittest - { - assert(ticksPerSec); - } - - - /++ - The number of system ticks in this $(D TickDuration). - - You can convert this $(D length) into the number of seconds by dividing - it by $(D ticksPerSec) (or using one the appropriate property function - to do it). - +/ - long length; - - /++ - Returns the total number of seconds in this $(D TickDuration). - +/ - @property long seconds() @safe const pure nothrow @nogc - { - return this.to!("seconds", long)(); - } - - version (CoreUnittest) unittest - { - foreach (T; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - assert((cast(T)TickDuration(ticksPerSec)).seconds == 1); - assert((cast(T)TickDuration(ticksPerSec - 1)).seconds == 0); - assert((cast(T)TickDuration(ticksPerSec * 2)).seconds == 2); - assert((cast(T)TickDuration(ticksPerSec * 2 - 1)).seconds == 1); - assert((cast(T)TickDuration(-1)).seconds == 0); - assert((cast(T)TickDuration(-ticksPerSec - 1)).seconds == -1); - assert((cast(T)TickDuration(-ticksPerSec)).seconds == -1); - } - } - - - /++ - Returns the total number of milliseconds in this $(D TickDuration). - +/ - @property long msecs() @safe const pure nothrow @nogc - { - return this.to!("msecs", long)(); - } - - - /++ - Returns the total number of microseconds in this $(D TickDuration). - +/ - @property long usecs() @safe const pure nothrow @nogc - { - return this.to!("usecs", long)(); - } - - - /++ - Returns the total number of hecto-nanoseconds in this $(D TickDuration). - +/ - @property long hnsecs() @safe const pure nothrow @nogc - { - return this.to!("hnsecs", long)(); - } - - - /++ - Returns the total number of nanoseconds in this $(D TickDuration). - +/ - @property long nsecs() @safe const pure nothrow @nogc - { - return this.to!("nsecs", long)(); - } - - - /++ - This allows you to construct a $(D TickDuration) from the given time - units with the given length. - - Params: - units = The time units of the $(D TickDuration) (e.g. $(D "msecs")). - length = The number of units in the $(D TickDuration). - +/ - static TickDuration from(string units)(long length) @safe pure nothrow @nogc - if (units == "seconds" || - units == "msecs" || - units == "usecs" || - units == "hnsecs" || - units == "nsecs") - { - enum unitsPerSec = convert!("seconds", units)(1); - - return TickDuration(convClockFreq(length, unitsPerSec, ticksPerSec)); - } - - version (CoreUnittest) unittest - { - foreach (units; AliasSeq!("seconds", "msecs", "usecs", "nsecs")) - { - foreach (T; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - assertApprox((cast(T)TickDuration.from!units(1000)).to!(units, long)(), - 500, 1500, units); - assertApprox((cast(T)TickDuration.from!units(1_000_000)).to!(units, long)(), - 900_000, 1_100_000, units); - assertApprox((cast(T)TickDuration.from!units(2_000_000)).to!(units, long)(), - 1_900_000, 2_100_000, units); - } - } - } - - - /++ - Returns a $(LREF Duration) with the same number of hnsecs as this - $(D TickDuration). - Note that the conventional way to convert between $(D TickDuration) - and $(D Duration) is using $(REF to, std,conv), e.g.: - $(D tickDuration.to!Duration()) - +/ - Duration opCast(T)() @safe const pure nothrow @nogc - if (is(immutable T == immutable Duration)) - { - return Duration(hnsecs); - } - - version (CoreUnittest) unittest - { - foreach (D; AliasSeq!(Duration, const Duration, immutable Duration)) - { - foreach (T; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - auto expected = dur!"seconds"(1); - assert(cast(D)cast(T)TickDuration.from!"seconds"(1) == expected); - - foreach (units; AliasSeq!("msecs", "usecs", "hnsecs")) - { - D actual = cast(D)cast(T)TickDuration.from!units(1_000_000); - assertApprox(actual, dur!units(900_000), dur!units(1_100_000)); - } - } - } - } - - - //Temporary hack until bug http://d.puremagic.com/issues/show_bug.cgi?id=5747 is fixed. - TickDuration opCast(T)() @safe const pure nothrow @nogc - if (is(immutable T == immutable TickDuration)) - { - return this; - } - - - /++ - Adds or subtracts two $(D TickDuration)s as well as assigning the result - to this $(D TickDuration). - - The legal types of arithmetic for $(D TickDuration) using this operator - are - - $(TABLE - $(TR $(TD TickDuration) $(TD +=) $(TD TickDuration) $(TD -->) $(TD TickDuration)) - $(TR $(TD TickDuration) $(TD -=) $(TD TickDuration) $(TD -->) $(TD TickDuration)) - ) - - Params: - rhs = The $(D TickDuration) to add to or subtract from this - $(D $(D TickDuration)). - +/ - ref TickDuration opOpAssign(string op)(TickDuration rhs) @safe pure nothrow @nogc - if (op == "+" || op == "-") - { - mixin("length " ~ op ~ "= rhs.length;"); - return this; - } - - version (CoreUnittest) unittest - { - foreach (T; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - auto a = TickDuration.currSystemTick; - auto result = a += cast(T)TickDuration.currSystemTick; - assert((a == result) == true); - assert(a.to!("seconds", real)() >= 0); - - auto b = TickDuration.currSystemTick; - result = b -= cast(T)TickDuration.currSystemTick; - assert((b == result) == true); - assert(b.to!("seconds", real)() <= 0); - - foreach (U; AliasSeq!(const TickDuration, immutable TickDuration)) - { - U u = TickDuration(12); - static assert(!__traits(compiles, u += cast(T)TickDuration.currSystemTick)); - static assert(!__traits(compiles, u -= cast(T)TickDuration.currSystemTick)); - } - } - } - - - /++ - Adds or subtracts two $(D TickDuration)s. - - The legal types of arithmetic for $(D TickDuration) using this operator - are - - $(TABLE - $(TR $(TD TickDuration) $(TD +) $(TD TickDuration) $(TD -->) $(TD TickDuration)) - $(TR $(TD TickDuration) $(TD -) $(TD TickDuration) $(TD -->) $(TD TickDuration)) - ) - - Params: - rhs = The $(D TickDuration) to add to or subtract from this - $(D TickDuration). - +/ - TickDuration opBinary(string op)(TickDuration rhs) @safe const pure nothrow @nogc - if (op == "+" || op == "-") - { - return TickDuration(mixin("length " ~ op ~ " rhs.length")); - } - - version (CoreUnittest) unittest - { - foreach (T; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - T a = TickDuration.currSystemTick; - T b = TickDuration.currSystemTick; - assert((a + b).usecs > 0); - assert((a - b).seconds <= 0); - } - } - - - /++ - Returns the negation of this $(D TickDuration). - +/ - TickDuration opUnary(string op)() @safe const pure nothrow @nogc - if (op == "-") - { - return TickDuration(-length); - } - - version (CoreUnittest) unittest - { - foreach (T; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - assert((-(cast(T)TickDuration(7)) == TickDuration(-7)) == true); - assert((-(cast(T)TickDuration(5)) == TickDuration(-5)) == true); - assert((-(cast(T)TickDuration(-7)) == TickDuration(7)) == true); - assert((-(cast(T)TickDuration(-5)) == TickDuration(5)) == true); - assert((-(cast(T)TickDuration(0)) == TickDuration(0)) == true); - } - } - - - /++ - operator overloading "<, >, <=, >=" - +/ - int opCmp(TickDuration rhs) @safe const pure nothrow @nogc - { - return (length > rhs.length) - (length < rhs.length); - } - - version (CoreUnittest) unittest - { - import core.internal.traits : rvalueOf; - foreach (T; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - foreach (U; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - T t = TickDuration.currSystemTick; - U u = t; - assert((t == u) == true); - assert((TDRvalueOf(t) == u) == true); - assert((t == TDRvalueOf(u)) == true); - } - } - - foreach (T; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - foreach (U; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - T t = TickDuration.currSystemTick; - U u = t + t; - assert((t < u) == true); - assert((t <= t) == true); - assert((u > t) == true); - assert((u >= u) == true); - - assert((TDRvalueOf(t) < u) == true); - assert((TDRvalueOf(t) <= t) == true); - assert((TDRvalueOf(u) > t) == true); - assert((TDRvalueOf(u) >= u) == true); - - assert((t < TDRvalueOf(u)) == true); - assert((t <= TDRvalueOf(t)) == true); - assert((u > TDRvalueOf(t)) == true); - assert((u >= TDRvalueOf(u)) == true); - } - } - } - - - /++ - The legal types of arithmetic for $(D TickDuration) using this operator - overload are - - $(TABLE - $(TR $(TD TickDuration) $(TD *) $(TD long) $(TD -->) $(TD TickDuration)) - $(TR $(TD TickDuration) $(TD *) $(TD floating point) $(TD -->) $(TD TickDuration)) - ) - - Params: - value = The value to divide from this duration. - +/ - void opOpAssign(string op, T)(T value) @safe pure nothrow @nogc - if (op == "*" && - (__traits(isIntegral, T) || __traits(isFloating, T))) - { - length = cast(long)(length * value); - } - - version (CoreUnittest) unittest - { - immutable curr = TickDuration.currSystemTick; - TickDuration t1 = curr; - immutable t2 = curr + curr; - t1 *= 2; - assert((t1 == t2) == true); - - t1 = curr; - t1 *= 2.0; - immutable tol = TickDuration(cast(long)(_abs(t1.length) * double.epsilon * 2.0)); - assertApprox(t1, t2 - tol, t2 + tol); - - t1 = curr; - t1 *= 2.1; - assert((t1 > t2) == true); - - foreach (T; AliasSeq!(const TickDuration, immutable TickDuration)) - { - T t = TickDuration.currSystemTick; - assert(!__traits(compiles, t *= 12)); - assert(!__traits(compiles, t *= 12.0)); - } - } - - - /++ - The legal types of arithmetic for $(D TickDuration) using this operator - overload are - - $(TABLE - $(TR $(TD TickDuration) $(TD /) $(TD long) $(TD -->) $(TD TickDuration)) - $(TR $(TD TickDuration) $(TD /) $(TD floating point) $(TD -->) $(TD TickDuration)) - ) - - Params: - value = The value to divide from this $(D TickDuration). - - Throws: - $(D TimeException) if an attempt to divide by $(D 0) is made. - +/ - void opOpAssign(string op, T)(T value) @safe pure - if (op == "/" && - (__traits(isIntegral, T) || __traits(isFloating, T))) - { - if (value == 0) - throw new TimeException("Attempted division by 0."); - - length = cast(long)(length / value); - } - - version (CoreUnittest) unittest - { - immutable curr = TickDuration.currSystemTick; - immutable t1 = curr; - TickDuration t2 = curr + curr; - t2 /= 2; - assert((t1 == t2) == true); - - t2 = curr + curr; - t2 /= 2.0; - immutable tol = TickDuration(cast(long)(_abs(t2.length) * double.epsilon / 2.0)); - assertApprox(t1, t2 - tol, t2 + tol); - - t2 = curr + curr; - t2 /= 2.1; - assert((t1 > t2) == true); - - _assertThrown!TimeException(t2 /= 0); - - foreach (T; AliasSeq!(const TickDuration, immutable TickDuration)) - { - T t = TickDuration.currSystemTick; - assert(!__traits(compiles, t /= 12)); - assert(!__traits(compiles, t /= 12.0)); - } - } - - - /++ - The legal types of arithmetic for $(D TickDuration) using this operator - overload are - - $(TABLE - $(TR $(TD TickDuration) $(TD *) $(TD long) $(TD -->) $(TD TickDuration)) - $(TR $(TD TickDuration) $(TD *) $(TD floating point) $(TD -->) $(TD TickDuration)) - ) - - Params: - value = The value to divide from this $(D TickDuration). - +/ - TickDuration opBinary(string op, T)(T value) @safe const pure nothrow @nogc - if (op == "*" && - (__traits(isIntegral, T) || __traits(isFloating, T))) - { - return TickDuration(cast(long)(length * value)); - } - - version (CoreUnittest) unittest - { - foreach (T; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - T t1 = TickDuration.currSystemTick; - T t2 = t1 + t1; - assert((t1 * 2 == t2) == true); - immutable tol = TickDuration(cast(long)(_abs(t1.length) * double.epsilon * 2.0)); - assertApprox(t1 * 2.0, t2 - tol, t2 + tol); - assert((t1 * 2.1 > t2) == true); - } - } - - - /++ - The legal types of arithmetic for $(D TickDuration) using this operator - overload are - - $(TABLE - $(TR $(TD TickDuration) $(TD /) $(TD long) $(TD -->) $(TD TickDuration)) - $(TR $(TD TickDuration) $(TD /) $(TD floating point) $(TD -->) $(TD TickDuration)) - ) - - Params: - value = The value to divide from this $(D TickDuration). - - Throws: - $(D TimeException) if an attempt to divide by $(D 0) is made. - +/ - TickDuration opBinary(string op, T)(T value) @safe const pure - if (op == "/" && - (__traits(isIntegral, T) || __traits(isFloating, T))) - { - if (value == 0) - throw new TimeException("Attempted division by 0."); - - return TickDuration(cast(long)(length / value)); - } - - version (CoreUnittest) unittest - { - foreach (T; AliasSeq!(TickDuration, const TickDuration, immutable TickDuration)) - { - T t1 = TickDuration.currSystemTick; - T t2 = t1 + t1; - assert((t2 / 2 == t1) == true); - immutable tol = TickDuration(cast(long)(_abs(t2.length) * double.epsilon / 2.0)); - assertApprox(t2 / 2.0, t1 - tol, t1 + tol); - assert((t2 / 2.1 < t1) == true); - - _assertThrownDep!TimeException(t2 / 0); - } - } - - - /++ - Params: - ticks = The number of ticks in the TickDuration. - +/ - @safe pure nothrow @nogc this(long ticks) - { - this.length = ticks; - } - - version (CoreUnittest) unittest - { - foreach (i; [-42, 0, 42]) - assert(TickDuration(i).length == i); - } - - - /++ - The current system tick. The number of ticks per second varies from - system to system. $(D currSystemTick) uses a monotonic clock, so it's - intended for precision timing by comparing relative time values, not for - getting the current system time. - - On Windows, $(D QueryPerformanceCounter) is used. On Mac OS X, - $(D mach_absolute_time) is used, while on other Posix systems, - $(D clock_gettime) is used. If $(D mach_absolute_time) or - $(D clock_gettime) is unavailable, then Posix systems use - $(D gettimeofday) (the decision is made when $(D TickDuration) is - compiled), which unfortunately, is not monotonic, but if - $(D mach_absolute_time) and $(D clock_gettime) aren't available, then - $(D gettimeofday) is the best that there is. - - $(RED Warning): - On some systems, the monotonic clock may stop counting when - the computer goes to sleep or hibernates. So, the monotonic - clock could be off if that occurs. This is known to happen - on Mac OS X. It has not been tested whether it occurs on - either Windows or on Linux. - - Throws: - $(D TimeException) if it fails to get the time. - +/ - static @property TickDuration currSystemTick() @trusted nothrow @nogc - { - import core.internal.abort : abort; - version (Windows) - { - ulong ticks = void; - QueryPerformanceCounter(cast(long*)&ticks); - return TickDuration(ticks); - } - else version (Darwin) - { - static if (is(typeof(mach_absolute_time))) - return TickDuration(cast(long)mach_absolute_time()); - else - { - timeval tv = void; - gettimeofday(&tv, null); - return TickDuration(tv.tv_sec * TickDuration.ticksPerSec + - tv.tv_usec * TickDuration.ticksPerSec / 1000 / 1000); - } - } - else version (Posix) - { - static if (is(typeof(clock_gettime))) - { - timespec ts = void; - immutable error = clock_gettime(CLOCK_MONOTONIC, &ts); - // CLOCK_MONOTONIC is supported and if tv_sec is long or larger - // overflow won't happen before 292 billion years A.D. - static if (ts.tv_sec.max < long.max) - { - if (error) - { - import core.internal.abort : abort; - abort("Call to clock_gettime failed."); - } - } - return TickDuration(ts.tv_sec * TickDuration.ticksPerSec + - ts.tv_nsec * TickDuration.ticksPerSec / 1000 / 1000 / 1000); - } - else - { - timeval tv = void; - gettimeofday(&tv, null); - return TickDuration(tv.tv_sec * TickDuration.ticksPerSec + - tv.tv_usec * TickDuration.ticksPerSec / 1000 / 1000); - } - } - } - - version (CoreUnittest) @safe nothrow unittest - { - assert(TickDuration.currSystemTick.length > 0); - } -} - /++ Generic way of converting between two time units. Conversions to smaller units use truncating division. Years and months can be converted to each @@ -3720,26 +2711,12 @@ Duration abs(Duration duration) @safe pure nothrow @nogc return Duration(_abs(duration._hnsecs)); } -/++ Ditto +/ -deprecated("TickDuration has been deprecated, please use Duration or MonoTime instead") -TickDuration abs(TickDuration duration) @safe pure nothrow @nogc -{ - return TickDuration(_abs(duration.length)); -} - unittest { assert(abs(dur!"msecs"(5)) == dur!"msecs"(5)); assert(abs(dur!"msecs"(-5)) == dur!"msecs"(5)); } -deprecated unittest -{ - assert((abs(TickDuration(17)) == TickDuration(17)) == true); - assert((abs(TickDuration(-17)) == TickDuration(17)) == true); -} - - //============================================================================== // Private Section. // @@ -4114,22 +3091,6 @@ version (CoreUnittest) void assertApprox(D, E)(D actual, throw new AssertError(msg ~ ": upper: " ~ actual.toString(), __FILE__, line); } -version (CoreUnittest) deprecated void assertApprox(D, E)(D actual, - E lower, - E upper, - string msg = "unittest failure", - size_t line = __LINE__) - if (is(D : const TickDuration) && is(E : const TickDuration)) -{ - if (actual.length < lower.length || actual.length > upper.length) - { - throw new AssertError(msg ~ (": [" ~ signedToTempString(lower.length) ~ "] [" ~ - signedToTempString(actual.length) ~ "] [" ~ - signedToTempString(upper.length) ~ "]").idup, - __FILE__, line); - } -} - version (CoreUnittest) void assertApprox(MT)(MT actual, MT lower, MT upper,