# How to calculate time difference

Hello.
Just start to learn Arduino platform and get stuck on simple (or not so simple) question.
I have two arrays. Arrays are time got from RTC DS1307. Event start time is in first array, event end time in second.
How do I manage to calculate time spent between events?

Alex

substract ?

z = y - x
10 - 2 = 8

?

It depends on what format you are storing the timestamps in.

subtract the arrays first write the code to store values in one array from DS iC and then at a later point in the program again store the time in the array and then subtract both.

Ok, maybe my question was unclear. Let’s say start time is 13:12:24, end time 14:01:24 arrays ia as follows:
t1=24 t1=12 t1=13
t2=24 t2=01 t2=14
if we substrast start time from stop time:

``````for (int i = 0, i < 3, i++) {
spent_time[i] = t2[i] - t1[i];
}
``````

we got: spent_time = 0 spent_time = -11 and spent_time = 01

but i need to get 00:49:00.

Alex

Convert both to seconds, do the subtraction, then convert back.

well I do something similar, but I use a minutecounter or secondcounter for storing times in that array

minutecounter = hour60+minute (1440 minutes per day)
secondcounter = minutecounter
60+second (86400 seconds per day)

( if you need full date you can use Unixtime )

Yes. If I was doing it, I’d just store a seconds value or a Unix/Epoch time.

was thinking about converting to unix epoch, but unsuccess to find formula for arduino platform Which library are you using for your RTC?

This would do?

int start = t1*3600 + t1 * 60 + t1;
int stop = t2*3600 + t2 * 60 + t2;
int delta = stop - start;
t3 = delta % 60;
delta /= 60; // Same as delta = delta / 60
t3 = delta % 60;
delta /= 60;
t3 = delta;

How to modify this to give a correct value if stop is after midnight is left to do karl_b, thanks a lot. but stop time can be after midnight. will try to figure out how to deal with it.

After some thought, you probably want to exchange those int:s to long:s, otherwise your code will only work for the first 8 hours in a day.

About the midnight problem, if your delta is always less than 24h you could try this:
if ( start > stop )
stop += 24*3600;

If you need longer delta than that you need to extend the function with calculation for days, months and/or years.

no need for longer time periods.
thanks a lot, problem solved.

Got strange behavior from calculating time using karl_b formula.
If time difference more than 1h and 10min, getting result with minus sign!

Let's say start time 08:00:00, stop time 09:15:00.
the result is -11:-9:-32 !!
I'm totally confused about this, since not so strong in math karl_b, pls help!

is Karl's array the other way around from yours ?

Check those first

the code is HOURS3600+MINUTES60+SECONDS
or Time*3600+time*60+time
when the array is { HOURS, MINUTS, SECONDS }

check the Numbers !

``````int t1[]={0,0,0,0,0,0,0,};//sec - min - hour - and so on
int t2[]={0,0,0,0,0,0,0,};
int t3[]={0,0,0,0,0,0,0,};

in loop :

long Start = t1*3600 + t1*60 +t1;
long Stop = t2*3600 + t1*60 +t2;
long Delta = Stop - Start;    // Delta is now number of seconds between

int hours = Delta / 3600;    // HOURS
Delta = Delta - hours*3600 // removing full hours from teh difference
int minutes = Delta / 60;   // MINUTES
Delta = Delta - minutes *60; // removing full minutes
int seconds = Delta;         // SECONDS

t3 = hours;
t3 = minutes;
t3 = seconds;
``````

this could be smaller I guess, but I think its easier to understand ?

my code now (assumes start at 8:00:00, finish at 9:15:00, variables declared of course):

``````m_starts = 8;
m_starts = 0;
m_starts = 0;

m_finish = 9;
m_finish = 15;
m_finish = 0;

long start = m_starts*3600 + m_starts * 60 + m_starts;
long finish = m_finish*3600 + m_finish * 60 + m_finish;

long Delta = finish - start;
int hours = Delta / 3600;
Delta = Delta - hours * 3600;
int minutes = Delta / 60;
Delta = Delta - minutes * 60;
int seconds = Delta;

last_laiks = seconds;
last_laiks = minutes;
last_laiks = hours;

sprintf(msg,"%02d:%02d:%02d", last_laiks, last_laiks, last_laiks);
lcd.print(msg);
``````

message printed: -16:-1149:-32 Strangines is, vhen i change m_finish = 1; (09:01:00), i will get correct answer!!!