Skip to content

Commit

Permalink
Merge pull request #6785 from n8sh/issue-19453
Browse files Browse the repository at this point in the history
Fix Issue 19453 - Remove unnecessary error checks in std.datetime.systime.currStdTime()
merged-on-behalf-of: Sebastian Wilzbach <sebi.wilzbach@gmail.com>
  • Loading branch information
dlang-bot committed Dec 18, 2018
2 parents c20df1e + 0f20fa2 commit deac4ed
Showing 1 changed file with 57 additions and 22 deletions.
79 changes: 57 additions & 22 deletions std/datetime/systime.d
Expand Up @@ -217,11 +217,12 @@ public:
else
{
import core.sys.posix.sys.time : gettimeofday, timeval;
timeval tv;
if (gettimeofday(&tv, null) != 0)
throw new TimeException("Call to gettimeofday() failed");
timeval tv = void;
// Posix gettimeofday called with a valid timeval address
// and a null second parameter doesn't fail.
gettimeofday(&tv, null);
return convert!("seconds", "hnsecs")(tv.tv_sec) +
convert!("usecs", "hnsecs")(tv.tv_usec) +
tv.tv_usec * 10 +
hnsecsToUnixEpoch;
}
}
Expand All @@ -237,9 +238,16 @@ public:
else static if (clockType == ClockType.normal) alias clockArg = CLOCK_REALTIME;
else static if (clockType == ClockType.precise) alias clockArg = CLOCK_REALTIME;
else static assert(0, "Previous static if is wrong.");
timespec ts;
if (clock_gettime(clockArg, &ts) != 0)
throw new TimeException("Call to clock_gettime() failed");
timespec ts = void;
immutable error = clock_gettime(clockArg, &ts);
// Posix clock_gettime called with a valid address and valid clock_id is only
// permitted to fail if the number of seconds does not fit in time_t. 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)
throw new TimeException("Call to clock_gettime() failed");
}
return convert!("seconds", "hnsecs")(ts.tv_sec) +
ts.tv_nsec / 100 +
hnsecsToUnixEpoch;
Expand All @@ -254,9 +262,16 @@ public:
else static if (clockType == ClockType.precise) alias clockArg = CLOCK_REALTIME_PRECISE;
else static if (clockType == ClockType.second) alias clockArg = CLOCK_SECOND;
else static assert(0, "Previous static if is wrong.");
timespec ts;
if (clock_gettime(clockArg, &ts) != 0)
throw new TimeException("Call to clock_gettime() failed");
timespec ts = void;
immutable error = clock_gettime(clockArg, &ts);
// Posix clock_gettime called with a valid address and valid clock_id is only
// permitted to fail if the number of seconds does not fit in time_t. 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)
throw new TimeException("Call to clock_gettime() failed");
}
return convert!("seconds", "hnsecs")(ts.tv_sec) +
ts.tv_nsec / 100 +
hnsecsToUnixEpoch;
Expand All @@ -267,12 +282,18 @@ public:
return unixTimeToStdTime(core.stdc.time.time(null));
else
{
import core.sys.posix.sys.time : gettimeofday, timeval;
timeval tv;
if (gettimeofday(&tv, null) != 0)
throw new TimeException("Call to gettimeofday() failed");
return convert!("seconds", "hnsecs")(tv.tv_sec) +
convert!("usecs", "hnsecs")(tv.tv_usec) +
import core.sys.netbsd.time : clock_gettime, CLOCK_REALTIME;
timespec ts = void;
// Posix clock_gettime called with a valid address and valid clock_id is only
// permitted to fail if the number of seconds does not fit in time_t. 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)
throw new TimeException("Call to clock_gettime() failed");
}
return convert!("seconds", "hnsecs")(ts.tv_sec) +
ts.tv_nsec / 100 +
hnsecsToUnixEpoch;
}
}
Expand All @@ -285,9 +306,16 @@ public:
else static if (clockType == ClockType.precise) alias clockArg = CLOCK_REALTIME_PRECISE;
else static if (clockType == ClockType.second) alias clockArg = CLOCK_SECOND;
else static assert(0, "Previous static if is wrong.");
timespec ts;
if (clock_gettime(clockArg, &ts) != 0)
throw new TimeException("Call to clock_gettime() failed");
timespec ts = void;
immutable error = clock_gettime(clockArg, &ts);
// Posix clock_gettime called with a valid address and valid clock_id is only
// permitted to fail if the number of seconds does not fit in time_t. 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)
throw new TimeException("Call to clock_gettime() failed");
}
return convert!("seconds", "hnsecs")(ts.tv_sec) +
ts.tv_nsec / 100 +
hnsecsToUnixEpoch;
Expand All @@ -303,9 +331,16 @@ public:
else static if (clockType == ClockType.normal) alias clockArg = CLOCK_REALTIME;
else static if (clockType == ClockType.precise) alias clockArg = CLOCK_REALTIME;
else static assert(0, "Previous static if is wrong.");
timespec ts;
if (clock_gettime(clockArg, &ts) != 0)
throw new TimeException("Call to clock_gettime() failed");
timespec ts = void;
immutable error = clock_gettime(clockArg, &ts);
// Posix clock_gettime called with a valid address and valid clock_id is only
// permitted to fail if the number of seconds does not fit in time_t. 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)
throw new TimeException("Call to clock_gettime() failed");
}
return convert!("seconds", "hnsecs")(ts.tv_sec) +
ts.tv_nsec / 100 +
hnsecsToUnixEpoch;
Expand Down

0 comments on commit deac4ed

Please sign in to comment.