Calculating time difference in C (days, hours, minutes, seconds)
Given that this is a poor way to calculate time difference, there are two problems:
- scanf cannot separate integers
- hour difference is wrong
Try this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int day1, hour1, min1, sec1, day2, hour2, min2, sec2, totalTime;
printf("Enter input:");
scanf("%d,%d,%d,%d,%d,%d,%d,%d", &day1, &hour1, &min1, &sec1, &day2, &hour2, &min2, &sec2);
totalTime = (day2 - day1) * 86400 + (hour2 - hour1) * 3600 + (min2 - min1) * 60 + (sec2 - sec1);
printf("D: %d\n", totalTime / 86400);
totalTime = totalTime % 86400;
printf("H: %d\n", totalTime / 3600);
totalTime = totalTime % 3600;
printf("M: %d\n", totalTime / 60);
totalTime = totalTime % 60;
printf("S: %d\n", totalTime);
return 0;
}
A better way to calculate time difference could be by using difftime function
Calculating difference between two date-times in C++
You use the conversion specifier%b
to get the month but it should be %m
:
ssBuffer >> get_time(&tm, "%Y-%m-%d:%H:%M:%S");
%b
- parses the month name, either full or abbreviated, e.g. Oct (non-numeric)%m
- parses the month as a decimal number (range [01,12]), leading zeroes permitted but not required
The year and month are correct. 121 is the number of years since 1900 and 9 is the month, zero-based [0,11], which is what's specified for std::tm
.
Time difference between system time and user inpuit time in C Programming
current_time
is of type time_t
which is basically an integer and does not have member variable like struct
s.
So you can't do like current_time.hour
.
time_t
stores the total number seconds from the UNIX epoch.
The user given time in your program does not include details like year, month, etc and has just hour, minute and second information. The time_t
that you get via time()
has the total number number of seconds from the UNIX epoch.
So, you may want to use just the hour, minute, second information like
time_t t=time(NULL);
struct tm *c=localtime(&t);
int seconds2 = c->tm_hour*60*60 + c->tm_min*60 + c->tm_sec;
struct tm
is a structure to store components of the calendar time and localtime()
localtime converts the calendar time into local time and returns a pointer to a struct tm
.
tm_hour
, tm_min
and tm_sec
are members of struct tm
denoting th hour, minute and second respectively.
Note that the hour in tm_hour
is in 24 hours format. If you want a 12 hour version, use tm_hour%12
.
How to calculate a time difference in C++
See std::clock()
function.
const clock_t begin_time = clock();
// do something
std::cout << float( clock () - begin_time ) / CLOCKS_PER_SEC;
If you want calculate execution time for self ( not for user ), it is better to do this in clock ticks ( not seconds ).
EDIT:
responsible header files - <ctime>
or <time.h>
Get Time Difference without if else statement
Use (some sort of) timestamps, by turning your hours and minutes variables into one, e.g:
stime = shours * 60 + sminutes;
etime = ehours * 60 + eminutes;
then calculate de difference of that
totime = etime - stime;
then convert that back into hours and minutes
tominutes = totime % 60;
tohours = (totime - tominutes) / 60;
(integer division will take care of rounding down)
Not the most elaborated solution, but I guess you're looking for an beginners-friendly solution
Edit
speaking of beginner-friendly: the % is the modulus operator that returns the remainder of a division. So when you divide 119 by 60 it returns 59. And yes, you could also just get the hours from dividing totime by 60 and let the integer division do the job, but it's nicer (read: clearer to read what's going on) when you divide (totime - tominutes) because it's like the missing part to the line with the modulus
Related Topics
How to Tell Where a Header File Is Included From
Return a "Null" Object If Search Result Not Found
Lvalue to Rvalue Implicit Conversion
C++ Data Alignment /Member Order & Inheritance
All Combinations of K Elements Out of N
C++ Threads, Std::System_Error - Operation Not Permitted
Can Someone Please Explain the "Indices Trick"
Modular Exponentiation for High Numbers in C++
How to Build Cmake Externalproject While Configurating Main One
Retrieving File Descriptor from a Std::Fstream
Pimpl Idiom VS Pure Virtual Class Interface
Is There Any Reason to Use the 'Auto' Keyword in C++03
How to Connect MySQL Database Using C++
What Are the Differences Between Overriding Virtual Functions and Hiding Non-Virtual Functions
C/C++ Changing the Value of a Const