diff --git a/src/ArduinoCellular.cpp b/src/ArduinoCellular.cpp index a4f0d2b..4b34eab 100644 --- a/src/ArduinoCellular.cpp +++ b/src/ArduinoCellular.cpp @@ -5,11 +5,38 @@ #include "Watchdog.h" #endif -unsigned long ArduinoCellular::getTime() { - int year, month, day, hour, minute, second; +Time ArduinoCellular::getTimeStruct(bool localTime) { + int year = 1970; + int month = 1; + int day = 1; + int hour = 0; + int minute = 0; + int second = 0; float tz; - modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz); - return Time(year, month, day, hour, minute, second).getUNIXTimestamp(); + if (syncNTPServer() == 0) { + if (localTime) { + modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz); + } else { + modem.getNetworkUTCTime(&year, &month, &day, &hour, &minute, &second, &tz); + } + } + return Time(year, month, day, hour, minute, second); +} + +unsigned long ArduinoCellular::getTime() { + return getTimeStruct().getUNIXTimestamp(); +} + +int ArduinoCellular::syncNTPServer(bool forceNTPSync) { + static bool needNTPSync = true; + if(!needNTPSync && !forceNTPSync) { + return 0; + } + if(modem.NTPServerSync() == 0) { + needNTPSync = false; + return 0; + } + return -1; } ArduinoCellular::ArduinoCellular() { @@ -126,20 +153,15 @@ Time ArduinoCellular::getGPSTime(){ return Time(year, month, day, hour, minute, second); } -Time ArduinoCellular::getCellularTime(){ - int year = 1970; - int month = 1; - int day = 1; - int hour = 0; - int minute = 0; - int second = 0; - float tz; - if (modem.NTPServerSync() == 0) { - modem.getNetworkTime(&year, &month, &day, &hour, &minute, &second, &tz); - } - return Time(year, month, day, hour, minute, second); +Time ArduinoCellular::getCellularTime(bool localTime){ + // Get the current time from the network as localtime + return getTimeStruct(localTime); } +bool ArduinoCellular::syncCellularTime(){ + // Sync the time with the network NTP service + return syncNTPServer(true) == 0 ? true : false; +} void ArduinoCellular::sendSMS(String number, String message){ modem.sendAT("+CMGF=1"); diff --git a/src/ArduinoCellular.h b/src/ArduinoCellular.h index 8f32de5..31923be 100644 --- a/src/ArduinoCellular.h +++ b/src/ArduinoCellular.h @@ -158,12 +158,19 @@ class ArduinoCellular { * @return The GPS location. If the location is not retrieved, the latitude and longitude will be 0.0. */ Geolocation getGPSLocation(unsigned long timeout = 60000); - + /** * @brief Gets the current time from the network. + * @param localTime If true, the time will be converted to local time. Default is true. * @return The current time. */ - Time getCellularTime(); + Time getCellularTime(bool localTime = true); + + /** + * @brief Sync the modem time using NTP service. + * @return True on success false otherwise. + */ + bool syncCellularTime(); /** * @brief Gets the current time from the GPS module. @@ -303,7 +310,11 @@ class ArduinoCellular { static unsigned long getTime(); /** Callback for getting the current time as an unix timestamp. */ + static Time getTimeStruct(bool localTime = false); /** Function for getting the current time as a Time object. */ + static constexpr unsigned long waitForNetworkTimeout = 20000L; /**< Maximum wait time for network registration (In milliseconds). */ + + static int syncNTPServer(bool forceNTPSync = false); /** Function for synchronizing the NTP server. */ };