RTClib and PCF8523 - Subtract time - maybe a bug?

Hi.Forum.
Using RTC Lib and Adafruit PCF8523 shield.
I need to know the time difference between two button presses also after days.
So first press I invoke in global variables a function that stores the timestamp.
void CatchTime() {
DateTime Start = rtc.now();
anno = Start.year();
mese = Start.month();
giorno = Start.day();
ora = Start.hour();
minuto = Start.minute();
secondo = Start.second();

Then at second button press I invoke the function passed:
void ElapsedTime() {
DateTime passed = (rtc.now() - TimeSpan(giorno, ora, minuto, secondo));

But guess what?

Pushing two buttons at the same day the
Serial.print(passed.day(), DEC); command instead of giving me 0Days gives me 31Days..

How could it be possible?
Best regards and thanks
Any help appreciated

How accurately do you need to measure the time difference ?

As accurate as possible in the second domain.
I mean considering the accuracy of the RTC just the calculation of two timestamps.
First stamp: Y1M1D1-H1M1S1 store it in a variable
Second stamp: actual YMD-HMS store it
Subtract and calculate how many months days, etc have passed.
why are you asking that?
Thank You

I was wondering whether you needed to use an RTC or not, Considering the fact that the button presses are going to be days apart

does not really seem to make much sense

If you do use an RTC do not store the YMD-HMS of the events, store the Unix time instead and do the calculation of the elapsed time based on that

As UKHeliBob says, use unixtime to record the events

RTClib.h makes it simple in that there is a function now.unixtime() which returns the seconds since 1970 from the year/day/hour/minute/second values stored in the rtc registers and returned to the Arduino with the call to rtc.now().

The reason for the days difference reported as 31 is that you are misusing the TimeSpan object
TimeSpan(giorno, ora, minuto, secondo) is just a number of seconds.

TimeSpan::TimeSpan (int16_t days, int8_t hours, int8_t minutes, int8_t seconds):
  _seconds((int32_t)days*86400L + (int32_t)hours*3600 + (int32_t)minutes*60 + seconds)
{}

It does not make sense to be using in like this

DateTime passed = (rtc.now() - TimeSpan(giorno, ora, minuto, secondo));

Review of the RTClib library example datecalc.ino shows how to get a timespan from two datetime objects and how to print it.

However, you will find if more simple to just work directly with now.unixtime().

You can extract hours,minutes and seconds from a difference between two time values in seconds like something like this this

delta_time =  endTime - startTime;  //delta_time from now.unixtime() is in seconds

deltaHours = (deltaTime/ 3600); //extract hours with integer math

deltaMinutes = (delta_time % 3600) / 60; //extract minutes with modulus and integer math

deltaSeconds = deltaTime % 60; //extract seconds with modulus

Thank You, pal!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.