RTC Library with Day number of Year

Subject says it all. I need the number of the current day or some code that will calculate it from the available Library data.

Can I please get some help with this?

If that is not available one-button from an RTC library, it can surely be done using Julian Day number routines, and subtract that for the previous December 31. Have a look through Peter Duffet-Smith or Jean Meeus.

See this link.

http://forum.arduino.cc/index.php?topic=44476.0

It worked six years ago, so it will probably work again. It's Kind of a brute force solution, but since you probably won't be calling it in a permanent Loop...

Thank you both. @JaBa: That link looks like it will do what I need.

Man there are some smart people out there. This works brilliantly, about 1/3rd way down by “chansen”…

int rdn(int y, int m, int d) { /* Rata Die day one is 0001-01-01 */
    if (m < 3)
        y--, m += 12;
    return 365*y + y/4 - y/100 + y/400 + (153*m - 457)/5 + d - 306;
}

int days = rdn(2013, 1, 8) - rdn(2012, 1, 24);

I made few functions like that, if that can help…

uint8_t getWeekDay( uint8_t day, uint8_t month, uint16_t year )
{
  if ( month <= 2 )
  {
    month += 12;
    --year;
  }

  uint8_t j = year % 100;
  uint8_t e = year / 100;
  return ( ( ( day + ( ( ( month + 1 ) * 26 ) / 10 ) + j + ( j / 4 ) + ( e / 4 ) - ( 2 * e ) ) - 2 ) % 7 );
}

uint8_t getLastSundayInMonth( const uint8_t month, const uint16_t year )
{
  uint8_t d = getDaysInMonth( month, year );
  while ( getWeekDay( --d, month, year ) != 6 );
  return d;
}

uint16_t getDayOfYear( const uint8_t day, const uint8_t month, const uint16_t year )
{
  uint8_t d = day;
  uint8_t m = month;
  while ( --m ) d += getDaysInMonth( m, year );
  return d;
}

bool isLeapYear( const uint16_t year )
{
  return ( (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0) );
}

uint16_t getDaysInYear( const uint16_t year )
{
  return isLeapYear( year ) ? 366 : 365;
}

uint16_t getNextLeapYear( const uint16_t year )
{
  uint16_t y = year;
  while ( !isLeapYear( ++y ) );
  return y;
}

uint16_t getPreviousLeapYear( const uint16_t year )
{
  uint16_t y = year;
  while ( !isLeapYear( --y ) );
  return y;
}

uint8_t getDaysInMonth( const uint8_t month, const uint16_t year )
{
  if ( month == 2 )  
    return isLeapYear( year ) ? 29 : 28;
    
  else if ( month == 4 || month == 6 || month == 9 || month == 11 )  
      return 30;
      
  return 31;
}

:slight_smile:

SalineSolution:
Man there are some smart people out there. This works brilliantly, about 1/3rd way down by “chansen”…
http://stackoverflow.com/questions/14218894/number-of-days-between-two-dates-c

int rdn(int y, int m, int d) { /* Rata Die day one is 0001-01-01 */

if (m < 3)
        y–, m += 12;
    return 365y + y/4 - y/100 + y/400 + (153m - 457)/5 + d - 306;
}

int days = rdn(2013, 1, 8) - rdn(2012, 1, 24);

365 * 100 is already too big to fit in an int.

365 * 2013 is way too big to fit in an int.

odometer: 365 * 100 is already too big to fit in an int.

365 * 2013 is way too big to fit in an int.

{sigh} Where would we be if there were no theorists around. As well as that maybe, but it is working just fine. AND, with those comments in mind, I have tested to the year 2065 and all good. Dare I say it, but my Arduinos will not last that long.

I suspect that some even more clever people at GCC or whatever compiler Arduino-IDE uses, have made the calculations using Long or even Long Long Arithmetic and then returning the value AFTER they have a Result. Only then, might it overflow/fail unless Casting was used in the original.

IF you really wanted to help, then instead of deriding other folks cleverness, you could have made changes to the code and re-displayed it. This IS after all, supposed to be a Help system.

SalineSolution: IF you really wanted to help, then instead of deriding other folks cleverness, you could have made changes to the code and re-displayed it. This IS after all, supposed to be a Help system.

That, or I could just refer you to some code of my own: http://forum.arduino.cc/index.php?topic=358129.msg2481890#msg2481890