Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculating user time and time elapsed

Status
Not open for further replies.

2774

Programmer
Feb 11, 2003
2
0
0
DE
Hi im a little bit stuck on this program im working on.

im creating a program which uses the data type Mytime the output should look something like

Enter start hh mm : 7 30

Enter finish hh mm : 7 30

Time elapsed from 07:30 until 07:30 is 00:00

2 functions have not yet been defined display1Time() & elapsed(). The program currently compiles in unix g++ i have filled the un defined function to get the program running. Basically i cant work out how to get the time displayed and the total time elapsed working. I think the time the user enters has to be converted to minutes. Can anyone help please ?

Code:
// Incomplete version
#include <iostream>
using namespace std;

struct Mytime
{
   int hours;
   int mins;
};

int timecmp(Mytime,Mytime);
// returns 0 if times are same
// returns >0 if first is later than second
// returns <0 if first is before second

int setTime(Mytime& t, int h, int m );
// if valid time sets hh and mm to h and m 
// and returns 0
// if invalid returns integer > 0 as error code
// error code +1 = underflow hours
// error code +2 = overflow hours
// error code +4 = underflow mins
// error code +8 = overflow mins

void display1Time(Mytime t);
// displays in form hh:mm

void elapsed(Mytime t1, Mytime t2, Mytime& duration);
// determines the time duration between t1 and t2
// t1 assumed to be earlier than t2

void main()
{
    Mytime now;
    Mytime then;
    Mytime howlong;
    
    int h,m;
    
    do // validate input
    {
        cout << &quot;Enter start hh mm : &quot;;
        cin >> h >> m ;
    } while (setTime(now,h,m));
    
    do
    {
        cout << &quot;Enter finish hh mm : &quot;;
        cin >> h >> m ;
    } while (setTime(then,h,m));
    
    elapsed(now,then,howlong);

    cout << &quot;Time elapsed from &quot;;
    display1Time(now);
    cout << &quot; until &quot;;
    display1Time(then);
    
    cout << &quot; is &quot;;
    display1Time(howlong);
    cout << endl;
}

int timecmp(Mytime t1,Mytime t2)
{
   if (t1.hours == t2.hours)
   {
      if (t1.mins == t2.mins)
      {
         return 0;
      }
      else // mins not same
      {
         if (t1.mins > t2.mins)
         {
            return 1; // greater than zero
         }
         else
         {
            return -1;
         }
      }
   }
   else // hours not same
   {
      if (t1.mins > t2.mins)
      {
         return 1;
      }
      else
      {
         return -1;
      }
   }
}

int setTime(Mytime& t, int h, int m )
{
    int errorCode = 0;
    if (h < 0) errorCode += 1;
    if (h > 23) errorCode += 2;
    if (m < 0) errorCode += 4;
    if (m > 59) errorCode += 8;
    if (errorCode==0){t.hours = h; t.mins = m;}
    return errorCode;
}

//////////// THE FOLLOWING FUNCTIONS ARE INCOMPLETE

void display1Time(Mytime t)
{
     cout << &quot;00:00&quot;;
}


void elapsed(Mytime t1, Mytime t2, Mytime& duration)
{
    setTime(duration,0,0);
}
 
Hi,

The standard C library (and hence std c++ lib) provide <time.h> which has &quot;struct tm&quot; and time_t types and a number of associated functions which should allow you all the manipulation you require.

I haven't tried the following but it should work ok:

// Time(0) returns the current number of seconds
// since 1-Jan-1970. localtime converts this into
// year, month, day, hours, minutes, seconds in a
// structure. You can use gmtime to instead if you
// don't want any daylght saving applied. Anyway it
// doesn't matter here as we are going to overwrite
// the time anyway - this just sets us up a valid
// struct tm.
struct tm time1 = *(localtime(time(0)); // copies the time
struct tm time2 = time1;

// Populate the two times with the user's values
time1.tm_hour = user's time 1 hours field
time1.tm_min = user's time 1 minutes field
time2.tm_hour = user's time 2 hours field
time2.tm_min = user's time 2 minutes field

// Get the difference in seconds
// you can then process this to get the elapsed hours
// and minutes
double diff = difftime(mktime(&time2), mktime(&time1));

mktime is interesting as it corrects any invalid &quot;struct tm&quot; to a valid date time. So if you have 400 days in a year, it will correct the year to the following year and set the day appropriately. I think that it also fixes broken times.

Thus the following is valid

time_t now = time(0);
struct tm TimeStruct = *(localtime(now));
TimeStruct.tm_mdat += 100;
cout << &quot;Date/Time in 100 days is &quot; << ctime(&(mktime(TimeStruct))) << endl;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top