Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #387 from jmdavis/time

Implement issue# 9337: There's no Duration.max.
  • Loading branch information...
commit e5c84ff3caade03730bbff8055779801448568b7 2 parents e1d9e4d + cc2b137
@andralex andralex authored
Showing with 44 additions and 9 deletions.
  1. +44 −9 src/core/time.d
View
53 src/core/time.d
@@ -73,15 +73,15 @@ ulong mach_absolute_time();
Use the $(LREF dur) function or on of its non-generic aliases to create
$(D Duration)s.
- You cannot create a duration of months or years because the variable number
- of days in a month or a year makes it so that you cannot convert between
- months or years and smaller units without a specific date. Any type or
- function which handles months or years has other functions for handling
- those rather than using durations. For instance, $(XREF datetime, Date) has
- $(D addYears) and $(D addMonths) for adding years and months, rather than
- creating a duration of years or months and adding that to a
- $(XREF datetime, Date). If you're dealing with weeks or smaller, however,
- durations are what you use.
+ It's not possible to create a Duration of months or years, because the
+ variable number of days in a month or year makes it impossible to convert
+ between months or years and smaller units without a specific date. So,
+ nothing uses $(D Duration)s when dealing with months or years. Rather,
+ functions specific to months and years are defined. For instance,
+ $(XREF datetime, Date) has $(D add!"years") and $(D add!"months") for adding
+ years and months rather than creating a Duration of years or months and
+ adding that to a $(XREF datetime, Date). But Duration is used when dealing
+ with weeks or smaller.
Examples:
--------------------
@@ -115,11 +115,29 @@ public:
+/
static @property @safe pure nothrow Duration zero() { return Duration(0); }
+ /++
+ Largest $(D Duration) possible.
+ +/
+ static @property @safe pure nothrow Duration max() { return Duration(long.max); }
+
+ /++
+ Most negative $(D Duration) possible.
+ +/
+ static @property @safe pure nothrow Duration min() { return Duration(long.min); }
+
unittest
{
assert(zero == dur!"seconds"(0));
+ assert(Duration.max == Duration(long.max));
+ assert(Duration.min == Duration(long.min));
+ assert(Duration.min < Duration.zero);
+ assert(Duration.zero < Duration.max);
+ assert(Duration.min < Duration.max);
+ assert(Duration.min - dur!"hnsecs"(1) == Duration.max);
+ assert(Duration.max + dur!"hnsecs"(1) == Duration.min);
}
+
/++
Compares this $(D Duration) with the given $(D Duration).
@@ -1468,9 +1486,26 @@ struct TickDuration
+/
static @property @safe pure nothrow TickDuration zero() { return TickDuration(0); }
+ /++
+ Largest $(D TickDuration) possible.
+ +/
+ static @property @safe pure nothrow TickDuration max() { return TickDuration(long.max); }
+
+ /++
+ Most negative $(D TickDuration) possible.
+ +/
+ static @property @safe pure nothrow TickDuration min() { return TickDuration(long.min); }
+
unittest
{
assert(zero == TickDuration(0));
+ assert(TickDuration.max == TickDuration(long.max));
+ assert(TickDuration.min == TickDuration(long.min));
+ assert(TickDuration.min < TickDuration.zero);
+ assert(TickDuration.zero < TickDuration.max);
+ assert(TickDuration.min < TickDuration.max);
+ assert(TickDuration.min - TickDuration(1) == TickDuration.max);
+ assert(TickDuration.max + TickDuration(1) == TickDuration.min);
}
Please sign in to comment.
Something went wrong with that request. Please try again.