This item was added on: 2003/03/22
C and C++ offer extensive date and time manipulation functions in the <time.h> and <ctime> headers, respectively. The functions that can be used are as follows:
clock_t clock ( void );
double difftime ( time_t time1, time_t time0 );
time_t mktime ( struct tm *timeptr );
time_t time ( time_t *timer );
char *asctime ( const struct tm *timeptr );
char *ctime ( const time_t *timer );
struct tm *gmtime ( const time_t *timer );
struct tm *localtime ( const time_t *timer );
size_t strftime ( char *s, size_t maxsize, const char *format,
const struct tm *timeptr );
The time header creates four types and two macros for use with these functions:
NULL
CLOCKS_PER_SEC
size_t
time_t
clock_t
struct tm
The clock function measures CPU execution time since the beginning of an implementation defined era related to the program's invocation. To convert the return value of clock to seconds, divide the difference by CLOCKS_PER_SEC:
#include <stdio.h>
#include <time.h>
int main ( void )
{
int i;
clock_t start, end;
start = clock();
for ( i = 0; i < 100000000; i++ )
;
end = clock();
printf ( "The loop took %f seconds\n",
(double)( end - start ) / (double)CLOCKS_PER_SEC );
return 0;
}
The time function returns the current date and time as a time_t value. The argument to time can be either NULL, or a pointer to a time_t variable which time will write to. time returns (time_t)-1 on failure.The difftime() function calculates the interval between two time_t values, returning the difference in seconds.
#include <stdio.h>
#include <time.h>
int main ( void )
{
int i;
time_t start, end;
if ( ( start = time ( NULL ) ) != (time_t)-1 ) {
for ( i = 0; i < 400000000; i++ )
;
end = time ( NULL );
printf ( "The loop took %f seconds\n", difftime ( end, start ) );
}
return 0;
}
The tm struct breaks dates and times down into useful components, it's members are:
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
Two of the functions in the time header convert a time_t value to a tm struct, localtime and gmtime. localtime gives the result represented as the program's local time zone dictates while gmtime returns the result as Coordinated Universal Time (UTC).Times can be converted to formatted strings with the ctime and asctime functions, the former takes a time_t pointer value as its argument while the latter takes a tm struct pointer. ctime(timer) is equivalent to asctime(localtime(timer)).
#include <stdio.h>
#include <time.h>
int main ( void )
{
time_t now;
struct tm *tm_now;
now = time ( NULL );
tm_now = localtime ( &now );
printf ( "%s", ctime ( &now ) );
printf ( "%s", asctime ( tm_now ) );
return 0;
}
The strftime function performs complex formatting not available to asctime, it is similar to printf in that it takes a format string to determine how the output is to look. The formats are as follows:
%a
%A
%b
%b
%c
%d
%H
%I
%j
%m
%M
%p
%S
%U
%w
%W
%x
%X
%y
%Y
%Z
%%
#include <stdio.h>
#include <time.h>
int main ( void )
{
time_t now;
struct tm *tm_now;
char buff[BUFSIZ];
now = time ( NULL );
tm_now = localtime ( &now );
strftime ( buff, sizeof buff, "%A, %x %X %p", tm_now );
printf ( "%s\n", buff );
return 0;
}
The mktime function is the opposite of localtime. It takes a tm struct and converts it to a time_t value. If the tm struct can't be represented as a time_t value, (time_t)-1 is returned.To end this little "How-To", I'll give you a common function that determines if the passed year is a leap year. It doesn't use any of the above functions or types, but it does come in handy when combined with them:
int leap_year ( int year )
{
return year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 );
}
Credit: Prelude