Would this work?:

```c++
if (ts.tv_nsec < 0 || ts.tv_nsec >= 1e9)
{
  ts.tv_sec += ts.tv_nsec / 1e9;
  ts.tv_nsec = ts.tv_nsec % 1e9;
}
```