For a project I needed to read data from EEPROM for every different day of the year. Using the day of the year from 1-365 as an index to read the memory seemed the easiest way. My RTClib library (from Adafruit) sadly didnt know a ‘day of the year’ function So I set out to make or find one. I came across a site where this was undertaken, but sadly that had a flaw that was probably not noticed by the owner because he only used it to determine if a day was even or odd. It gave me a good start though.
It entails altering the RTClib.h and RTClib.cpp files from the Adafruit RTC library.
First take the RTClib.h file and add the line as shown below:
Then open the RTClin.cpp file and add the function as shown below.
This function cycles through an Array of ‘number of days per month’ stored in Flashram with PROGMEM. It starts by determining the current day and then add the days in the months from january till the current month to it.
Then it determines if february already passed and checks if it is a leapyear. If so it adds an extra day.
To use the function: define, initialize and start the RTC as usual. The call to “now.dayOfYear()” returns the number of days into the year.
The function is m.m.somewhat akin to the ‘days since 1-1-2000’ in the lib.
This function will work up to 2100 because that year is not a leap year (centuries as arule are not leap years). I could correct for that but then I also would have to correct for the year 2400 as that ís a leap year again (centuries are no leap years… unless the century is a multitude of 400).