Pages: [1]   Go Down
Author Topic: DS3234 Date calculations  (Read 1201 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Atlanta
Offline Offline
Full Member
***
Karma: 4
Posts: 128
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;

« Last Edit: March 04, 2012, 12:16:32 am by abrookfield » Logged


Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 smiley

Logged

Rob Tillaart

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys. Much appreciated.

Now to give it a go! smiley
Logged

Pages: [1]   Go Up
Jump to: