Reputation: 87
I'm trying to understand a function call that doesn't supply the required parameters but seems to work. The code is in an Arduino library called RTClib. Why/how does this work????
The function making the call:
uint8_t DateTime::dayOfWeek() const {
uint16_t day = date2days(yOff, m, d);
return (day + 6) % 7; // Jan 1, 2000 is a Saturday, i.e. returns 6
}
The function being called:
static uint16_t date2days(uint16_t y, uint8_t m, uint8_t d) {
if (y >= 2000)
y -= 2000;
uint16_t days = d;
for (uint8_t i = 1; i < m; ++i)
days += pgm_read_byte(daysInMonth + i - 1);
if (m > 2 && y % 4 == 0)
++days;
return days + 365 * y + (y + 3) / 4 - 1;
}
The full library: https://github.com/adafruit/RTClib
Upvotes: 0
Views: 574
Reputation: 1433
The required variables are from the DateTime class. They are protected variables, so all methods in the DateTime class can access them.
As seen in RTClib.h line 27:
protected:
uint8_t yOff, m, d, hh, mm, ss;
Those variables are set by the various functions in RTClib.cpp, such as the constructors that first initialize them:
DateTime::DateTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) {
if (year >= 2000)
year -= 2000;
yOff = year;
m = month;
d = day;
hh = hour;
mm = min;
ss = sec;
}
Upvotes: 2