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 #387 from jmdavis/time
Browse files Browse the repository at this point in the history
Implement issue# 9337: There's no Duration.max.
  • Loading branch information
andralex committed Jan 18, 2013
2 parents e1d9e4d + cc2b137 commit e5c84ff
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions src/core/time.d
Expand Up @@ -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:
--------------------
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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);
}


Expand Down

0 comments on commit e5c84ff

Please sign in to comment.