Skip to content

Commit b0e991a

Browse files
committed
Add toString function to RTCTime
Former-commit-id: e5b59a5
1 parent bd8e9c3 commit b0e991a

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

libraries/RTC/src/RTC.h

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <ctime>
55
#include "r_rtc_api.h"
6+
#include <api/String.h>
67

78
struct timeval {
89
time_t tv_sec;
@@ -107,6 +108,12 @@ class RTCTime {
107108
time_t getUnixTime();
108109
struct tm getTmTime();
109110

111+
/**
112+
* @brief Returns the ISO 8601 string representation of the date and time.
113+
*
114+
* @return String The date and time in the format YYYY-MM-DDTHH:MM:SS.
115+
*/
116+
arduino::String toString() const;
110117

111118
enum class Period {
112119
ONCE_EVERY_2_SEC,

libraries/RTC/src/RTClock.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,48 @@ DayOfWeek RTCTime::getDayOfWeek() const { return day_of_week; }
361361
time_t RTCTime::getUnixTime() { return mktime ( (struct tm *)&stime ); }
362362
struct tm RTCTime::getTmTime() { return (struct tm)stime; }
363363

364+
arduino::String RTCTime::toString() const {
365+
String formattedTime = "";
366+
367+
// Year
368+
formattedTime += String(getYear());
369+
formattedTime += "-";
370+
371+
// Month
372+
uint8_t month = static_cast<uint8_t>(getMonth()) + 1;
373+
if (month < 10)
374+
formattedTime += '0';
375+
formattedTime += String(month);
376+
formattedTime += "-";
377+
378+
// Day of month
379+
if (getDayOfMonth() < 10)
380+
formattedTime += '0';
381+
formattedTime += String(getDayOfMonth());
382+
383+
// T separator
384+
formattedTime += "T";
385+
386+
// Hours
387+
if (getHour() < 10)
388+
formattedTime += '0';
389+
formattedTime += String(getHour());
390+
formattedTime += ":";
391+
392+
// Minutes
393+
if (getMinutes() < 10)
394+
formattedTime += '0';
395+
formattedTime += String(getMinutes());
396+
formattedTime += ":";
397+
398+
// Seconds
399+
if (getSeconds() < 10)
400+
formattedTime += '0';
401+
formattedTime += String(getSeconds());
402+
403+
return formattedTime;
404+
}
405+
364406
/* -------------------------------------------------------------------------- */
365407
/* RTClass */
366408
/* -------------------------------------------------------------------------- */

0 commit comments

Comments
 (0)