Time t

From Wikipedia, the free encyclopedia

The correct title of this article is time_t. The initial letter is shown capitalized due to technical restrictions.

The time_t datatype is a data type in the ISO C library defined for storing system time values. Such values are returned from the standard time() library function. This type is a typedef defined in the standard <time.h> header. ISO C defines time_t as an arithmetic type, but does not specify any particular type, range, resolution, or encoding for it. Also unspecified are the meanings of arithmetic operations applied to time values.

Unix and POSIX-compliant systems implement the time_t type as a signed integer (typically 32 or 64 bits wide) which represents the number of seconds since the start of the Unix epoch (ignoring leap seconds). Some systems support negative time values, while others do not.

In addition to the time() function, ISO C also specifies other functions and types for converting time_t system time values into calendar times and vice versa.

[edit] Example

The following C code retrieves the current time, formats it as a string, and writes it to the standard output.

#include <stdio.h>
#include <time.h>

int main(void)
{
    time_t      now;
    struct tm   ts;
    char        buf[80];

    // Get the current time
    time(&now);

    // Format and print the time, "ddd yyyy-mm-dd hh:mm:ss zzz"
    ts = *localtime(&now);
    strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
    printf("%s\n", buf);

    return 0;
}

[edit] See also

In other languages