calculate time difference

Thread Starter

ak52

Joined Oct 15, 2014
230
Hello Everyone ,

Do any of you have example code or an algorithm to calculate difference between two times?

For example if the time is 23:59 yesterday and the present time is 00:01 today,then the time difference is 2 minutes.
I have access to the date and time via RTC if that helps.

Thanks in advance,
Arun
 

mitko89

Joined Sep 20, 2012
127
What exactly is the problem if you have an RTC? Just read the values of the registers and compare them. In the reference manual of the uC, there should be information about the registers of the RTC. When an event occures (your earier time), read the value of the register and store it in a static variable. When you want to compare times, just store the value of the rtc registers in another variable (or use a structure to make things look more readable) and just substract. Start by reading the data sheet and/or some application notes on how to use the RTC, I don't think there is a specific algorithm for calculating time differences, just use static variables, substract the values and then convert the ticks to whatever dimension you need.
 

ErnieM

Joined Apr 24, 2011
8,377
All you need do is convert both the times to the accuracy you wish (days, hours, minutes, or seconds) as a single number, subtract them, then expand the result if need be.

Example: 12:45:15 - 11:22:30
12:45:15 is 12 hours 45 minutes 15 seconds
11:22:30 is 11 hours 22 minutes 30 seconds

Hours to minutes = 12 * 60 = 720 minutes
Minutes to seconds = (720 + 45) * 60 = 45,900
Net seconds = 45,900 + 15 = 45,915

Hours to minutes = 11 * 60 = 660 minutes
Minutes to seconds = (660 + 22) * 60 = 40,920
Net seconds = 40,920 + 30 = 40,950

Difference in time:
45,915 - 40,950 = 4,965 seconds

To convert back to seconds minutes hours you find the largest integer portions

Seconds to minutes:
4,965 / 60 = 82.75

So there are 82 integer minutes
4,965 – ( 60 * 82 ) = 45 seconds

Minutes to hours:
82 / 60 = 1.366 hours
So there is 1 integer hour
82 – (1 * 60) = 22 minutes

Net difference is 1 hour 22 minutes 45 seconds.
 

Thread Starter

ak52

Joined Oct 15, 2014
230
All you need do is convert both the times to the accuracy you wish (days, hours, minutes, or seconds) as a single number, subtract them, then expand the result if need be.

Example: 12:45:15 - 11:22:30
12:45:15 is 12 hours 45 minutes 15 seconds
11:22:30 is 11 hours 22 minutes 30 seconds

Hours to minutes = 12 * 60 = 720 minutes
Minutes to seconds = (720 + 45) * 60 = 45,900
Net seconds = 45,900 + 15 = 45,915

Hours to minutes = 11 * 60 = 660 minutes
Minutes to seconds = (660 + 22) * 60 = 40,920
Net seconds = 40,920 + 30 = 40,950

Difference in time:
45,915 - 40,950 = 4,965 seconds

To convert back to seconds minutes hours you find the largest integer portions

Seconds to minutes:
4,965 / 60 = 82.75

So there are 82 integer minutes
4,965 – ( 60 * 82 ) = 45 seconds

Minutes to hours:
82 / 60 = 1.366 hours
So there is 1 integer hour
82 – (1 * 60) = 22 minutes

Net difference is 1 hour 22 minutes 45 seconds.
Thanks for the detailed explanation ,the above explanation is true if i am calculating the time difference within the same day.
Consider these times:
23:59:00 - 00:00:30 ,the answer should come as 90 seconds.But if you use the same explanation as stated ,you get completely different results. :(
 

mitko89

Joined Sep 20, 2012
127
Thanks for the detailed explanation ,the above explanation is true if i am calculating the time difference within the same day.
Consider these times:
23:59:00 - 00:00:30 ,the answer should come as 90 seconds.But if you use the same explanation as stated ,you get completely different results. :(
If you are using an RTC, the RTC module should have registers that represent the year, month and date. What is the module of the microcontroller you are using?
 

Thread Starter

ak52

Joined Oct 15, 2014
230
If you are using an RTC, the RTC module should have registers that represent the year, month and date. What is the module of the microcontroller you are using?
The problem is not with getting the time from rtc,that's a piece of cake.
How to you get the time difference between any two times.This is purely algorithmic and has nothing to do with RTC.
 

ErnieM

Joined Apr 24, 2011
8,377
Thanks for the detailed explanation ,the above explanation is true if i am calculating the time difference within the same day.
Consider these times:
23:59:00 - 00:00:30 ,the answer should come as 90 seconds.But if you use the same explanation as stated ,you get completely different results. :(
Seriously? You don't see the need to add 24 hours to one of these numbers?

Stop looking for an algorithm to blindly follow and start using the brain God gave you.
 

Thread Starter

ak52

Joined Oct 15, 2014
230
Thanks Ernie,the solution was right there in front of me,i got it working now. ;)

Edit:
The code if anyone need it.

Code:
#include <stdio.h>
#include <stdlib.h>
int get_time_diff(int*,int*);
int main()
{
  int old_time[] = {23,59,00,15,7,2015};  //hour,minute,sec,day,month,year
  int new_time[] = {00,00,30,16,7,2015};

  return get_time_diff(old_time,new_time);
}
int get_time_diff(int*old_t,int *new_t)
{
  int day_difference = 0;
  long new_t_sec = 0;
  long old_t_sec = 0;
  if(new_t[3] - old_t[3] == 0)
  {
  //convert hours to seconds
  old_t[0] = (old_t[0]*60)*60;
  new_t[0] = (new_t[0]*60)*60;

  //convert minutes to seconds
  old_t[1] = old_t[1]*60;
  new_t[1] = new_t[1]*60;

  //add all the seconds
  old_t_sec = old_t[0] + old_t[1] + old_t[2];
  new_t_sec = new_t[0] + new_t[1] + new_t[2];

  return (new_t_sec - old_t_sec);
  }
  else
  {
  day_difference = (new_t[3] - old_t[3])*24;
  //convert hours to seconds
  old_t[0] = (old_t[0]*60)*60;
  new_t[0] = ((new_t[0] + day_difference)*60)*60;

  //convert minutes to seconds
  old_t[1] = old_t[1]*60;
  new_t[1] = new_t[1]*60;

  //add all the seconds
  old_t_sec = old_t[0] + old_t[1] + old_t[2];
  new_t_sec = new_t[0] + new_t[1] + new_t[2];

  return (new_t_sec - old_t_sec);
  }
}
 
Last edited:

MrChips

Joined Oct 2, 2009
30,810
Wouldn't work. This will fail when going from a one month to a new month and also at the end of the year.
You have to encode the year, month and day.
 

ErnieM

Joined Apr 24, 2011
8,377
He'll get there now, just needed a little push.

Getting the number of days passed from the month is a bit trickier as months vary in size. That's best handled by a static array to look up the correct answer.

static int DAYS_PASSED[] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273,304, 334}

Give that array an index of the month and it equals how many days have passed before the first of the month. so you add the date day number to it for total days.

Ex: March 5 DAYS_PASSED[2] + 5 = 59 + 5 = 64 days
 

theonewho

Joined Jul 9, 2015
17
If you value your sanity you will use date/time libraries that already exist in any language worth its salt. There are a lot of edge cases that make no sense at all when dealing with time. Other people have already done all that hard work for you. You may be able to use time.h for this. Don't try and reinvent the wheel. Google things.
 
Top