Arduino Unix TImestamp

Hi Everyone,

I am using the library (Arduino_TIme) for getting the current time on my Arduino MKR WIFI 1010 controller. It can be found here https://github.com/michaelmargolis/arduino_time/tree/master/Time.

The code I use is

#include <TimeLib.h>

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  time_t T = now();
  unsigned long seconds = (unsigned long) T;
  Serial.println(seconds);
}

The documentation for now() states

[color=#24292e]now(); // returns the current time as seconds since Jan 1 1970

The cast to unsigned long as stated in [u]https://en.wikipedia.org/wiki/C_data_types[/u] should be enough to hold the UNIX timestamp. 

But what is printed is something like
1
1
2
2
3
3[/color]

It is returning seconds since the now() was first run.

Any ideas why this could be happening?

Try this in setup() :

setTime( 1607912147 ) ; // Unix time stamp for now as an example but normally read from an RTC, NTP etc.

If you don’t initialize the time, it will behave as if the date is 1st January 1970.

6v6gt:
If you don’t initialize the time, it will behave as if the date is 1st January 1970.

when the Arduino powers up.

i.e. if you don't set the time, the time is zero on power up and will advance from there.

--- bill

Hi,
I had lots of trouble with time as well.
Figured it all out and explained it over here.