Go Down

Topic: DS3234 Date calculations (Read 2304 times) previous topic - next topic

bjaaz

Hello,

I need some help with programming for the DS3234. Specifically Date calculations.

I'm using Spark Fun's DeadOn RTC DS3234 and an Uno.

I've played with JeeLabs RTClib for the DS3234, and can easily set the datetime and output the datetime via the serialmonitor.

What I want to achieve is to simply work out the number of days between the current date and a date that I have set. All I need to output is the number of elapsed days as a number. I just have no idea how to achieve it.

Any help would be greatly appreciated.

abrookfield

#1
Mar 04, 2012, 05:46 am Last Edit: Mar 04, 2012, 06:16 am by abrookfield Reason: 1
Convert both your dates to Julian Date and subtract one from the other to get the difference...

use integer variables


  A = Y/100;
  B = A/4;
  C = 2-A+B;
  E = 365.25x(Y+4716);
  F = 30.6001x(M+1);
  JD= C+D+E+F-1524.5;

www.reeftopper.com

robtillaart

Quote
use integer variables

use long (especially for JD) as julian date's can exceed the integer limits.

added some naming to the code above to better understand it.

Definition: The Julian date (JD) is a continuous count of days from 1 January 4713 BC.

Code: [Select]
long JulianDate(int year, int month, int day)
{
  long centuries = year/100;
  long leaps = centuries/4;                             
  long leapDays = 2 - centuries + leaps;         // note is negative!!
  long yearDays = 365.25 * (year + 4716);     // days until 1 jan this year
  long monthDays = 30.6001* (month + 1);    // days until 1st month
  long jd = leapDays + days + monthDays + yearDays -1524.5;
  return jd;
}


There are also formulas for calculating back to Gregorian e.g. when will I be 10000 or 20000 days old.
- http://aa.usno.navy.mil/faq/docs/JD_Formula.php -
- http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html -

coding this is left as an exercise for the reader :)

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

bjaaz

Thanks guys. Much appreciated.

Now to give it a go! :)

slawa92

Ran into similar problem recently, just wanted to share my solution. The idea behind my solution is to take a date, far back in time, I've taken 1/1/2015 (D/M/Y format) ( actually "any" other reasonable date can be used with my function ) and simply calculate how many days passed since that date using a simple function:

Code: [Select]
//==========================================================
//CODE CALCULATES DAYS BETWEEN NOW AND 1ST JAN 2015
int daysSince2015 (int days, int months, int years)
{
  int daysPerMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
 
  int daysPassed = 0;
 
  int startDays = 1;
  int startMonth = 1;
  int startYears = 2015;
 
  //KEEP SCROLLIN' TILL THE REQUIRED YEAR IS REACHED
  for (startYears = 2015; startYears != years; startYears ++)
  {
    for (int n = 0; n < 12; n++)
    {
      //ONE MONTH AT A TIME
      daysPassed += daysPerMonth [n];
      //ADD LEAP DAY IF REQUIRED
      if ((n == 1) && ((startYears % 4) == 0))
      {
        daysPassed += 1;
      }
    }
  }

  //SCROLL MONTHS IF THE MONTHS IS NOT JANUARRY
  if (startMonth != months)
    {
      //SCROLL MONTH, USE startMonth AS A CURSOR TO daysPerMonth ARRAY
      //KEEP IN MIND THAT 1ST ELEMENT OF daysPerMonth ARRAY IS AT daysPerMonth[0]
      for (startMonth = 1; startMonth != months; startMonth ++)
      {
          //ONE MONTH AT A TIME
          daysPassed += daysPerMonth [startMonth - 1];
          //ADD A DAY IF IT'S FEBRUARY OF A LEAP YEAR
          if ((startMonth == 2) && ((startYears % 4) == 0))
          {
            daysPassed += 1;
          }
      }
    }
   
    //SINCE WE ARE ON THE SAME MONTH AND YEAR, JUST CALCULATE DAY DIFFERENCE
    daysPassed += days - startDays;

    //PRINT THE RESULT
    Serial.print("DAYS PASSEDS: ");
    Serial.println(daysPassed);

    //RETURN THE RESULT
    return daysPassed;

 
}
//=============================================================


Calculate this function for both dates of interest, the result of subtraction will represent the days difference between dates. The function was tested, and the results are proven using : https://www.timeanddate.com/date/durationresult.html?d1=1&m1=1&y1=2015&d2=1&m2=1&y2=2018

PaulS

Code: [Select]
  for (startYears = 2015; startYears != years; startYears ++)
That's going to produce some strange results for January 18, 2014.
The art of getting good answers lies in asking good questions.

slawa92

#6
Dec 06, 2016, 04:03 pm Last Edit: Dec 06, 2016, 04:09 pm by slawa92
Two quite obvious ways you can avoid it:
  • Either use with dates that come after 01.01.2015
  • Or , perhaps replace 01.01.2015 with something else, you even can use Epoch date. As far as you use 1st day of the 1st month it will work.


I use the given code to work with meaningful dates (present and future), as I cant see how would I need dates from past in my automation projects. I wrote this function as I had to calculate how many days in between of a current date and a date in the future, for me this approach works.

How I normally use this function:

int startDays = daysSince2015(guardDay, guardMonth, guardYear);
int endDays = daysSince2015(Td, TM, Ty);
int daysInbetween = endDays - startDays;

And, sure, start date is earlier than the end date.

jurs

There are also formulas for calculating back to Gregorian e.g. when will I be 10000 or 20000 days old.
- http://aa.usno.navy.mil/faq/docs/JD_Formula.php

Reference: Fliegel, H. F. & van Flandern, T. C. 1968, Communications of the ACM, 11, 657.

Code reference to the Apollo 11 ACM (Apollo Command Module)

Hehe, anybody remembers?
Apollo11 was a big success, landing the first man on the moon, so perhaps the date algorithm will work successfully, too.
:smiley-lol:

PaulS

Quote
Two quite obvious ways you can avoid it:

    Either use with dates that come after 01.01.2015
    Or , perhaps replace 01.01.2015 with something else, you even can use Epoch date. As far as you use 1st day of the 1st month it will work.
Whichever date is used, the program should properly handle out-of-range dates. That was my point. For some definition of "properly", of course.
The art of getting good answers lies in asking good questions.

KeithRB

The Command Module might have been OK, but the Lander had a "bug" (actually operator error) that almost caused the mission to be scrubbed.

el_supremo

#10
Dec 06, 2016, 04:57 pm Last Edit: Dec 06, 2016, 04:59 pm by el_supremo
Use the Time library's makeTime function. When you have separate year, month, etc from the RTC, store those values in a tm_elements structure (I think I've got its name right). Then use makeTime to convert that to number of seconds since the beginning of 1970. Do that for both date/times. Then subtract.

Pete
P.S. I didn't notice how dead the original thread was until after I'd posted this.
Don't send me technical questions via Private Message.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy