C++ Program to Calculate the Difference in Time

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

This article will discuss various methods to calculate the difference between two time periods. But before knowing about these methods of calculating the difference between two time periods, we must know what time period is. A time period is uniquely defined as triplets of hours, minutes, and seconds. To calculate the difference in time, we require two time periods.

Introduction

In this article, we will calculate the difference between two given time periods in C++. To calculate the difference between time periods, we require two time periods which are provided by the user. The time periods should be in the form of hours, minutes, and seconds. After taking the time periods, the difference between the two given time periods is calculated.

For example-:

Working

We used two methods to calculate the difference between the two time periods.

  • The first method uses the difftime() function, defined in the <ctime> header file. In this method, the difftime() function takes two parameters which are start_time and end_time. In order to set start_time and end_time, the time() function is used, which provides the current calendar time of type time_t.
  • The second method uses the structure in which we will take the hour, minute, and second values of both time periods and keep them in two variables defined by a structure. Then, we will find the difference between the two times and then print the values.

Program to Calculate the Difference in Time using difftime() Function

The difftime() function is defined in the <ctime> header file. The difftime() function is used to compute the difference between two times in seconds.

The syntax of the difftime() is:

Here in the above syntax, The difftime() takes two parameters. The first parameter is end, which represents the ending time, and the second parameter is start, which represents the beginning time.

Example:

Output:

Explanation: In the above C++ program, we used the difftime() function to calculate the difference in time. Here, the difference in time is the time taken by the user to answer the question. The difftime() takes two parameters, start, and end, and returns the difference between start and end times in seconds. Here, the time() function is used to get the current calendar time of type time_t.

Program to Calculate the Difference in Time Using Structure

A structure is a way to group various related variables into one place. Each variable in the structure is referred to as a member variable. The structure is almost similar to a class in that both contain a collection of data of different data types. But the main difference between class and structure is that structure members are public by default, and class members are private by default.

The syntax of the structure is:

Here in the above syntax, the structure is defined with the struct keyword. The data_type variable is the C++ variable of different data types like int, char, float, etc., and the StructureName is the name we want to give to our structure. Here is a simple example for a better understanding of the structures.

Output:

Example:

Output:

Explanation:

In the above C++ program, the user is asked to enter two time periods stored in structure variables time1 and time2, respectively. We have defined three Time variables, time1, time2, and time_diff, to hold the first time, the second time, and the difference between the first and second time. Then, the CalulateTimeDifference() function is called to calculate the difference between time1 and time2. In CalulateTimeDifference() function, we are checking if the secs value of time2 is greater than the secs value time1. If yes, we are incrementing the secs value of time1 by 60 and decrementing the mins value of time1 by 1. Similarly, we are checking for the mins values and incrementing the mins value of time1 by 60 and decrementing the hours value time1 by 1. In the end, return the time_diff variable, which contains the difference between time1 and time2.

Conclusion

  • A time period is uniquely defined as triplets of hours, minutes, and seconds.
  • The difftime() function can be used to calculate the difference between two times in seconds.
  • The difftime() function is defined in the <ctime> header file.
  • The difftime() function takes two parameters. The first parameter is end, which represents the ending time, and the second parameter is start, which represents the beginning time.
  • We can use structure to calculate the difference between two time periods. In which we use three member variables to store hours, minutes, and seconds.
  • A structure is a collection of variables of various data types that go under a single name.