Setting Time Library with RTC

Hi all

I have been really struggling for the last few hours, with this code.

So using the time and time alarm, libraries, I have setup a watering system, it all works great and just what I need.

However I lose the programmed time if my battery dies, as the project is battery powered

So I thought I would use an RTC, no problem, but I need to "program the time library" directly from the RTC.

And that is the issue I am having, if anyone can think of a simple solution to enable me to use the time and timealarm libraries in conjunction with an RTC that would be great.

I have both the time library and the RTC printing their respective times, so all code is here, just cant work out how to set the time library time with the RTC!

Cheers in advance Alan

#include <TimeLib.h>
#include <TimeAlarms.h>
#include "RTClib.h"
#include <Wire.h>
#define Pump D8

RTC_DS3231 rtc;

void setup() {
  Serial.begin(9600);
  rtc.begin();
  pinMode(Pump, OUTPUT); //Pump
  digitalWrite(Pump, LOW);

  setTime(7, 29, 50, 1, 1, 11); // set time to Saturday 8:29:00am Jan 1 2011
  //rtc.adjust(DateTime(2019, 12, 16, 14, 29, 50));

  // Pump start and stop times
  Alarm.alarmRepeat(7, 00, 00, StartAlarm);
  Alarm.alarmRepeat(7, 00, 15, StopAlarm);
  Alarm.alarmRepeat(7, 30, 00, StartAlarm);
  Alarm.alarmRepeat(7, 30, 15, StopAlarm);
  Alarm.alarmRepeat(8, 00, 00, StartAlarm);
  Alarm.alarmRepeat(8, 00, 15, StopAlarm);
  Alarm.alarmRepeat(8, 30, 00, StartAlarm);
  Alarm.alarmRepeat(8, 30, 15, StopAlarm);
  Alarm.alarmRepeat(9, 00, 00, StartAlarm);
  Alarm.alarmRepeat(9, 00, 15, StopAlarm);
  Alarm.alarmRepeat(9, 30, 00, StartAlarm);
  Alarm.alarmRepeat(9, 30, 15, StopAlarm);
  Alarm.alarmRepeat(10, 00, 00, StartAlarm);
  Alarm.alarmRepeat(10, 00, 15, StopAlarm);
  Alarm.alarmRepeat(10, 30, 00, StartAlarm);
  Alarm.alarmRepeat(10, 30, 15, StopAlarm);
  Alarm.alarmRepeat(11, 00, 00, StartAlarm);
  Alarm.alarmRepeat(11, 00, 15, StopAlarm);
  Alarm.alarmRepeat(11, 30, 00, StartAlarm);
  Alarm.alarmRepeat(11, 30, 15, StopAlarm);
  Alarm.alarmRepeat(12, 00, 00, StartAlarm);
  Alarm.alarmRepeat(12, 00, 15, StopAlarm);
  Alarm.alarmRepeat(12, 30, 00, StartAlarm);
  Alarm.alarmRepeat(12, 30, 15, StopAlarm);
  Alarm.alarmRepeat(13, 00, 00, StartAlarm);
  Alarm.alarmRepeat(13, 00, 15, StopAlarm);
  Alarm.alarmRepeat(13, 30, 00, StartAlarm);
  Alarm.alarmRepeat(13, 30, 15, StopAlarm);
  Alarm.alarmRepeat(14, 00, 00, StartAlarm);
  Alarm.alarmRepeat(14, 00, 15, StopAlarm);
  Alarm.alarmRepeat(14, 30, 00, StartAlarm);
  Alarm.alarmRepeat(14, 30, 15, StopAlarm);
  Alarm.alarmRepeat(15, 00, 00, StartAlarm);
  Alarm.alarmRepeat(15, 00, 15, StopAlarm);
  Alarm.alarmRepeat(15, 30, 00, StartAlarm);
  Alarm.alarmRepeat(15, 30, 15, StopAlarm);
  Alarm.alarmRepeat(16, 00, 00, StartAlarm);
  Alarm.alarmRepeat(16, 00, 15, StopAlarm);
  Alarm.alarmRepeat(16, 30, 00, StartAlarm);
  Alarm.alarmRepeat(16, 30, 15, StopAlarm);
  Alarm.alarmRepeat(17, 00, 00, StartAlarm);
  Alarm.alarmRepeat(17, 00, 15, StopAlarm);
  Alarm.alarmRepeat(17, 30, 00, StartAlarm);
  Alarm.alarmRepeat(17, 30, 15, StopAlarm);
  Alarm.alarmRepeat(18, 00, 00, StartAlarm);
  Alarm.alarmRepeat(18, 00, 15, StopAlarm);
  Alarm.alarmRepeat(18, 30, 00, StartAlarm);
  Alarm.alarmRepeat(18, 30, 15, StopAlarm);
}

void loop() {
  digitalClockDisplay();
  Alarm.delay(1000);
}

void StartAlarm() {
  Serial.println("Alarm: - turn pump on");
  digitalWrite(Pump, HIGH);
  delay(100);
}

void StopAlarm() {
  Serial.println("Alarm: - turn pump off");
  digitalWrite(Pump, LOW);
  delay(100);
}

void digitalClockDisplay() {

  // digital clock display of the time library
  Serial.print(hour()); Serial.print(":");
  Serial.print(minute()); Serial.print(":");
  Serial.print(second());
  Serial.println();


  // digital clock display of the RTC
  DateTime now = rtc.now();
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
}

Look at the manual and examples for the TimeLib.
Specifically the

setSyncProvider(getTimeFunction); // set the external time provider
setSyncInterval(interval); // set the number of seconds between re-sync

functions and the example code here.