Is there a more elegant way to set time on internal RTC?

Although you are discouraging me making use of the RTC (RTC and Battery Power), at least I want to copy the compile/upload time TIME (underscores deleted) to the RTC clock. (The tutorials do it manually:

RTCTime startTime(30, Month::JUNE, 2023, 13, 37, 00, DayOfWeek::WEDNESDAY, SaveLight::SAVING_TIME_ACTIVE);

I tried it this way, but look for a more elegant way to do it.

  // a lot of work to extract HMS from __TIME__:
  // hour
  char time[] = __TIME__;
  char temp[8];
  strncpy(temp, time, 2);
  int h = atoi(temp);
  // minute:
  memset(time, ' ', 3);
  strncpy(temp, time, 5);
  int m = atoi(temp);
  // second:
  memset(time,' ',6);
  int s = atoi(time);  
  // RTC:
  RTC.begin();
  RTCTime startTime(1, Month::JANUARY, 13, h, m, s, DayOfWeek::MONDAY, SaveLight::SAVING_TIME_ACTIVE);
  RTC.setTime(startTime);

Any suggesions?

Hello,

Why not using DATE also ?

char time[] = __TIME__ , date[] = __DATE__ , temp[11]="          ";

  Serial.println(date);
  strncpy(temp, date, 3); Serial.println(temp);
  
  memset(date, ' ', 4);
  strncpy(temp, date, 6); byte day = atoi(temp); // Serial.println(day);
  memset(date, ' ', 6);   int year = atoi(date); // Serial.println(year);

But how to convert "Aug" into Month:: AUGUST ?
I am not familiar with the Month::AUGUST syntax, please help !

Have a nice day,

1 Like

Yes, you are right. It is a pity that the month is given by three letters instead of a number in the DATE string. My solution was to add the ASCII codes of that letters and use a lookup table. I was lucky, as some sums are very close to each other. The startTime record wants strong types for Month, DayOfWeek, and SaveLight, so I used type-casting. This is my result:

#include <RTC.h>

void setup() {
  Serial.begin(9600);
  while (!Serial)
    ;
  Serial.println(__FILE__);
  Serial.println(__DATE__ " - " __TIME__);
  char date[] = __DATE__, time[] = __TIME__;
  //*
  int day, month, year, hour, minute, second;
  // time:
  Serial.println(time);
  hour = a2i(time, 0, 2);
  Serial.print("Stunde ");
  Serial.println(time);
  minute = a2i(time, 3, 5);
  Serial.print("Minute ");
  Serial.println(time);
  second = a2i(time, 6, 8);
  Serial.print("Sekunde ");
  Serial.println(time);
  // date:
  month = m2i(date, 0, 3);
  day = a2i(date, 4, 6);
  Serial.print("Tag ");
  Serial.println(date);
  year = a2i(date, 7, 11);
  Serial.print("Jahr ");
  Serial.println(date);
  // RTC:
  RTC.begin();
  RTCTime startTime(day, (Month) month, year, hour, minute, second, (DayOfWeek) 1, (SaveLight) 1);
  RTC.setTime(startTime);
}

int m2i(char *c, int from, int to) {
  int mm[] = { 281, 269, 288, 291, 295, 301, 299, 285, 296, 294, 307, 268 };
  int cc = c[0] + c[1] + c[2]; // add 3 characters
  for (int i = 0; i < 12; i++) if (mm[i] == cc) return i + 1;
  return 0;
}

int a2i(char *c, int from, int to) {
  memset(c, ' ', from);  // setzt die ersten from bytes auf ' '
  char temp[12];
  strncpy(temp, c, to);  // kopiert die ersten to byte von c nach temp
  return atoi(temp);
}

void loop() {
  RTCTime currentTime;
  RTC.getTime(currentTime);  // Get current time from RTC
  Serial.print("date time = ");
  Serial.print(currentTime.getDayOfMonth());
  Serial.print(".");
  int m = (int)currentTime.getMonth();
  Serial.print(m);
  Serial.print(".");
  Serial.print(currentTime.getYear());
  Serial.print("  ");
  Serial.print(currentTime.getHour());
  Serial.print(":");
  Serial.print(currentTime.getMinutes());
  Serial.print(":");
  Serial.println(currentTime.getSeconds());
  delay(10000);
}

But the main problem is still unsolved: how does the Minima detect a power-cut and restarts with the time being set at the time of uploading?
No idea.