Home > OS >  Why are some of Linux time APIs obsolete?
Why are some of Linux time APIs obsolete?

Time:10-15

Linux has a number of APIs that can be used to set system time. As far as I know, We can use time(), gettimeofday(),clock_gettime() to get time and stime(), settimeofday(),clock_settime() to set time.

However, I noticed some of them are marked as "deprecated" or "obsolete", but I couldn't find the reason behind it, especially: Why is stime() deprecated (source) but not time()? gettimeofday() offers higher resolution than time(), but it's the former rather than the latter that is "obsolete" (source). What are the reasons of these decisions, and how can I find more about why an API becomes deprecated?

CodePudding user response:

Why are some of Linux time APIs obsolete?

The usual reasons - are old, not secure, not portable, have unspecified/undefined behavior, tricky edge cases, better alternatives exists, etc. It's on case to case bases, there's no general rule.

Why is stime() deprecated (source)

It's a nonstandard function from SVr4 that nobody uses (on Linux). Use clock_settime.

but not time()?

time() is super standard function available everywhere, as it's the part of C langauge standard. It will be used till the end of... time!

gettimeofday() offers higher resolution than time(), but it's the former rather than the latter that is "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 t:

APPLICATION USAGE

Applications should use the clock_gettime() function instead of the obsolescent gettimeofday() function.

how can I find more about why an API becomes 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 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.
  • Related