time


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


Description
Gets time of day.
time gives the current time, in seconds, elapsed since 00:00:00 GMT, January 1, 1970, and stores that value in the location pointed to by timer, provided that timer is not a NULL pointer.

 

#include <time.h>
#include <stdio.h>
#include <dos.h>

int main(void)
{
time_t t;

t = time(NULL);
printf("The number of
seconds since January 1, 1970 is %ld",t);
return 0;
}


 

ctime


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


Description
Converts date and time to a string.
ctime converts a time value pointed to by time (the value returned by the function time) into a 26-character string in the following form, terminating with a newline character and a null character:

Mon Nov 21 11:31:54 1983\n\0

All the fields have constant width.
 

#include <stdio.h>
#include <time.h>

int main(void)
{
time_t t;

time(&t);
printf("Today's date and time: %s\n", ctime(&t));
return 0;
}

 

localtime

 

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


Description
Converts date and time to a structure.
localtime accepts the address of a value returned by time and returns a pointer to the structure of type tm containing the time elements. It corrects for the time zone and possible daylight saving time.


This is the tm structure declaration from the time.h header file:

struct tm {
    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;
};

 localtime example

#include <time.h>
#include <stdio.h>
#include <dos.h>

int main(void)
{
time_t timer;
struct tm *tblock;

/* gets time of day */
timer = time(NULL);

/* converts date/time to a structure */
tblock = localtime(&timer);

printf("Local time is: %s", asctime(tblock));

return 0;
}

 

difftime


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


Description
Computes the difference between two times.
difftime calculates the elapsed time in seconds, from time1 to time2.

 

#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>

int main(void)
{
time_t first, second;

clrscr();
first = time(NULL); /* Gets system
time */
delay(2000); /* Waits 2 secs */
second = time(NULL); /* Gets system time
again */

printf("The difference is: %f seconds\n",difftime(second,first));
getch();

return 0;
}