http: Fix use of deprecated function ftime()

Closed Mark Harris requested to merge markh/opusfile:fix-ftime into master

The ftime() function, introduced in V7 Unix (1979), gets the current time in seconds and milliseconds, and time zone information. It was marked as a legacy interface in POSIX.1-2001, and removed altogether from POSIX.1-2008. The gettimeofday() function, originally from 4.1 BSD, gets the current time in seconds and microseconds, and optional time zone information, and was marked as obsolete in POSIX.1-2008 although it was kept in the standard. The POSIX recommended function for getting time with sub-second resolution is clock_gettime(), which was introduced in POSIX.1b-1993 and is now part of the base POSIX standard; it supports multiple clocks and nanosecond resolution. Additionally the function timespec_get() was introduced in C11 and also supports nanosecond resolution.

To support dates beyond the year 2038, glibc and other libraries are being updated to support 64-bit time_t even on 32-bit architectures, requiring new implementations of interfaces that work with time. As part of this effort, the ftime() function was deprecated in glibc 2.31 (released February 1, 2020), a warning is now issued when building code that uses this function, and removal is planned for a future version of glibc (

ftime() is used in http.c to measure time intervals with millisecond resolution. To avoid the glibc 2.31 deprecation warning and further issues when the function is removed entirely from glibc, clock_gettime() is now used instead when it is available in the C library, as it is on current Linux systems. Prior to glibc 2.17, clock_gettime() required linking with librt; on such systems ftime() will continue to be used, to avoid an additional library dependency. macOS provides clock_gettime() starting in macOS 10.12; earlier versions will continue to use ftime(). Windows provides ftime() but not clock_gettime(), so ftime() will continue to be used on Windows.

ftime(), gettimeofday(), and clock_gettime() with the CLOCK_REALTIME clock get the "real time", which is subject to jumps if set by an administrator or time service. The CLOCK_MONOTONIC clock does not have this problem and is more suitable for measuring time intervals. On Linux, the CLOCK_BOOTTIME clock measures the time since last boot and is the same as CLOCK_MONOTONIC except that the Linux CLOCK_MONOTONIC clock does not advance when the system is suspended. Because it is used to measure time intervals, CLOCK_BOOTTIME or CLOCK_MONOTONIC are used when available, when clock_gettime() is used. However the only clock required by POSIX.1-2008 is CLOCK_REALTIME, so that will be used if the other clocks are not available.

Merge request reports