entering date/time over the serial monitor?

Does someone know a library able to enter date/time over the serial monitor?
It should decode the serial input and translate it into the Epoch to synchronize the time library.
If I could avoid to reinvent the wheel...
If you know of a ready made solution,I would be very grateful to get the information.
Regards.
Laszlo

I believe the latest Time library I believe will do the conversion, you just have to read the input string and format the data first. A lot of this appears when people are using NTP (Network Time Protocol).

Check some RTC library examples. You don’t have to use the libraries, but you can maybe use the time setting examples. I made myself one, but it’s too crude to post here. It accepts a YYMMDDHHMMSS input string like:

t 21 02 01 03 11 00

I used the DateTime library functions for that. But later on, I got lazy and just input Epoch time for manual setting…

gilshultz:
I believe the latest Time library I believe will do the conversion,

I have found something:
in loop()

if (Year < 2020)
  {
    if (Serial.available() > 0)
    {
      // read in the user input
      Day = Serial.parseInt();
      Month = Serial.parseInt();
      Year = Serial.parseInt();
      Hour = Serial.parseInt();
      Minute = Serial.parseInt();
      Second = Serial.parseInt();
      Console1.printf("I have understood %u/%u/%u %u:%u:%u\n", Day, Month, Year, Hour, Minute, Second);
      boolean validDate = (inRange(Day, 1, 31) && inRange(Month, 1, 12) && inRange(Year, 2021, 2031));
      boolean validTime = (inRange(Hour, 0, 23) && inRange(Minute, 0, 59) && inRange(Second, 0, 59));
      if (validTime && validDate)
      {
       setTime(Hour,Minute,Second,Day,Month,Year);
       sprintf(charbuff, "Now is %02d:%02d:%02d. The Epoch is: %10lu\r\nDate is %s, %02d %s %04d", Hour,   Minute, Second, Epoch, DayName, Day, MonthName, Year);
        Console3.println(charbuff);
      }
    }
  }

My current problem is that I compile for the ESP8266 and

setTime(Hour,Minute,Second,Day,Month,Year);

is not understood by the ESP time library.

ESP should have makeTime() and breakTime() functions

aarg:
ESP should have makeTime() and breakTime() functions

Agreed, the built-in Time library should. The function is even highlighted
setTime(Hour,Minute,Second,Day,Month,Year);

But the compiler issues:

exit status 1
'setTime' was not declared in this scope

I have grouped some pieces of code and found an almost working solution:

if (Year < 2020)

  {
Serial.println("I need the date(dd/mm/yyyy) and time(hh:mm:ss)");
    if (Serial.available() > 0)
    {
      // read in the user input
      Day = Serial.parseInt();
      Month = Serial.parseInt();
      Year = Serial.parseInt();
      Hour = Serial.parseInt();
      Minute = Serial.parseInt();
      Second = Serial.parseInt();
      Console1.printf("I have understood %u/%u/%u %u:%u:%u\n", Day, Month, Year, Hour, Minute, Second);
      boolean validDate = (inRange(Day, 1, 31) && inRange(Month, 1, 12) && inRange(Year, 2021, 2031));
      boolean validTime = (inRange(Hour, 0, 23) && inRange(Minute, 0, 59) && inRange(Second, 0, 59));
      if (validTime && validDate)
      {
        //setTime(Hour,Minute,Second,Day,Month,Year);
        struct tm t;
          time_t t_of_day;
          t.tm_year = Year-1900;  // Year - 1900
          t.tm_mon = Month-1;       // Month, where 0 = jan
          t.tm_mday = Day-1;        // Day of the month
          t.tm_hour = Hour;
          t.tm_min = Minute;
          t.tm_sec = Second;
          t.tm_isdst = -1;         // Is DST on? 1 = yes, 0 = no, -1 = unknown
        t_of_day = mktime(&t);
        sprintf(charbuff, "Now is %02d:%02d:%02d. The Epoch is: %10lu\r\nDate is %s, %02d %s %04d", Hour, Minute, Second, t_of_day, DayName, Day, MonthName, Year);
        Console3.println(charbuff);
        struct timeval tv;
          tv.tv_sec = t_of_day;  // epoch time (seconds)
          tv.tv_usec = 0;    // microseconds
         settimeofday(&tv,0);
      }
    }
  }

That almost works:…
running offline, back in the 70’s
RSSI: 31 dBm
Now is 08:00:18. The Epoch is: 28818
Date is Thursday, 01 January 1970
I need the date(dd/mm/yyyy) and time(hh:mm:ss)
I have understood 4/2/2021 20:46:0
Now is 20:46:00. The Epoch is: 1612385160
Date is Thursday, 04 January 2021

but then the system time is:

Thu Feb 4 04:46:23 2021
How to get the right hour? My timezone is GMT+1

And else: couldn’t it be easier?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.