Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Implement issue# 9337: There's no Duration.max. #387

Merged
merged 3 commits into from

2 participants

@ghost Show outdated diff Hide outdated diff Unknown commented on an outdated diff
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 so that it's not
@ghost
ghost added a note

Just to shorten it a bit: makes it so that it's not possible -> makes it impossible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
src/core/time.d
@@ -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); }
+
+ /++
+ Smallest $(D Duration) possible.
@andralex Owner

This may be confusing, I'm thinking "smallest" would be zero. How about adding a brief explanation a la "since duration may be negative, this is the largest negative duration possible".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
jmdavis added some commits
@jmdavis jmdavis Implement issue# 9337: There's no Duration.max.
Added Duration.max and Duration.min.
606a6e5
@jmdavis jmdavis Added TickDuration.max and TickDuration.min. f742648
@jmdavis jmdavis Some tweaks to Duration's documentation.
It erroneously mentioned addYears and addMonths as functions on
std.datetime.Date.
cc2b137
@jmdavis
Collaborator

Adjustments made per suggestions.

@andralex andralex merged commit e5c84ff into D-Programming-Language:master
@andralex
Owner

merged -- thanks!

@andralex
Owner

@jmdavis One more thought - wouldn't it be even cheaper if we made min and max enums?

@jmdavis
Collaborator

@andralex > One more thought - wouldn't it be even cheaper if we made min and max enums?

I hadn't thought of that. However, that doesn't seem to work. I get errors like this

src/core/time.d(116): Error: cannot create a struct until its size is determined

when I try and make either zero, min, or max into enums, even if I put them at the end of the struct. That seems rather odd to me given that the enum isn't actually part of the struct - just part of its namespace really - but apparently it doesn't work right now.

@ghost Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@ghost Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@ghost Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@ghost Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 18, 2013
  1. @jmdavis

    Implement issue# 9337: There's no Duration.max.

    jmdavis authored
    Added Duration.max and Duration.min.
  2. @jmdavis
  3. @jmdavis

    Some tweaks to Duration's documentation.

    jmdavis authored
    It erroneously mentioned addYears and addMonths as functions on
    std.datetime.Date.
This page is out of date. Refresh to see the latest.
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);
}
Something went wrong with that request. Please try again.