networking · beginner · ~15 min
Split milliseconds into the seconds field.
A socket timeout option like SO_RCVTIMEO takes a struct timeval, which splits a duration into seconds (tv_sec) and microseconds (tv_usec). Compute the whole-seconds part of a millisecond duration.
Implement long ms_to_sec(long ms) that returns the whole-seconds portion of ms (for the tv_sec field).
One long ms — a duration in milliseconds.
Returns the number of whole seconds in ms.
ms_to_sec(1500) -> 1
ms_to_sec(999) -> 0
ms_to_sec(3000) -> 3
One long: a duration in milliseconds.
The whole-seconds part of ms.
Integer-divide milliseconds by 1000.
long ms_to_sec(long ms) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.