calculate time difference

Discussion in 'Embedded Systems and Microcontrollers' started by ak52, Jul 16, 2015.

  1. ak52

    Thread Starter Member

    Oct 15, 2014
    145
    4
    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
     
  2. mitko89

    Member

    Sep 20, 2012
    123
    19
    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.
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  4. ak52

    Thread Starter Member

    Oct 15, 2014
    145
    4
    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. :(
     
  5. mitko89

    Member

    Sep 20, 2012
    123
    19
    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?
     
  6. ak52

    Thread Starter Member

    Oct 15, 2014
    145
    4
    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.
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
    theonewho and ak52 like this.
  8. ak52

    Thread Starter Member

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

    Edit:
    The code if anyone need it.

    Code (Text):
    1.  
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. int get_time_diff(int*,int*);
    5. int main()
    6. {
    7.   int old_time[] = {23,59,00,15,7,2015};  //hour,minute,sec,day,month,year
    8.   int new_time[] = {00,00,30,16,7,2015};
    9.  
    10.   return get_time_diff(old_time,new_time);
    11. }
    12. int get_time_diff(int*old_t,int *new_t)
    13. {
    14.   int day_difference = 0;
    15.   long new_t_sec = 0;
    16.   long old_t_sec = 0;
    17.   if(new_t[3] - old_t[3] == 0)
    18.   {
    19.   //convert hours to seconds
    20.   old_t[0] = (old_t[0]*60)*60;
    21.   new_t[0] = (new_t[0]*60)*60;
    22.  
    23.   //convert minutes to seconds
    24.   old_t[1] = old_t[1]*60;
    25.   new_t[1] = new_t[1]*60;
    26.  
    27.   //add all the seconds
    28.   old_t_sec = old_t[0] + old_t[1] + old_t[2];
    29.   new_t_sec = new_t[0] + new_t[1] + new_t[2];
    30.  
    31.   return (new_t_sec - old_t_sec);
    32.   }
    33.   else
    34.   {
    35.   day_difference = (new_t[3] - old_t[3])*24;
    36.   //convert hours to seconds
    37.   old_t[0] = (old_t[0]*60)*60;
    38.   new_t[0] = ((new_t[0] + day_difference)*60)*60;
    39.  
    40.   //convert minutes to seconds
    41.   old_t[1] = old_t[1]*60;
    42.   new_t[1] = new_t[1]*60;
    43.  
    44.   //add all the seconds
    45.   old_t_sec = old_t[0] + old_t[1] + old_t[2];
    46.   new_t_sec = new_t[0] + new_t[1] + new_t[2];
    47.  
    48.   return (new_t_sec - old_t_sec);
    49.   }
    50. }
    51.  
    52.  
     
    Last edited: Jul 16, 2015
    ErnieM likes this.
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Very good work, congrats.
     
  10. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,360
    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.
     
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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
     
  12. theonewho

    New Member

    Jul 9, 2015
    17
    2
    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.
     
    ErnieM likes this.
Loading...