RTC clock call on setup and strange behavior

I would like to call rtc.now() during setup to do some math with dates. I am following the toString.ino example with minor modifications to show the behavior I am finding from rtc

#include <Wire.h>
#include <RTClib.h>

RTC_PCF8523 rtc;
DateTime finished;

void setup () {
  Serial.begin(57600);

#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

  DateTime finished = rtc.now();
}

void loop() {

   DateTime now = rtc.now();

   char buf2[] = "YYMMDD-hh:mm:ss";
   Serial.println("Setup finished");
   Serial.println(finished.toString(buf2));
   Serial.println("Now:");
   Serial.println(now.toString(buf2));

   char buf3[] = "Today is DDD, MMM DD YYYY hh:mm:ss";
   Serial.println(now.toString(buf3));
   
   delay(1000);
}

My serial monitor is showing

Setup finished
000101-00:00:00
Now:
000101-00:00:00
Today is Thu, Dec 08 2022 15:52:01
Setup finished
000101-00:00:00
Now:
000101-00:00:00
Today is Thu, Dec 08 2022 15:52:02
Setup finished
000101-00:00:00
Now:
000101-00:00:00
Today is Thu, Dec 08 2022 15:52:03

I am not sure where the problem is and how the same object could be both 000101-00:00:00
and today's datetime.
If I try to do math with the rtc.now() finished, for example adding a day with finished + TimeSpan(0, 24, 0, 0); I get the same date back. Appreciate any help with this issue

Adding edits to show that the suggested method by @anon57585045 also produces weird results

#include <Wire.h>
#include <RTClib.h>

RTC_PCF8523 rtc;
DateTime finished;
DateTime finishedOffset;

void setup () {
  Serial.begin(57600);

#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }

  DateTime finished = rtc.now();
  long zoneOffset = -5L * 3600;
  DateTime finishedOffset = DateTime(finished.unixtime() + zoneOffset);
}

void loop() {

   DateTime now = rtc.now();

   char buf2[] = "YYMMDD-hh:mm:ss";
   Serial.println("Setup finished");
   Serial.println(finished.toString(buf2));
   Serial.println("Offset time is:");
   Serial.println(finishedOffset.toString(buf2));
   Serial.println("Now:");
   Serial.println(now.toString(buf2));

   char buf3[] = "Today is DDD, MMM DD YYYY hh:mm:ss";
   Serial.println(now.toString(buf3));
   
   delay(1000);
}

Here is how I made a time offset, perhaps it will illuminate something for you

long zoneOffset = -5L * 3600;
...
    DateTime now = rtc.now();
    DateTime tNow = DateTime(now.unixtime() + zoneOffset);

Thank you for the input. Although using unixtime() + zoneOffset is a different approach, it yields the same result. I don't understand what's going on when handling dates inside setup()

Setup finished
000101-00:00:00
Offset time is:
000101-00:00:00
Now:
000101-00:00:00
Today is Thu, Dec 08 2022 16:07:13

You have declared this variable twice. Once as a global that is accessed in loop() but it is never initialised (therefore 000101-00:00:00), and again in setup() were it is initialised.

Change setup to...

finished = rtc.now();
1 Like

Thank you! I knew there was some stupid thing going on!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.