# 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[0]=24 t1[1]=12 t1[2]=13
t2[0]=24 t2[1]=01 t2[2]=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] = 0 spent_time[1] = -11 and spent_time[2] = 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[2]*3600 + t1[1] * 60 + t1[0];
int stop = t2[2]*3600 + t2[1] * 60 + t2[0];
int delta = stop - start;
t3[0] = delta % 60;
delta /= 60; // Same as delta = delta / 60
t3[1] = delta % 60;
delta /= 60;
t3[2] = 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 !!
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[0]*3600+time[1]*60+time[2]
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[2]*3600 + t1[1]*60 +t1[0];
long Stop = t2[2]*3600 + t1[1]*60 +t2[0];
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[2] = hours;
t3[1] = minutes;
t3[0] = 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[2] = 8;
m_starts[1] = 0;
m_starts[0] = 0;

m_finish[2] = 9;
m_finish[1] = 15;
m_finish[0] = 0;

long start = m_starts[2]*3600 + m_starts[1] * 60 + m_starts[0];
long finish = m_finish[2]*3600 + m_finish[1] * 60 + m_finish[0];

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[0] = seconds;
last_laiks[1] = minutes;
last_laiks[2] = hours;

sprintf(msg,"%02d:%02d:%02d", last_laiks[2], last_laiks[1], last_laiks[0]);
lcd.print(msg);
``````

message printed: -16:-1149:-32

Strangines is, vhen i change m_finish[1] = 1; (09:01:00), i will get correct answer!!!