C - SPLessons
boy2 Chapter 22

C Time Function

C - SPLessons

C Time Function

shape Description

By using C Time Function, there is a possibility of setting the date and time. C inherits the properties of functions and structures from C- library and exhibits the settings of date and time.

The header file required to set date and time in C is <time.h>.

Mainly, there are four C Time Function types. They are:

The first three terms acts as functions and helps in showing the time and date in the form of integer. Some of the functions included in C standard libraries are given in the below table.

Functions Description
time_t time(time_t *time); Returns local time by converting time_t using pointer
char *ctime(const time_t *time); Returns local time by converting time_t using pointer
struct tm *localtime(const time_t *time); Returns to tm structure using pointer to represent local time
clock_t clock(void) Returns duration of current running program
char * asctime(const struct tm * time); Returns pointer to structure converting the data to day month date hours:minutes:seconds:year\n\0 format
struct tm * gmtime(const time_t *time); Returns pointer to structure representing the GMT time
time_t mktime(struct tm *time); Returns the exact time in the structure when pointer points it.
double difftime(time_t time2, time_t time1); Calculates the differences between time1 and time2
size_t strftime(); Returns formatted date and time

The last term ‘tm‘ will be in structure format which consists of following elements.

Field Type Meaning Range
tm_sec int represents seconds of minutes 0-61
tm_min int represents minutes of hour 0-59
tm_hour int represents hours of day 0-23
tm_mday int represents day of month 1-31
tm_mon int represents month of year from January 0-11
tm_year int represents years since 1900
tm_wday int represents days since Sunday 0-6
tm_yday int represents days since January 1st 0-365
tm_isdst int represents hours saving day time

shape More Info

The macros used in C Time Function are:

  1. CLOCKS_PER_SEC: The macro indicated the number of clocks taken per second by the processor.
  2. NULL : The null macro indicates the null pointer value which is constant.

shape Examples

Program using clock() function

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

int main()
{
   clock_t begin_t, stop_t, sum_t;
   int a;

   begin_t = clock();
   printf("Beginning of program, begin_t = %ld\n", begin_t);
    
   printf("scanning loop, begin_t = %ld\n", begin_t);
   for(a=0; a< 10000000; a++)
   {
   	
   }
   stop_t = clock();
   printf("Ending loop, stop_t = %ld\n", stop_t);
   
   sum_t = (double)(stop_t - begin_t) / CLOCKS_PER_SEC;
   printf("Total time taken by CPU: %f\n", sum_t  );
   printf("End of the program...\n");

   return(0);
}

Output

Beginning of program, begin_t = 0
scanning loop, begin_t = 0
Ending loop, stop_t = 46
Total time taken by CPU: 0.000000
End of the program...

Program to find local time using asctime function

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

int main ()
{
   time_t t;
   struct tm *m;
   char buffer[50];

   time( &t );

   m = localtime( &t );
   printf("Present local time and date is: %s", asctime(m));

   return(0);
}

Output

Present local time and date is: Fri Dec 11 10:34:11 2015

Program to find the execution time taken using the difference function

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

int main ()
{
   time_t begin_t,stop_t;
   double difference_t;

   printf("Start of program\n");
   time(&begin_t);
   
   printf("Wait for 3 seconds\n");
   sleep(3);

   time(&stop_t);
   difference_t = difftime(stop_t, begin_t);

   printf("Time taken to execute = %f\n", difference_t);
   printf("Ending the program\n");

   return(0);
}

Output

Start of program
Wait for 3 seconds
Time taken to execute = 3.000000
Ending the program

Summary

shape Key Points

  • C Time Function use <time.h> header file.
  • time_t, clock_t and size_t used to set time.