rounding timestamps (or not)

Jeremy Allison jra at
Mon Sep 13 15:21:39 MDT 2010

On Mon, Sep 13, 2010 at 12:11:23PM +0200, Björn Jacke wrote:
> Hi,
> in round_timespec() from source3/lib/time.c there is rounding being done for
> second resolution but for usec resulition therer is only being truncated.
> Currently the only user of round_timespec() is smb_set_file_time. Even if it's
> it looks like one microsecond more or less make no big difference here, the
> rounding at this place should probably be made consistent.

This is a more subtle problem than it looks. It depends
what the kernel does with a timespec where the number
of tv_nsec is greater than 1second. Hopefully it will
do the rounding correctly itself, so the code should
work as is.

The current code for round_timespec_to_usec essentially
divides the nsec value by 1000, then miltiplies by 1000,
essentially losing any resolution less than 1usec.

So I guess the patch to do this in userspace should be

-------------- next part --------------
diff --git a/source3/lib/time.c b/source3/lib/time.c
index fad5d97..eba358f 100644
--- a/source3/lib/time.c
+++ b/source3/lib/time.c
@@ -409,6 +409,10 @@ void round_timespec_to_usec(struct timespec *ts)
 	struct timeval tv = convert_timespec_to_timeval(*ts);
 	*ts = convert_timeval_to_timespec(tv);
+	while (ts->tv_nsec > 1000000000) {
+		ts->tv_sec += 1;
+		ts->tv_nsec -= 1000000000;
+	}

More information about the samba-technical mailing list