Thursday, 27 March 2014

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;}

Thursday, 13 March 2014

Fill is a template function used to initialize array or vectors using the some value. You should specify the starting pointer , number of elements to be reseted and value to be reseted.

Fill is the standard c++ function comes under the namespace std.
You can use fill to fill the array from intermediate.

void fill(type *start,type *end,const int reset_value);


int main()
   using namespace std;

   char arr[]="123456";
   printf("\n     arr   :: %s ",    arr      );
   printf("\n     arr   :: %s ",    arr      );

   return 0;


  • you can reset the value in sequence only
  • This is similar to for loop internally
  • use of  memset is efficient when compare to fill but memset works on bytes level.

  • used for filling some range of value in the array
  • used for initializing array and vector

Monday, 3 March 2014

Before saying “what is local host?”, You should know about what is host first of all.
Host or node is nothing but a point in a network. Yes there is relation between the host and local host.

Here the Local host is nothing but that refers to the same node or host.

In other words,
local host is the host name(default host name) given to the ipaddress 127.*.*.*. series. 
All the packets which is sent to 127 series will be looped back to your host.

127 is the last series in the class a network.

Consider you were sending the packet to nothing but the localhost, now the packet you send will be received by yourself. 


LOOPBACK is the interface that lies behind the local host mechanism.

what is loop back interface?
one of the main objective of networking is process of   sending and receiving the packet between the hosts.

In LOOPBACK the packets you were sending will not be delivered to your neighbor hosts. It will just deliver to your own machine. yes it like a throwing a ball on a wall infront of you. It will just come back to you. 


If your server and client are at same station(computer) you can use the localhost.
Localhost never enters the network communication. It will be looped back by your system itself.


Yes you can very well use it. 

what you will do for determining the network connection between the two hosts. Probably you will type just ping followed by ipaddress. Just check it for local host like “ping”. Packets will be received on running this command and shows that network connection is available for loopback.