6.6 Summary
Times are stored internally as time_t values, representing “seconds since the Epoch.” The Epoch is midnight, January 1, 1970, UTC, for GNU/Linux and Unix systems. The current time is retrieved from the system by the time() system call, and difftime() returns the difference, in seconds, between two time_t values.
The struct tm structure represents a “broken-down time,” which is a much more usable representation of a date and time. gmtime() and localtime() convert time_t values into struct tm values, and mktime() goes in the opposite direction.
asctime() and ctime() do simplistic formatting of time values, returning a pointer to a fixed-size, fixed-format static character string. strftime() provides much more flexible formatting, including locale-based values. The strptime() function parses a formatted date and time, filling in the members of a struct tm.
Time-zone information is made available by a call to tzset(). Since the standard routines act as if they call tzset() automatically, it is rare to need to call this function directly.
The standard routine for sorting arrays is qsort(). By using a user-provided comparison function and being told the number of array elements and their size, qsort() can sort any kind of data. This provides considerable flexibility.
scandir() reads an entire directory into an array of struct dirent. User-provided functions can be used to select which entries to include and can provide ordering of elements within the array. alphasort() is a standard function for sorting directory entries by name; scandir() passes the comparison function straight through to qsort().
The bsearch() function works similarly to qsort(). It does fast binary searching. Use it if the cost of linear searching outweighs the cost of sorting your data. (An additional API for searching data collections is described in Section 16.4, “Advanced Searching with Binary Trees,” page 575.)
The user and group databases may be kept in local disk files or may be made available over a network. The standard API purposely hides this distinction. Each database provides both linear scanning of the entire database and direct queries for a user/group name or user/group ID.
Finally, for those times when stat() just isn’t enough, isatty() can tell you whether or not an open file represents a terminal device.
