Skip to content

Commit 2a37d6c

Browse files
committed
Add flags to handle NTP sync
1 parent 4b7a446 commit 2a37d6c

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/ArduinoCellular.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,26 @@ Time ArduinoCellular::getGPSTime(){
126126
return Time(year, month, day, hour, minute, second);
127127
}
128128

129-
Time ArduinoCellular::getCellularTime(bool localTime){
129+
Time ArduinoCellular::getCellularTime(bool localTime, bool forceNTPSync) {
130130
int year = 1970;
131131
int month = 1;
132132
int day = 1;
133133
int hour = 0;
134134
int minute = 0;
135135
int second = 0;
136136
float tz;
137-
if (modem.NTPServerSync() == 0) {
138-
if (localTime) {
139-
modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz);
140-
} else {
141-
modem.getNetworkUTCTime(&year, &month, &day, &hour, &minute, &second, &tz);
142-
}
137+
138+
if ((needNTPSync || forceNTPSync) && (modem.NTPServerSync() != 0)) {
139+
return Time(year, month, day, hour, minute, second);
143140
}
141+
needNTPSync = false;
142+
143+
if (localTime) {
144+
modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz);
145+
} else {
146+
modem.getNetworkUTCTime(&year, &month, &day, &hour, &minute, &second, &tz);
147+
}
148+
144149
return Time(year, month, day, hour, minute, second);
145150
}
146151

src/ArduinoCellular.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class ArduinoCellular {
163163
* @brief Gets the current time from the network.
164164
* @return The current time.
165165
*/
166-
Time getCellularTime(bool localTime = true);
166+
Time getCellularTime(bool localTime = true, bool forceNTPSync = false);
167167

168168
/**
169169
* @brief Gets the current time from the GPS module.
@@ -277,7 +277,6 @@ class ArduinoCellular {
277277

278278
private:
279279
bool connectToGPRS(const char * apn, const char * gprsUser, const char * gprsPass);
280-
281280

282281
/**
283282
* @brief Waits for network registration. (Blocking call)
@@ -304,6 +303,8 @@ class ArduinoCellular {
304303
static unsigned long getTime(); /** Callback for getting the current time as an unix timestamp. */
305304

306305
static constexpr unsigned long waitForNetworkTimeout = 20000L; /**< Maximum wait time for network registration (In milliseconds). */
306+
307+
bool needNTPSync = true; /**< Flag to indicate if NTP synchronization is needed. */
307308
};
308309

309310

0 commit comments

Comments
 (0)