How have you set the time on the rtc?
The code you posted only sets the time if the rtc is not running. Yours is running.
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(2017, 10, 3, 17, 40, 0));
// 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));
}
Move the rtc.adjust() statement outside of the if() block, run the code once to set the time, then comment out tjhe rtc.adjust() line so that it won't reset the time every time the code restarts.