summaryrefslogtreecommitdiff
path: root/lib/dtotimespec.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/dtotimespec.c')
-rw-r--r--lib/dtotimespec.c30
1 files changed, 10 insertions, 20 deletions
diff --git a/lib/dtotimespec.c b/lib/dtotimespec.c
index ecce2e5bcc5..064f7d3a0a9 100644
--- a/lib/dtotimespec.c
+++ b/lib/dtotimespec.c
@@ -29,41 +29,31 @@
struct timespec
dtotimespec (double sec)
{
- enum { BILLION = 1000 * 1000 * 1000 };
double min_representable = TYPE_MINIMUM (time_t);
double max_representable =
- ((TYPE_MAXIMUM (time_t) * (double) BILLION + (BILLION - 1))
- / BILLION);
- struct timespec r;
+ ((TYPE_MAXIMUM (time_t) * (double) TIMESPEC_RESOLUTION
+ + (TIMESPEC_RESOLUTION - 1))
+ / TIMESPEC_RESOLUTION);
if (! (min_representable < sec))
- {
- r.tv_sec = TYPE_MINIMUM (time_t);
- r.tv_nsec = 0;
- }
+ return make_timespec (TYPE_MINIMUM (time_t), 0);
else if (! (sec < max_representable))
- {
- r.tv_sec = TYPE_MAXIMUM (time_t);
- r.tv_nsec = BILLION - 1;
- }
+ return make_timespec (TYPE_MAXIMUM (time_t), TIMESPEC_RESOLUTION - 1);
else
{
time_t s = sec;
- double frac = BILLION * (sec - s);
+ double frac = TIMESPEC_RESOLUTION * (sec - s);
long ns = frac;
ns += ns < frac;
- s += ns / BILLION;
- ns %= BILLION;
+ s += ns / TIMESPEC_RESOLUTION;
+ ns %= TIMESPEC_RESOLUTION;
if (ns < 0)
{
s--;
- ns += BILLION;
+ ns += TIMESPEC_RESOLUTION;
}
- r.tv_sec = s;
- r.tv_nsec = ns;
+ return make_timespec (s, ns);
}
-
- return r;
}