networking · beginner · ~15 min

Milliseconds to timeval seconds

Split milliseconds into the seconds field.

Challenge

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.

Task

Implement long ms_to_sec(long ms) that returns the whole-seconds portion of ms (for the tv_sec field).

Input

One long ms — a duration in milliseconds.

Output

Returns the number of whole seconds in ms.

Example

ms_to_sec(1500)   ->   1
ms_to_sec(999)    ->   0
ms_to_sec(3000)   ->   3

Edge cases

  • Durations under 1000 ms give 0 whole seconds.

Input format

One long: a duration in milliseconds.

Output format

The whole-seconds part of ms.

Constraints

Integer-divide milliseconds by 1000.

Starter code

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.