Code (as function/method)
#include <EEPROM.h> // EEPROM Address Definitions #define adrBuildYear 0 #define adrBuildMonth 1 #define adrBuildDay 2 #define adrBuildHour 3 #define adrBuildMinute 4 #define adrBuildSecond 5 #define adrBuildCounter 6 #define adrRunCounterHighByte 7 #define adrRunCounterLowByte 8 String ArduinoDateTimeToDisplayAndEEPROM() { char s_month[5]; int month, day, year; int hour, minute, second; static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; sscanf(__DATE__/*time*/, "%s %d %d", s_month, &day, &year); sscanf(__TIME__, "%d:%d:%d", &hour, &minute, &second); month = ((strstr(month_names, s_month) - month_names) / 3) + 1; String sMonth = month < 10 ? "0" + String(month) : String(month); String sDay = day < 10 ? "0" + String(day) : String(day); // Icrease the build counter (if different from the previous time) if ((EEPROM.read(adrBuildYear) != (byte)(year%100)) || (EEPROM.read(adrBuildMonth) != (byte)month) || (EEPROM.read(adrBuildDay) != (byte)day) || (EEPROM.read(adrBuildHour) != (byte)hour) || (EEPROM.read(adrBuildMinute) != (byte)minute) || (EEPROM.read(adrBuildSecond) != (byte)second)) { // Write to EEPROM (Uno/Nano: 1kb, Mega: 4kb) EEPROM.update(adrBuildYear, (byte)(year%100)); EEPROM.update(adrBuildMonth, (byte)month); EEPROM.update(adrBuildDay, (byte)day); EEPROM.update(adrBuildHour, (byte)hour); EEPROM.update(adrBuildMinute, (byte)minute); EEPROM.update(adrBuildSecond, (byte)second); EEPROM.update(adrBuildCounter, (byte)(EEPROM.read(adrBuildCounter) + 1)); } // Icrease the run counter by 1 and update (rewrite back) int rCnt = 256 * EEPROM.read(adrRunCounterHighByte) + EEPROM.read(adrRunCounterLowByte) + 1; EEPROM.update(adrRunCounterHighByte, (byte)(rCnt/256)); EEPROM.update(adrRunCounterLowByte, (byte)(rCnt%256)); return "b" + String(EEPROM.read(adrBuildCounter)) + " d" + String(year) + "." + sMonth + "." + sDay + " t" + __TIME__ + " r" + String(rCnt); }
Sample Usage
void setup() { Serial.println(ArduinoDateTimeToDisplayAndEEPROM()); // OUTPUT: (Something like) // b1 d2021.01.18 t12:00:00 r2 // b: Build, d: Date, t: Time, r: Runtime counter } void loop() { }
Link / Source