ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software

Time Difference in Cpp

Updated on May 1, 2009

This program demonstrates how to calculate the difference between two times. The first time is assumed to preceed or equal the second time. Both times are assumed to be in the same 24 hour period.

Each time is in HH:MM:SS (hours, minutes seconds) format.

The strategy is to convert each time to seconds, calculate the difference between the two sceonds values, then convert back to HH:MM:SS format for display.

The logic does detect when the second time crosses midnight and adjusts the calculations accordingly.

/***********************************************************************
*                                        DifferenceBetweenTwoTimes.cpp *
* Description:    Calculate difference between two times               *
*                                                                      *
* Abstract:       Calculate the difference between 2 times             *
*                 Assume the times are in the same 24 hour period.     *
*                 The first time is 'earlier' than the second.         *
*                                                                      *
* Author:                                                *
* Date:           10/11/08                                             *
* Change History:                                                      *
*                                                                      *
************************************************************************/
#include <iostream>
using namespace std;

/* define a new data type */
struct timeType {
    int hours;
    int mins;
    int secs;
};

void main(void)
{
/*     declare some variables of type "timeType" */
    struct timeType start, stop, diff;

/*    Read user input. Note the ignore() methods, which skip over the colons in the input stream */
    cout << "\n Enter the start time (hh:mm:ss): ";
    cin >> start.hours; cin.ignore(1,':'); cin >> start.mins; cin.ignore(1,':'); cin >> start.secs;

    cout << "\n Enter the stop time (hh:mm:ss): ";
    cin >> stop.hours; cin.ignore(1,':'); cin >> stop.mins; cin.ignore(1,':'); cin >> stop.secs;

    int intStartSeconds, intStopSeconds, intDiffSeconds = 0;

/*    ---------------------------------------*
*    Convert both times to seconds.         *
*    ---------------------------------------*/
    intStartSeconds = start.hours * 3600 + start.mins * 60 + start.secs ;
    intStopSeconds =  stop.hours  * 3600  + stop.mins * 60 + stop.secs;
    cout << "\n start = " << intStartSeconds << " seconds, stop = " << intStopSeconds << " seconds";

/*    -----------------------------------------------*
*    Calculate the difference between the two times *
*    -----------------------------------------------*/
    intDiffSeconds = intStopSeconds - intStartSeconds;

/*    ---------------------------------------------------------*
*    If the time crosses midnight, recalculate the difference *
*    ---------------------------------------------------------*/
    if (intStartSeconds >= intStopSeconds)  {  /* Time crosses midnight ? */
        cout << "\n Time crosses midnight";
        intDiffSeconds = ((24 * 60 * 60) - intStartSeconds) + intStopSeconds;
    }

    cout << "\n difference (in seconds) = " << intDiffSeconds;
/*    -----------------------------------------------------*
*    Convert the seconds back to hours:mins:secs          *
*    -----------------------------------------------------*/
    diff.hours = intDiffSeconds / 3600;   /* integer math ! */
    intDiffSeconds = intDiffSeconds - (diff.hours * 3600);

    diff.mins = intDiffSeconds / 60;      /* integer math ! */
    intDiffSeconds = intDiffSeconds - (diff.mins * 60);

    diff.secs = intDiffSeconds;

    cout << "\n difference = " << diff.hours << ":" << diff.mins << ":" << diff.secs;

    cout << "\n\n";
}


Comments

    0 of 8192 characters used
    Post Comment

    • nicomp profile image
      Author

      nicomp really 7 years ago from Ohio, USA

      @guest: please post your code and the errors you observed.

    • profile image

      guest  7 years ago

      instructions were clear but the program did not run :(