Obtaining the current time

Querying the current time is, at first glance, straightforward. Many programmers use the following API sequence to achieve it:

time(2)localtime(3)strftime(3)

We do not. Why is this? Recall our discussion on the async-signal-safe (reentrant) functions in the first of Chapters 11, Signaling - Part I, (within the section Reentrant Safety and Signaling). Of the aforementioned three APIs, only the time(2) API is considered signal-safe; the other two are not (that is, they should not be used within a signal handler). The relevant man page (signal-safety(7)) confirms this.

Hence, we use documented async-signal-safe APIs-the time(2), clock_gettime(2) and ctime_r(3)—to perform the role of obtaining the timestamp safely. ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.