C date and time functions

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.

Contents

Function overview

The C date and time operations are defined in the time.h header file (ctime header in C++).

Time manipulation
difftime - computes the difference between times
time - returns the current time of the system as time since epoch (which is usually Unix epoch)
clock - returns raw processor clock time since the program is started
Format conversions
asctime - converts a tm object to a textual representation
ctime - converts a tm object to a textual representation
strftime - converts a tm object to custom textual representation
wcsftime - converts a tm object to custom wide string textual representation
gmtime - converts time since epoch to calendar time expressed as Universal Coordinated Time
localtime - converts time since epoch to calendar time expressed as local time
mktime - 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

This source code snippet prints the current time to the standard output stream.

#include <stdio.h>
#include <time.h>
 
int main(void)
{
    time_t timer = time(NULL);
    printf("current time is %s", ctime(&timer));
    return 0;
}
 
/*
Output is:
current time is Sat Dec 31 11:20:45 2011
*/

References