C date and time functions
Appearance
C standard library (libc) |
---|
General topics |
Miscellaneous headers |
C date and time functions refer to 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.
Overview of functions
The C date and time operations are defined in the time.h
header file (ctime
header in C++).
Identifier | Description | |
---|---|---|
Time manipulation |
|
computes the difference between times |
|
returns the current time of the system as time since epoch (which is usually Unix epoch) | |
|
returns a processor tick count associated with the process | |
Format conversions |
|
converts a tm object to a textual representation
|
|
converts a tm object to a textual representation
| |
|
converts a tm object to custom textual representation
| |
|
converts a tm object to custom wide string textual representation
| |
|
converts time since epoch to calendar time expressed as Universal Coordinated Time | |
|
converts time since epoch to calendar time expressed as local time | |
|
converts calendar time to time since epoch | |
Constants | CLOCKS_PER_SEC
|
number of processor clock ticks per second |
Types | tm
|
calendar time type |
time_t
|
time since epoch type | |
clock_t
|
process running time |
Example
The following C source code snippet prints the current time to the standard output stream.
#include <stdio.h>
#include <time.h>
int main()
{
/* Obtain current time as seconds elapsed since the Epoch. */
time_t clock = time(NULL);
/* Convert to local time format and print to stdout. */
printf("Current time is %s", ctime(&clock));
return 0;
}
The output is:
Current time is Sat Dec 31 11:20:45 2011
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