Unixtime

I have question regarding the UNIX time:

I use a RTC with the related library (DS3231.h)

When I print the UNIX time:
Serial.println(clock.dateFormat("U", dt));
I get a big number like 1481980794...

Now I want to assign this value to a variable (a)

long a = 0;

a = (clock.dateFormat("U", dt);

When I now print the value of a, I get as output "1884"

Any explaination why I don't get the expected value for a ...?

Thanks J-M-L..

looks like I'm a bit overstrained with this answer as an Arduino beginner :slight_smile:

May be its better to use the uint32_t unixtime() function- but this seamst to be not in the library I'm using.
Where can I Download the right library ?

vbueschken:
When I print the UNIX time:
Serial.println(clock.dateFormat("U", dt));
I get a big number like 1481980794...

You actually don't get a big number - you get a pointer to a char buffer with a long number represented in ASCII.

You can see this if you look at the method signature is

[color=red]char* [/color]dateFormat(const char* dateFormat, RTCDateTime dt)

when you do a = (clock.dateFormat("U", dt); besides missing a parenthesis and not looking at the compiler warnings, what you do is store in your a variable the address of the pointer to the char buffer.

There is another function returning the unix time in your library

[color=green][b]uint32_t[/b][/color] unixtime()

note that it returns an unsigned long on 32 bits, not a long

The one you probably have code for is most likely GitHub - jarzebski/Arduino-DS3231: DS3231 Real-Time-Clock

You could also use that one DS3231 - Rinky-Dink Electronics

and there are many more I use often Adafruit's fork of Jeelab's RTC library GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC Arduino library which is good enough for most use