Unix time from DS3231

Hi, i have read a lot of posts about "normal time" and Unix time as it would be much easier for me to do some calculations using the Unix time, especially because i dont need to convert it back into normal time at the end. But it seems that it is not so easy (well at least needs a bit of code) to get the Unix time out of the DS3231 (or am i missing something here) i would have thought that it is working in Unix time anyway and it would be easy for it just to give you that value anyway...?? If not then i will just do it the long way... :slight_smile:

i would have thought that it is working in Unix time anyway and it would be easy for it just to give you that value anyway...??

The most simple thing to do when you have thoughts like that is to look at the data sheet for the device. You would see that there is no internal storage register for Unix time.

But it seems that it is not so easy (well at least needs a bit of code) to get the Unix time out of the DS3231 (or am i missing something here)

Many Arduino libraries for the DS3231 have a function to return the Unix time stamp derived from the internal time storage register. Are you using a library?

Jack Christensens excellent library DS3232.h which works with the DS3231 returns the Unix time stamp with

rtc.get();

RTClib.h returns Unix time with two lines

DateTime now = rtc.now();
uint32_t timestamp = now.unixtime());

Thanks for the data sheet tip, i have never thought about looking there. :slight_smile:

Sorry for not saying which library i am using, should have thought about that, i am using the <DS3231.h> library is there the same fetcher for this?? and where could i read up myself to find that out (sorry probably a very stupid question)? If it does not have can i just include Jack Christensens DS3232.h one and then just use the rtc.get() to get the time or is it bad practice to have two libraries for the same hardware?

It is generally bad practice to have two separate libraries controlling the same hardware.

Thanks John, yes i had a feeling that it might be, do you know where i might find out what the unix time stamp is for the DS3231.h library, or if it has one? if not i will have to change to Jack Christensens DS3232.h one...

Use the Arduino Time library to create unix time stamps. It can synchronize with the DS3231 to keep time, as well.

Sorry for not saying which library i am using, should have thought about that, i am using the <DS3231.h> library is there the same fetcher for this??

There are several libraries with the same name. DS3231.h You will have to be more specific.

Looking at library source code is a good habit to develop. Most are available for review on GitHub.

If you have actually added the library to the libraries folder. Any text editor should be able to open the .h and .cpp files for you to look at. The library code for user added libraries is located in the libraries folder along with your sketches in the Arduino documents folder.

Thanks! found it in the folders yes!

Anyone is reading this at a later date and using the Rinky-Dink Electronics, Henning Karlsen DS3231.h the line of code you need for getting unix is rtc.getUnixTime(rtc.getTime())

:slight_smile: