My application:
I need to calculate a date whenever a certain button is pushed. it should take the current date... add days e.g future_day = current_day + 21 or something like that. the reason why i am not going with the standard current_day + 21 is because the rtc library can recognize if the month has 28 days or 30 so if i fiddle with that.. i might malfunction something or it wont work.
Problem:
All the other functions are ready: i can read time. show time with the press of a button, everything is okay but i can't seem to figure out what the solution to this could be. i also need to write the future_day value to eeprom as well.. and read from it periodically so i need the time to be in a format which is usable.
Use the "Time" library (available in the IDE library manager) to convert the DS1307 time value into a unix time, make the calculation on that (seconds since 1970-01-01 00:00:00) and convert it back to any of the formats you need.
manhoosbilli1: My application:
I need to calculate a date whenever a certain button is pushed. it should take the current date... add days e.g future_day = current_day + 21 or something like that. the reason why i am not going with the standard current_day + 21 is because the rtc library can recognize if the month has 28 days or 30 so if i fiddle with that.. i might malfunction something or it wont work.
Here is a function that might help you. Give it a year and a month, and this function will give you the number of days in that month.
int days_in_month (int y, int m) {
// Fourth, eleventh, ninth, and sixth,
// thirty days to each we fix.
if ((m==4)||(m==11)||(m==9)||(m==6)) return 30;
// Every other, thirty-one,
// except the second month alone,
if (m!=2) return 31;
// which hath twenty-eight, in fine,
// till leap-year give it twenty-nine.
if ((y%400)==0) return 29; // leap year
if ((y%100)==0) return 28; // not a leap year
if ((y%4)==0) return 29; // leap year
return 28; // not a leap year
}
I will describe how you could apply this to your needs.
Today is October 18, 2018. What will the date be in 21 days?
For the day of the month, we calculate 18 + 21, which gets us 39.
Is October 39, 2018 a valid date? No, because there are only 31 days in October.
We "repair" this as follows:
We calculate 39 - 31, which gets us 8. Therefore, the date we want is the 8th day of the month after October 2018.
The result is therefore November 8, 2018.
pylon:
Use the "Time" library (available in the IDE library manager) to convert the DS1307 time value into a unix time, make the calculation on that (seconds since 1970-01-01 00:00:00) and convert it back to any of the formats you need.
I think the library i am using right now is using Time.h and is already able to convert BCD to DEC easily and into a readable time format. i think you misunderstood my question.
manhoosbilli1:
I think the library i am using right now is using Time.h and is already able to convert BCD to DEC easily and into a readable time format. i think you misunderstood my question.
Nope, it's apparent that you misunderstood the answer. Time.h #include(s) the file TimeLib.h. If you look in there, you'll find the prototypes for the functions 'breakTime()' and 'makeTime()'. These can be used for the technique recommended by @pylon.