Jump to content

C date and time functions

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Zehdeh (talk | contribs) at 01:45, 26 April 2012 (Overview of functions: using the much more better wikipedia internal links SUS). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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
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 a processor tick count associated with the process
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

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

  1. ^ ISO/IEC 9899:1999 specification (PDF). p. 351, § 7.32.2.