Tuesday 20 November 2012

Various time functions in c in time.h


I wrote a program in C for sorting using different algorithms.
I performed a timing mesurement experiment for each of the algorithms with different input sizes,

In order to measure timeing taken by each algorithm, I came across various C functions.
In this blog I am writing a broef summary of the functions I cam eacross. I am also mentioning some small examples to use eacf of these functions.

In C there are two main header files which have functions related To time, namely time.h and windows.h.

1.Time.h


A) The important datatypes associated with time.h are
1) time_t:  which is actually a 32/64 bit integer    ( __int32   / __int64)
2) size-t  which corresponds to the integral data type returned by the language operator sizeof and is defined in the<ctime> header file (among others) as an unsigned integral type.

B) The structure associated with manipulation of time is  tm. The structure as defined in time.h is :


 
struct tm {
        int tm_sec;     /* seconds after the minute - [0,59] */
        int tm_min;     /* minutes after the hour - [0,59] */
        int tm_hour;    /* hours since midnight - [0,23] */
        int tm_mday;    /* day of the month - [1,31] */
        int tm_mon;     /* months since January - [0,11] */
        int tm_year;    /* years since 1900 */
        int tm_wday;    /* days since Sunday - [0,6] */
        int tm_yday;    /* days since January 1 - [0,365] */
        int tm_isdst;   /* daylight savings time flag */
        };
C) Some of the important functions in time.h are:

1)  time


  • Function headers:-time_t time ( time_t * timer );
  • Description:-The function returns this value, and if the argument is not a null pointer, the value is                  also set to the object pointed by timer.
  • Example:int main ()
    {
      time_t seconds;
    
      seconds = time (NULL);
      printf ("%ld hours since January 1, 1970", seconds/3600);
      
      return 0;
    }



2) difftime

  • Function headers:double difftime ( time_t time2, time_t time1 );
  • Description:It returns the diffrence between two times.  
  • Example:int main ()
    {
      time_t start,end;
      char szInput [256];
      double dif;
    
      time (&start);
      printf ("Please, enter your name: ");
      gets (szInput);
      time (&end);
      dif = difftime (end,start);
      printf ("Hi %s.\n", szInput);
      printf ("It took you %.2lf seconds to type your name.\n", dif );
     
      return 0;
    }


3) mktime

  • Function headers:time_t mktime ( struct tm * timeptr );
  • Description:It converts the Convert tm structure to time_t 
  • Example:
    int main ()
    {
      time_t rawtime;
      struct tm * timeinfo;
      int year, month ,day;
      char * weekday[] = { "Sunday", "Monday",
                           "Tuesday", "Wednesday",
                           "Thursday", "Friday", "Saturday"};
    
      /* prompt user for date */
      printf ("Enter year: "); scanf ("%d",&year);
      printf ("Enter month: "); scanf ("%d",&month);
      printf ("Enter day: "); scanf ("%d",&day);
    
      /* get current timeinfo and modify it to the user's choice */
      time ( &rawtime );
      timeinfo = localtime ( &rawtime );
      timeinfo->tm_year = year - 1900;
      timeinfo->tm_mon = month - 1;
      timeinfo->tm_mday = day;
    
      /* call mktime: timeinfo->tm_wday will be set */
      mktime ( timeinfo );
    
      printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);
      
      return 0;
    }

2.Windows.h

A)Functions
B) Structure
C) Datatypes

No comments:

Post a Comment