Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

time — obtaining the current calendar time

The time function from <time.h> returns an approximation of current calendar time as time_t; check for (time_t)(-1).

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

Syntax

#include <time.h>
time_t time(time_t *timer);

time returns the implementation’s best approximation to the current calendar time. When time is unavailable, it returns (time_t)(-1). Check this result before using the value.

Calling time(NULL) only returns the result. With a non-null pointer, as in time(&stored), the function also stores the returned value in the pointed-to time_t object.

The time encoding is unspecified: portable code cannot assume seconds since the Unix epoch. The example compares the stored and returned results instead of printing a changing date.

Examples

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

int main(void)
{
    time_t stored;
    time_t result = time(&stored);

    if (result == (time_t)(-1)) {
        puts("Time unavailable");
        return 1;
    }

    puts(stored == result ? "Values match" : "Values differ");
    return 0;
}

Result

Values match

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