fs: fix nanosecond precision and rounding in uv__fs_to_timespec
Add proper rounding to nearest nanosecond and handle overflow when rounding pushes tv_nsec beyond 1e9.
This commit is contained in:
parent
8c4198fbb0
commit
7a1ce4e1ef
@ -222,12 +222,16 @@ static struct timespec uv__fs_to_timespec(double time) {
|
||||
return (struct timespec){UTIME_OMIT, UTIME_OMIT};
|
||||
|
||||
ts.tv_sec = time;
|
||||
ts.tv_nsec = (time - ts.tv_sec) * 1e9;
|
||||
ts.tv_nsec = (long)((time - ts.tv_sec) * 1e9 + 0.5);
|
||||
|
||||
if (ts.tv_nsec < 0) {
|
||||
ts.tv_nsec += 1e9;
|
||||
ts.tv_sec -= 1;
|
||||
}
|
||||
if (ts.tv_nsec >= 1e9) {
|
||||
ts.tv_nsec -= 1e9;
|
||||
ts.tv_sec += 1;
|
||||
}
|
||||
return ts;
|
||||
}
|
||||
#endif
|
||||
|
||||
Loading…
Reference in New Issue
Block a user