Data Error when store Unix Time


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() {

void loop() {
  DateTime now =;
  Serial.print("Unix Time: ");
  clock_unix = (now.unixtime());
  Serial.print("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.

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.