188 Chapter 10
Figure 10-1: Functions for retrieving and working with calendar time
10.2.1 Converting time_t to Printable Form
The ctime() function provides a simple method of converting a time_t value into
printable form.
Given a pointer to a time_t value in timep, ctime() returns a 26-byte string containing
the date and time in a standard format, as illustrated by the following example:
Wed Jun 8 14:22:34 2011
The string includes a terminating newline character and a terminating null byte. The
ctime() function automatically accounts for local timezone and DST settings when
performing the conversion. (We explain how these settings are determined in
Section 10.3.) The returned string is statically allocated; future calls to ctime() will
overwrite it.
SUSv3 states that calls to any of the functions ctime(), gmtime(), localtime(), or
asctime() may overwrite the statically allocated structure that is returned by any of
the other functions. In other words, these functions may share single copies of the
returned character array and tm structure, and this is done in some versions of
glibc. If we need to maintain the returned information across multiple calls to these
functions, we must save local copies.
asctime()
strftime()*+
strptime()+
gmtime()
localtime()*
mktime()*
time() stime() gettimeofday() settimeofday()
ctime()*
Functions affected as marked:
* by TZ environment variable
+ by locale setting
time_t
(calendar time)
struct tm
(broken-down time)
struct timeval
fixed-format string
Tue Feb 1 21:39:46 2011\n\0
user-formatted,
localized string
Kernel
#include <time.h>
char *ctime(const time_t *timep);
Returns pointer to statically allocated string terminated
by newline and \0 on success, or NULL on error