How to get reliable yesterdays date?

Hi,

I am using the DS3231 RTC to generate file names whitch are then used to log some data on a SD card.
I wish to consolidate yesterdays log file into a smaller file and for that would need to open that file.
What I need is a date-1 function but DS3231's functions are separated on day,month,year,etc so I cannot see a way to get yesterday's proper date for my function to work.

Thanks in advance
Paulo

Use the Time library functions makeTime and breakTime. Initialize a tm_elements structure with day, month, year etc and use makeTime to convert that to time_t. Subtract 86400L from the time_t and then use breakTime to convert that back to a tm_elements which will then contain the correct day,month,year etc for the previous day.

Pete

"Time uses a special time_t variable type, which is the number of seconds elapsed since 1970. Using time_t lets you store or compare times as a single number, rather that dealing with 6 numbers and details like the number of days in each month and leap years."

To get yesterday, subtract the number on seconds in a day from now().

Refer to:
https://www.pjrc.com/teensy/td_libs_Time.html

.

Note that the constant SECS_PER_DAY is already defined for you in the Time library.

Hi, thank you all.

I am using
#include <DS3231.h>
That is the library for the hardware RTC I am using and I am having a hard time finding how to sincronize in a way I can have access to the function now().

Thanks
Paulo

pcborges:
Hi, thank you all.

I am using
#include <DS3231.h>
That is the library for the hardware RTC I am using and I am having a hard time finding how to sincronize in a way I can have access to the function now().

Thanks
Paulo

You call the setSyncProvider() function with the identity of the get method, in setup():

setSyncProvider(RTC.get);   // the function to get the time from the RTC

Change RTC.get to the name of the method appropriate to your type of RTC.

I dunno. I think it would be a lot less code, and a lot faster, (in the final sketch) if you wrote your own "yesterday" function that knew about days in each month and leap years, and so on, compared with including a library based on the unix "seconds since 1970" stuff. I mean, unix time is elegant and fits nicely in filesystem data structures and stuff, but it generally takes a lot of multiplications and divisions to go between the "seconds" and the current month/day/year/etc.

but it generally takes a lot of multiplications and divisions to go between the "seconds" and the current month/day/year/etc.

On the other hand, that code was written by people that knew what they were doing.

OP might, too, but my impression was that if he/she did, he/she wouldn't have had to ask.

It wouldn't be too bad as a "programming exercise." Not too trivial, but not really difficult, either.
Not an bad exercise is generating a test program, either...

westfw:
It wouldn't be too bad as a "programming exercise." Not too trivial, but not really difficult, either.
Not an bad exercise is generating a test program, either...

Posting the code here for review would be a good idea, too.