Thursday, 27 March 2014

time related functions in c++

No comments:

C provides the standard library to calculate the system time. In c you need to include the header file time.h and in c++ you need to include <ctype>


time_t is the data type name used for storing the epoch time.  The typedef of time_t is varies depend upon the platform. It is usually a long int or float type. 

Note on epoch time:

The epoch time is nothing but the number of seconds since 1 JAN 1970. The 1 Jan 1970 is UTC time. This time is also called unix time or posix time.

struct tm:

struct tm {               int tm_sec;         /* seconds */               int tm_min;         /* minutes */               int tm_hour;        /* hours */               int tm_mday;        /* day of the month */               int tm_mon;         /* month start with 0  */               int tm_year;        /* year since 1900*/               int tm_wday;        /* day of the week */               int tm_yday;        /* day in the year */               int tm_isdst;       /* daylight saving time */           };


double difftime(time_t time1, time_t time2);
This returns the difference between the two times in double format.

time function:
time_t time(time_t *epoch_time)
This function actually returns the number of seconds till JAN 1 1970. Note that the return value of the time function is also an time_t. If argument is not a null pointer then the value stored in the timer and return value will be same. If time function fails to fetch the calendar time then it returns null.


The argument can be passed as NULL. Even If passed as NULL, the return value will be time_t.

localtime function:

This localtime converts the epoch time to readable format. The local time returns the struct tm.

time_struct = localtime(&current_time);

mktime function:

time_t mktime (struct tm * time_struct);

This does the reverse process of the localtime. It returns the epoch time based on time struct. But make sure that the year should be subtracted from 1900 and month by 1 before converting  to epoch time when tm struct was filled manually.

printf("\n mk_time to get epoch time again  :: %d ",    mktime(time_struct));

asctime function:

It converts the time struct to human readable format.

char* asctime (const struct tm * time_struct);

The snippet is
printf("\n asctime :: %s ", asctime(time_struct) );


This function is used to format the date according to our needs and stores in a buffer 

 size_t strftime (char* formatted_string, size_t maxsize, const char* format,const struct tm* time_struct );

Choose the list of format specifers from here

some basic format specifiers are
Format Meaning
%b Month name in abbreviation (Aug)
%d day of month (01-31) Zero padded for single digit
%e Day of month( 1-31) Space padded for single digit
%m month as decimal number(01-12)
%M minutes (00-59)
%y Year last two digits (92)
%Y Year last four digits (1992)
%H Hour in 24 Hour format (00-23)
%S Seconds(0-61)

Simple formatted time is
printf("\n  formatted_time :: %s ",  formatted_time   );

This function is used to compute the Greenwich median time
struct tm * gmtime (const time_t * timer);
This is similar to the asctime
char* ctime (const time_t * timer);

Clock function:

This is used to calculate the cpu time
clock_t clock ();
The return type of the clock function is the clock_t. which is the basic arithmetic type typedefed to clock_t.
The return value is the number of clock ticks that elapsed till the epoch time.

So to calculate the clock time of program execution take the difference of two clock time executed at start and end of the program

It also has the inbuilt macro CLOCKS_PER_SEC to calculate the cpu time in terms of seconds.

Sample  Program:
#include<iostream>#include<cstdio>#include<ctime> // In c it is <time.h>
int main(){   using namespace std;
   time_t last_connect_time,current_time;   struct tm *time_struct;   char formatted_time[25];
   time(&last_connect_time);   sleep(1);   current_time=time(0); // This is similar to time(&current_time)   printf("\n DIFFERENCE TIME IN SECONDS  :: %f ", difftime(current_time,last_connect_time) );
   printf("\n Epoch time or unix time     :: %ld ",         current_time );
   time_struct = localtime(&current_time);   printf("\n CURRENT YEAR :: %d ",  (time_struct->tm_year+1900)     );   printf("\n CURRENT MON  :: %d ",  (time_struct->tm_mon+1)     );   printf("\n CURRENT DAY  :: %d ",  time_struct->tm_mday     );   printf("\n CURRENT HOUR :: %d ",  time_struct->tm_hour     );   printf("\n CURRENT MIN  :: %d ",  time_struct->tm_min     );   printf("\n CURRENT SEC  :: %d ",  time_struct->tm_sec     );
   printf("\n mk_time to get epoch time again  :: %ld ",    mktime(time_struct) );
   printf("\n asctime       :: %s ",     asctime(time_struct)     );   strftime(formatted_time,sizeof(formatted_time),"%d-%b-%y",time_struct);   printf("\n formatted_time:: %s ", formatted_time );

   return 0;}

No comments:

Post a Comment