< C++ Programming < Code < Standard C Library

Standard C Time & Date

This section will cover the Time and Date elements of the C Standard Library.

asctime

Syntax
#include <ctime>
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

An example:

Mon Jun 26 12:03:53 2000
Related topics
clock - ctime - difftime - gmtime - localtime - mktime - time

clock

Syntax
#include <ctime>
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_SEC.

Related topics
asctime - ctime - time

ctime

Syntax
#include <ctime>
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            

using ctime() is equivalent to

asctime( localtime( tp ) );
Related topics
asctime - clock - gmtime - localtime - mktime - time

difftime

Syntax
#include <ctime>
double difftime( time_t time2, time_t time1 );

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

Related topics
asctime - gmtime - localtime - time

gmtime

Syntax
#include <ctime>
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. Watch out for the static return.

Related topics
asctime - ctime - difftime - localtime - mktime - strftime - time

localtime

Syntax
#include <ctime>
struct tm *localtime( const time_t *time );

The function localtime() converts calendar time time into local time. Watch out for the static return.

Related topics
asctime - ctime - difftime - gmtime - strftime - time

mktime

Syntax
#include <ctime>
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
asctime - ctime - gmtime - time

setlocale

Syntax
#include <clocale>
char *setlocale( int category, const char * locale );

The setlocale() function is used to set and retrieve the current locale. If locale is NULL, the current locale is returned. Otherwise, locale is used to set the locale for the given category.

category can have the following values:

ValueDescription
LC_ALLAll of the locale
LC_TIMEDate and time formatting
LC_NUMERICNumber formatting
LC_COLLATEString collation and regular expression matching
LC_CTYPERegular expression matching, conversion, case-sensitive comparison, wide character functions, and character classification.
LC_MONETARYFor monetary formatting
LC_MESSAGESFor natural language messages
Related topics
(Standard C String & Character) strcoll

strftime

Syntax
#include <ctime>
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 (e.g. Fri)
%Afull weekday name (e.g. Friday)
%babbreviated month name (e.g. Oct)
%Bfull month name (e.g. October)
%cthe standard date and time string
%dday of the month, as a number (01-31) with a leading zero
%-dday of the month, as a number (1-31) without a leading zero
%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).
%Mminute as a number (0-59)
%plocale's equivalent of AM or PM
%Ssecond as a number (0-59)
%Uweek of the year, (0-53), where week 1 has the first Sunday
%wweekday as a decimal (0-6), where Sunday is 0
%Wweek of the year, (0-53), where week 1 has the first Monday
%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
Related topics
gmtime - localtime - time

time

Syntax
#include <ctime>
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
asctime - clock - ctime - difftime - gmtime - localtime - mktime - strftime
(Other Standard C functions) srand - the preprocessor macros __DATE__ and __TIME__
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.