networking · beginner · ~15 min

Milliseconds to timeval microseconds

Compute the tv_usec remainder.

Challenge

The companion to the seconds split: compute the sub-second remainder of a millisecond duration, expressed in microseconds, for the tv_usec field of a struct timeval.

Task

Implement long ms_to_usec(long ms) that returns the leftover microseconds — the part of ms below a whole second, converted to microseconds.

Input

One long ms — a duration in milliseconds.

Output

Returns (ms % 1000) * 1000, the sub-second remainder in microseconds.

Example

ms_to_usec(1500)   ->   500000
ms_to_usec(3000)   ->   0
ms_to_usec(250)    ->   250000

Edge cases

  • An exact number of seconds (e.g. 3000 ms) leaves 0 microseconds.

Input format

One long: a duration in milliseconds.

Output format

The sub-second remainder of ms in microseconds.

Constraints

Take ms modulo 1000, then multiply by 1000 to convert to microseconds.

Starter code

long ms_to_usec(long ms) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.