Skip to content

Commit 74e2b17

Browse files
committed
feat: add getAlarmEpoch method
Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent d5484bb commit 74e2b17

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ setTime KEYWORD2
3838

3939
getEpoch KEYWORD2
4040
getY2kEpoch KEYWORD2
41+
getAlarmEpoch KEYWORD2
4142
setEpoch KEYWORD2
4243
setY2kEpoch KEYWORD2
4344
setAlarmEpoch KEYWORD2

src/STM32RTC.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,60 @@ time_t STM32RTC::getY2kEpoch(void)
10771077
return (getEpoch() - EPOCH_TIME_OFF);
10781078
}
10791079

1080+
/**
1081+
* @brief get alarm epoch time
1082+
* @param name: ALARM_A or ALARM_B if exists
1083+
* @retval epoch time in seconds
1084+
*/
1085+
time_t STM32RTC::getAlarmEpoch(Alarm name)
1086+
{
1087+
return getAlarmEpoch(nullptr, name);
1088+
}
1089+
1090+
/**
1091+
* @brief get alarm epoch time
1092+
* @param subSeconds: optional pointer to where to store subseconds of the epoch in ms
1093+
* @param name: optional (default: ALARM_A)
1094+
* ALARM_A or ALARM_B if exists
1095+
* @retval epoch time in seconds
1096+
*/
1097+
time_t STM32RTC::getAlarmEpoch(uint32_t *subSeconds, Alarm name)
1098+
{
1099+
struct tm tm;
1100+
1101+
tm.tm_isdst = -1;
1102+
/*
1103+
* mktime ignores the values supplied by the caller in the
1104+
* tm_wday and tm_yday fields
1105+
*/
1106+
tm.tm_yday = 0;
1107+
tm.tm_wday = 0;
1108+
tm.tm_year = _year + EPOCH_TIME_YEAR_OFF;
1109+
tm.tm_mon = _month - 1;
1110+
syncAlarmTime(name);
1111+
#ifdef RTC_ALARM_B
1112+
if (name == ALARM_B) {
1113+
tm.tm_mday = _alarmBDay;
1114+
tm.tm_hour = _alarmBHours;
1115+
tm.tm_min = _alarmBMinutes;
1116+
tm.tm_sec = _alarmBSeconds;
1117+
if (subSeconds != nullptr) {
1118+
*subSeconds = _alarmBSubSeconds;
1119+
}
1120+
} else
1121+
#endif
1122+
{
1123+
tm.tm_mday = _alarmDay;
1124+
tm.tm_hour = _alarmHours;
1125+
tm.tm_min = _alarmMinutes;
1126+
tm.tm_sec = _alarmSeconds;
1127+
if (subSeconds != nullptr) {
1128+
*subSeconds = _alarmSubSeconds;
1129+
}
1130+
}
1131+
return mktime(&tm);
1132+
}
1133+
10801134
/**
10811135
* @brief set RTC alarm from epoch time
10821136
* @param epoch time in seconds

src/STM32RTC.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ class STM32RTC {
206206
time_t getY2kEpoch(void);
207207
void setEpoch(time_t ts, uint32_t subSeconds = 0);
208208
void setY2kEpoch(time_t ts);
209+
time_t getAlarmEpoch(Alarm name);
210+
time_t getAlarmEpoch(uint32_t *subSeconds = nullptr, Alarm name = ALARM_A);
209211
void setAlarmEpoch(time_t ts, Alarm_Match match, Alarm name);
210212
void setAlarmEpoch(time_t ts, Alarm_Match match = MATCH_DHHMMSS, uint32_t subSeconds = 0, Alarm name = ALARM_A);
211213

0 commit comments

Comments
 (0)