Trouble when reading data from DS1307 RTC

Having downloaded a sketch to display Time, Date and Temp etc. The Day of the Week data displayed shows a day which is two ahead of actual. I have exchanged the DS1307 module but same issue. I used a completely different and simple code to access date etc and that works fine so conclusion is that original downloaded code is in error. I cannot fathom out where that could be and if I Serial print various points in the sketch, I only see a "post error" condition. Some of the relevant code follows and I would appreciate any pointers as I'm just getting to grips with the Arduino so far. Thanks.

char *dow2str(uint8_t code, char *psz, uint8_t len)
{
  static const char str[][10] PROGMEM =
  {
    "Sunday", "Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday", "Saturday"
  };

  *psz = '\0';
  code--;
    if (code < 7)
  {
    strncpy_P(psz, str[code], len);
    psz[len] = '\0';
  }

  return(psz);
}
void getTime(char *psz, bool f = true)
// Code for reading clock time
{
  sprintf(psz, "%02d%c%02d", RTC.h, (f ? ':' : ' '), RTC.m);
}
void getsecond(char *psz)
// Code for reading clock date
{
  char  szBuf[10];
  sprintf(psz, "%02d", RTC.s);
}
void getDate(char *psz)
// Code for reading clock date
{
  
  char  szBuf[10];
    sprintf(psz, "%d %s %04d", RTC.dd, mon2str(RTC.mm, szBuf, sizeof(szBuf)-1), RTC.yyyy);
  //Serial.println (sprintf(psz, "%d %s %04d", RTC.dd, mon2str(RTC.mm, szBuf, sizeof(szBuf)-1), RTC.yyyy));
}
void getTemperature()
{
DHT.read11(DHT11_PIN);
  h = DHT.humidity;
  t = DHT.temperature;
  // Read temperature as Fahrenheit
  f = (1.8 * DHT.temperature)+32;
}
void setup(void)
{
// initialise the LED display
  Serial.begin (9600);
  P.begin(4);
  // Set up zones for 4 halves of the display
  // Each zone gets a different font, making up the top
  // and bottom half of each letter
  P.setZone(0, 0, 1);
  P.setZone(1, 2, 7);
  P.setZone(2, 10, 15);
  P.setZone(3, 8, 9);
  
      P.setFont(0, NULL);
      P.setFont(1, BigFontLower);
      P.setFont(2, BigFontUpper);
      P.setFont(3, NULL);
  P.displayZoneText(0, szsecond, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
  P.displayZoneText(1, szTime, PA_LEFT, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
  P.displayZoneText(2, szTime, PA_LEFT, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
  P.displayZoneText(3, szMesg, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
  P.addChar('$', degC);
  P.addChar('&', degF);
  RTC.control(DS1307_CLOCK_HALT, DS1307_OFF);
  RTC.control(DS1307_12H, DS1307_OFF);
    RTC.readTime();
}

void loop(void)
{
  static uint32_t lastTime = 0; // millis() memory
  static uint8_t  display = 0;  // current display mode
  static bool flasher = false;  // seconds passing flasher
  P.displayAnimate();
  if (P.getZoneStatus(3))
  {
    switch (display)
    {

      case 0: // day of week
        P.setTextEffect(3, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
        display++;
        dow2str(RTC.dow, szMesg, MAX_MESG);
        break;
        
      case 1:  // Calendar
        P.setTextEffect(3, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
        display++;
        getDate(szMesg);
        break;

Some of the code is not enough. Please post the entire sketch. Also please provide some context. What does this code do? What hardware is it written for? Where did you get it? What is a "post error" condition?

OK. What follows is the whole code/sketch. The code will provide Time, Date, and Weather data onto a set of MX7219 LED displays.(two banks of 8 modules). The original code was downloaded from an article on Youtube (Great Projects) and runs from an UNO board, with an TinyRTC DS1307 and DHT11 module. As I said, Everything works but the Day of the Week produced is two days ahead of real actual day. i.e. instead of displaying Monday, it shows Wednesday, and that two day ahead error is only produced in this script.

[code]

//- Optional use of DS1307 module for time and DHT11 sensor for temp and humidity
// - DHT11 library (DHT11) found at https://github.com/RobTillaart/Arduino
// - DS1307 library (MD_DS1307) found at https://github.com/MajicDesigns/DS1307
// NOTE: MD_MAX72xx library must be installed and configured for the LED
// matrix type being used. Refer documentation included in the MD_MAX72xx
// library or see this link:
// https://github.com/MajicDesigns/MD_Parola
// https://github.com/MajicDesigns/MD_MAX72XX

//https://www.youtube.com/channel/UCCC8DuqicBtP3A_aC53HYDQ/videos

// Use the DS1307 clock module
#define USE_DS1307 0
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include "Font_Data.h"
#include <MD_DS1307.h>
#include <Wire.h>

// Use the DHT11 temp and humidity sensor
#include "dht.h"
dht DHT;
#define DHT11_PIN 2
  float h = 0;
  float t = 0;
  float f = 0;

// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN    10

// HARDWARE SPI
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, 16);
// SOFTWARE SPI
//int MAX_DEVICES =16;
//MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);

#define SPEED_TIME  80
#define PAUSE_TIME  10
#define MAX_MESG  20

// Global variables
char szTime[9];    // mm:ss\0
char szMesg[MAX_MESG+1] = "";
char szsecond[4];    // ss

uint8_t degC[] = { 6, 3, 3, 56, 68, 68, 68 }; // Deg C
uint8_t degF[] = { 6, 3, 3, 124, 20, 20, 4 }; // Deg F

char *mon2str(uint8_t mon, char *psz, uint8_t len)
// Get a label from PROGMEM into a char array
{
  static const char str[][4] PROGMEM =
  {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  };

  *psz = '\0';
  mon--;
  if (mon < 12)
  {
    strncpy_P(psz, str[mon], len);
    psz[len] = '\0';
  }

  return(psz);
}

char *dow2str(uint8_t code, char *psz, uint8_t len)
{
  static const char str[][10] PROGMEM =
  {
    "Sunday", "Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday", "Saturday"
  };

  *psz = '\0';
  code--;
    if (code < 7)
  {
    strncpy_P(psz, str[code], len);
    psz[len] = '\0';
  }

  return(psz);
}
void getTime(char *psz, bool f = true)
// Code for reading clock time
{
  sprintf(psz, "%02d%c%02d", RTC.h, (f ? ':' : ' '), RTC.m);
}
void getsecond(char *psz)
// Code for reading clock date
{
  char  szBuf[10];
  sprintf(psz, "%02d", RTC.s);
}
void getDate(char *psz)
// Code for reading clock date
{
  
  char  szBuf[10];
    sprintf(psz, "%d %s %04d", RTC.dd, mon2str(RTC.mm, szBuf, sizeof(szBuf)-1), RTC.yyyy);
  //Serial.println (sprintf(psz, "%d %s %04d", RTC.dd, mon2str(RTC.mm, szBuf, sizeof(szBuf)-1), RTC.yyyy));
}
void getTemperature()
{
DHT.read11(DHT11_PIN);
  h = DHT.humidity;
  t = DHT.temperature;
  // Read temperature as Fahrenheit
  f = (1.8 * DHT.temperature)+32;
}
void setup(void)
{
// initialise the LED display
  Serial.begin (9600);
  P.begin(4);
  // Set up zones for 4 halves of the display
  // Each zone gets a different font, making up the top
  // and bottom half of each letter
  P.setZone(0, 0, 1);
  P.setZone(1, 2, 7);
  P.setZone(2, 10, 15);
  P.setZone(3, 8, 9);
  
      P.setFont(0, NULL);
      P.setFont(1, BigFontLower);
      P.setFont(2, BigFontUpper);
      P.setFont(3, NULL);
  P.displayZoneText(0, szsecond, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
  P.displayZoneText(1, szTime, PA_LEFT, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
  P.displayZoneText(2, szTime, PA_LEFT, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_NO_EFFECT);
  P.displayZoneText(3, szMesg, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
  P.addChar('$', degC);
  P.addChar('&', degF);
  RTC.control(DS1307_CLOCK_HALT, DS1307_OFF);
  RTC.control(DS1307_12H, DS1307_OFF);
    RTC.readTime();
}

void loop(void)
{
  static uint32_t lastTime = 0; // millis() memory
  static uint8_t  display = 0;  // current display mode
  static bool flasher = false;  // seconds passing flasher
  P.displayAnimate();
  if (P.getZoneStatus(3))
  {
    switch (display)
    {

      case 0: // day of week
        P.setTextEffect(3, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
        display++;
        dow2str(RTC.dow, szMesg, MAX_MESG);
        break;
        
      case 1:  // Calendar
        P.setTextEffect(3, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
        display++;
        getDate(szMesg);
        break;

      case 2: // Temperature deg F
        P.setTextEffect(3, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
        display++;
        getTemperature();
          dtostrf(t, 3, 1, szMesg);
          strcat(szMesg, "$");
        break;

      case 3: // Temperature deg F
        P.setTextEffect(3, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
        display++;
          dtostrf(f, 3, 1, szMesg);
          strcat(szMesg, "&");
        break;

      case 4: // Relative Humidity
        P.setTextEffect(3, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
        display = 0;
          dtostrf(h, 3, 0, szMesg);
          strcat(szMesg, "% RH");
        break;
    }

    P.displayReset(3);
  }
  // Finally, adjust the time string if we have to
  if (millis() - lastTime >= 1000)
  {
    RTC.readTime();
    lastTime = millis();
    getsecond(szsecond);
    getTime(szTime, flasher);
    flasher = !flasher;
    P.displayReset(0);
    P.displayReset(1);
    P.displayReset(2);
  }
  }

What about the end days? You said two days ahead, what does it display if it is now the last day of the week? Does it wrap around to the second day?

By the way, there are no "scripts" in Arduino coding.

Also, again, what is a "post error" condition?

Lastly, did you add day of week functionality to the program yourself, or did it already handle it?

Have you tested the function 'dow2str()' to make sure it works?

I presume you are enquiring about weekend scenarios? Don't know about the roll over as I would have to re-programme a DS1307 module to set up that situation (or wait a few more days.)
However the real issue is why is it doing it at present?
"post error" is my way of explaining "after the error has occurred in the programme".
The full sketch is "as is". No additions or home changes.
I put in a Serial.print command just to look at "dow2str......" and it prints out "wednesday" for today - Monday.

case 0: // day of week
        P.setTextEffect(3, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
        display++;
        dow2str(RTC.dow, szMesg, MAX_MESG);
        Serial.println (dow2str(RTC.dow, szMesg, MAX_MESG));  // my code
        break;

Well, that was a troubleshooting idea. But if you can't change the RTC time, oh well...

If it wraps, it's probably an RTC issue. If not, it's probably a coding issue.

How did you set the time on the RTC? Some libraries set/read the actual DOW register, some don't. So you might have a mismatch there. Yet another reason to try changing the time.

Found another piece of software via Arduino that sets up (and reads back) all of the RTC parameters. Obviously used it and all is fine.
Set up two RTC modules with this, moved them one at a time for testing onto the present sketch set up and zippo. problem.
Yesterday, found within the sketch (thats the full listing sent in) that the word "code" seems to get a numeric value relating to the DOW. If you care to look its where the coder has set 'CODE--;'
So I reduced its value to coincide with what I assumed would be a correct value for DOW and bingo, it now works - but now struggling to find out why this gets an incorrect value in first place????

Thanks for the response and feel that issue is now fixed. By changing RTC module for a different manufacturer and loading it up with correct date & time etc, It all works fine. Using the same sketch as listed here so must be something weird within modules (Tiny Rtc) and or combination of those and Lib file. Hopefully all will stay well now.#Thanks again.

Well, there are known, documented DS1307 fakes floating around. So maybe that....

I think you have hit the button. I've been visually comparing the DS1307 TinyRTC modules and there are some physical differences. Almost unnoticeable unless you look closely.