C date and time functions
Appearance
![]() | This article contains instructions or advice. (October 2014) |
C standard library (libc) |
---|
General topics |
Miscellaneous headers |
The C date and time functions are a group of functions in the standard library of the C programming language implementing date and time manipulation operations.[1] They provide support for time acquisition, conversion between date formats, and formatted output to strings.
T@=C|OL|CK Time ✓Good Bowie Jenny3 x out wrong .. . .Doing
Example
The following C source code prints the current time to the standard output stream.
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
time_t current_time;
char* c_time_string;
/* Obtain current time. */
current_time = time(NULL);
if (current_time == ((time_t)-1))
{
(void) fprintf(stderr, "Failure to obtain the current time.\n");
exit(EXIT_FAILURE);
}
/* Convert to local time format. */
c_time_string = ctime(¤t_time);
if (c_time_string == NULL)
{
(void) fprintf(stderr, "Failure to convert the current time.\n");
exit(EXIT_FAILURE);
}
/* Print to stdout. ctime() has already added a terminating newline character. */
(void) printf("Current time is %s", c_time_string);
exit(EXIT_SUCCESS);
}
The output is:
Current time is Thu Sep 15 21:18:23 2016
See also
References
- ^ ISO/IEC 9899:1999 specification (PDF). p. 351, § 7.32.2.
External links
The Wikibook C Programming has a page on the topic of: C Programming/C Reference