Skip to content

Commit 557a822

Browse files
committed
add getTimeStruct to handle local/utc time
1 parent 3f9b2e1 commit 557a822

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/ArduinoCellular.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "Watchdog.h"
66
#endif
77

8-
unsigned long ArduinoCellular::getTime() {
8+
Time ArduinoCellular::getTimeStruct(bool localTime) {
99
int year = 1970;
1010
int month = 1;
1111
int day = 1;
@@ -14,9 +14,17 @@ unsigned long ArduinoCellular::getTime() {
1414
int second = 0;
1515
float tz;
1616
if (syncNTPServer() == 0) {
17-
modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz);
17+
if (localTime) {
18+
modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz);
19+
} else {
20+
modem.getNetworkUTCTime(&year, &month, &day, &hour, &minute, &second, &tz);
21+
}
1822
}
19-
return Time(year, month, day, hour, minute, second).getUNIXTimestamp();
23+
return Time(year, month, day, hour, minute, second);
24+
}
25+
26+
unsigned long ArduinoCellular::getTime() {
27+
return getTimeStruct().getUNIXTimestamp();
2028
}
2129

2230
int ArduinoCellular::syncNTPServer(bool forceNTPSync) {
@@ -145,17 +153,8 @@ Time ArduinoCellular::getGPSTime(){
145153
}
146154

147155
Time ArduinoCellular::getCellularTime(){
148-
int year = 1970;
149-
int month = 1;
150-
int day = 1;
151-
int hour = 0;
152-
int minute = 0;
153-
int second = 0;
154-
float tz;
155-
if (syncNTPServer() == 0) {
156-
modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz);
157-
}
158-
return Time(year, month, day, hour, minute, second);
156+
// Get the current time from the network as localtime
157+
return getTimeStruct(true);
159158
}
160159

161160

src/ArduinoCellular.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ class ArduinoCellular {
303303

304304
static unsigned long getTime(); /** Callback for getting the current time as an unix timestamp. */
305305

306+
static Time getTimeStruct(bool localTime = false); /** Function for getting the current time as a Time object. */
307+
306308
static constexpr unsigned long waitForNetworkTimeout = 20000L; /**< Maximum wait time for network registration (In milliseconds). */
307309

308310
static int syncNTPServer(bool forceNTPSync = false); /** Function for synchronizing the NTP server. */

0 commit comments

Comments
 (0)