Data Error when store Unix Time

Hi,

I am using an Arduino Nano ATmega328P and when I run my code using the lib RTClib.h the stored Unix Time in the variable has an error.

This is my code:

#include <RTClib.h>

float clock_unix;

void setup() {
  Serial.begin(9600);
}

void loop() {
  DateTime now = rtc.now();
  Serial.print("Unix Time: ");
  Serial.println(now.unixtime());
  clock_unix = (now.unixtime());
  Serial.print("Clock Unix: ");
  Serial.println(clock_unix);
}

And this is my Output from Serial Port:

19:02:23.639 -> Unix Time: 1566500509
19:02:23.671 -> Clock Unix: 1566500480.00

Why they are different? How to store the right Unix Time on a variable?

Nice job on your first post.

The variable type for "unix time" is a uint32_t or unsigned long in Arduino speak.

unsigned long clock_unix;

The float data type has only 6-7 decimal digits of precision.
https://www.arduino.cc/reference/en/language/variables/data-types/float/

Why they are different?

A float variable cannot accurately store a unix timestamp.

  • Karma for your first post and following the guidelines.

Please always match a functions return to the proper variable type. This will avoid issues like this.