Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

time.h — date and time

The <time.h> header provides date and time types and functions. Distinguish processor time from calendar time.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <time.h>

<time.h> declares time_t, clock_t, struct tm, and struct timespec, among others. struct tm stores date components; strftime formats them.

CLOCKS_PER_SEC scales clock() results to processor-time seconds, not elapsed real time. Divide by (double)CLOCKS_PER_SEC; check for (clock_t)-1 failure.

TIME_UTC selects the UTC base in timespec_get(&ts, TIME_UTC): success returns TIME_UTC, failure returns 0. The epoch is implementation-defined, not necessarily Unix. A nanosecond field does not guarantee nanosecond resolution; UTC does not guarantee monotonicity.

Examples

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

int main(void)
{
    struct tm date = {
        .tm_year = 2024 - 1900,
        .tm_mon = 3 - 1,
        .tm_mday = 14
    };
    char text[32];

    if (strftime(text, sizeof text, "%Y-%m-%d", &date) == 0) {
        return 1;
    }
    puts(text);
    return 0;
}

Result

2024-03-14

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources