Linux has a number of APIs that can be used to set system time: we can use time()
, gettimeofday()
,clock_gettime()
to get the time, and we can usestime()
, settimeofday()
,clock_settime()
to set the time.
However, I noticed that some of these APIs are marked as "deprecated" or "obsolete". I couldn't find the reason behind this deprecation. Specifically:
Why is stime()
deprecated (source) but not time()
? gettimeofday()
offers higher resolution than time()
, but the former rather than the latter was declared "obsolete" (source).
What are the reasons for these decisions?
CodePudding user response:
These APIs have been declared as "obsolete" for the usual reasons: they are old, not secure, not portable, have unspecified/undefined behavior, have tricky edge cases, better alternatives exist, etc.
Deprecation of APIs in Linux is generally done on a case-by-case basis; there is no general rule.
Why is
stime()
deprecated…
It's a non-standard function from SVr4
that nobody uses (on Linux). You should use clock_settime
instead in every case.
…but not
time()
?
time()
is a super standard function available everywhere, as it's the part of the C language standard. It will be used until the end of... time!
gettimeofday()
offers higher resolution thantime()
, but the former rather than the latter was declared "obsolete"
gettimeofday()
is a POSIX function, and POSIX standardized existing practices. After some time (POSIX Issue 7) clock_gettime()
was invented with greater resolution and to be more portable. Because a better and safer interface exists, POSIX noted that gettimeofday()
is obsolesced. I think the note is pretty self-explanatory to a developer:
APPLICATION USAGE
Applications should use the clock_gettime() function instead of the obsolescent gettimeofday() function.
In general, to find out why a particular API was deprecated, research about particular project history. Source code repositories offer commit history, standards, like POSIX or the C standard, come with "rationale", a separate document or included with the document, that explain the decisions.
For example, you can find out about stime()
deprecation in glibc from this commit:
* The obsolete function stime is no longer available to newly linked
binaries and it has been removed from <time.h> header. This function
has been deprecated in favor of clock_settime.