cppreference.com -> Standard C Time & Date -> Details

Standard C Date & Time


asctime

Syntax:

  #include <time.h>
  char *asctime( const struct tm *ptr );

The function asctime() converts the time in the struct ptr to a character string of the following format:

    day month date hours:minutes:seconds year\n\0

An example:

    Mon Jun 26 12:03:53 2000
Related topics:
localtime(), gmtime(), time(), and ctime().

clock

Syntax:

  #include <time.h>
  clock_t clock( void );

The clock() function returns the processor time since the program started, or -1 if that information is unavailable. To convert the return value to seconds, divide it by CLOCKS_PER_SECOND. (Note: if your compiler is POSIX compliant, then CLOCKS_PER_SECOND is always defined as 1000000.)

Related topics:
time(), asctime(), and ctime().

ctime

Syntax:

  #include <time.h>
  char *ctime( const time_t *time );

The ctime() function converts the calendar time time to local time of the format:

    day month date hours:minutes:seconds year\n\0

using ctime() is equivalent to

    asctime( localtime( tp ) );
Related topics:
localtime(), gmtime(), time(), and asctime().

difftime

Syntax:

  #include <time.h>
  double difftime( time_t time2, time_t time1 );

The function difftime() returns time2-time1, in seconds.

Related topics:
localtime(), gmtime(), time(), and asctime().

gmtime

Syntax:

  #include <time.h>
  struct tm *gmtime( const time_t *time );

The gmtime() function returns the given time in Coordinated Universal Time (usually Greenwich mean time), unless it's not supported by the system, in which case NULL is returned. Warning!

Related topics:
localtime(), time(), and asctime().

localtime

Syntax:

  #include <time.h>
  struct tm *localtime( const time_t *time );

The function localtime() converts calendar time time into local time. Warning!

Related topics:
gmtime(), time(), and asctime().

mktime

Syntax:

  #include <time.h>
  time_t mktime( struct tm *time );

The mktime() function converts the local time in time to calendar time, and returns it. If there is an error, -1 is returned.

Related topics:
time(), gmtime(), asctime(), and ctime().

strftime

Syntax:

  #include <time.h>
  size_t strftime( char *str, size_t maxsize, const char *fmt, struct tm *time );

The function strftime() formats date and time information from time to a format specified by fmt, then stores the result in str (up to maxsize characters). Certain codes may be used in fmt to specify different types of time:

CodeMeaning
%aabbreviated weekday name
%Afull weekday name
%babbreviated month name
%Bfull month name
%cthe standard date and time string
%dday of the month, as a number (1-31)
%Hhour, 24 hour format (0-23)
%Ihour, 12 hour format (1-12)
%jday of the year, as a number (1-366)
%mmonth as a number (1-12). Note: some versions of Microsoft Visual C++ may use values that range from 0-11.
%Mminute as a number (0-59)
%plocale's equivalent of AM or PM
%Ssecond as a number (0-59)
%Uweek of the year, sunday as the first day
%wweekday as a decimal (0-6, sunday=0)
%Wweek of the year, monday as the first day
%xstandard date string
%Xstandard time string
%yyear in decimal, without the century (0-99)
%Yyear in decimal, with the century
%Ztime zone name
%%a percent sign

The strftime() function returns the number of characters put into str, or zero if an error occurs.

Related topics:
time(), localtime(), and gmtime().

time

Syntax:

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

The function time() returns the current time, or -1 if there is an error. If the argument time is given, then the current time is stored in time.

Related topics:
localtime(), gmtime(), strftime(), ctime(),