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:
AyushCodes160 2025-12-01 12:25:34 +05:30
parent 8c4198fbb0
commit 7a1ce4e1ef

View File

@ -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