diff --git a/src/TinyGsmClientBG96.h b/src/TinyGsmClientBG96.h index 3a3d287..67cbe43 100644 --- a/src/TinyGsmClientBG96.h +++ b/src/TinyGsmClientBG96.h @@ -400,7 +400,66 @@ class TinyGsmBG96 : public TinyGsmModem, * Time functions */ protected: - // Can follow the standard CCLK function in the template + String getGSMDateTimeImpl(TinyGSMDateTimeFormat format) { + sendAT(GF("+QLTS=2")); + if (waitResponse(2000L, GF("+QLTS: \"")) != 1) { return ""; } + + String res; + + switch (format) { + case DATE_FULL: res = stream.readStringUntil('"'); break; + case DATE_TIME: + streamSkipUntil(','); + res = stream.readStringUntil('"'); + break; + case DATE_DATE: res = stream.readStringUntil(','); break; + } + waitResponse(); // Ends with OK + return res; + } + + // The BG96 returns UTC time instead of local time as other modules do in + // response to CCLK, so we're using QLTS where we can specifically request + // local time. + bool getNetworkTimeImpl(int* year, int* month, int* day, int* hour, + int* minute, int* second, float* timezone) { + sendAT(GF("+QLTS=2")); + if (waitResponse(2000L, GF("+QLTS: \"")) != 1) { return false; } + + int iyear = 0; + int imonth = 0; + int iday = 0; + int ihour = 0; + int imin = 0; + int isec = 0; + int itimezone = 0; + + // Date & Time + iyear = streamGetIntBefore('/'); + imonth = streamGetIntBefore('/'); + iday = streamGetIntBefore(','); + ihour = streamGetIntBefore(':'); + imin = streamGetIntBefore(':'); + isec = streamGetIntLength(2); + char tzSign = stream.read(); + itimezone = streamGetIntBefore(','); + if (tzSign == '-') { itimezone = itimezone * -1; } + streamSkipUntil('\n'); // DST flag + + // Set pointers + if (iyear < 2000) iyear += 2000; + if (year != NULL) *year = iyear; + if (month != NULL) *month = imonth; + if (day != NULL) *day = iday; + if (hour != NULL) *hour = ihour; + if (minute != NULL) *minute = imin; + if (second != NULL) *second = isec; + if (timezone != NULL) *timezone = static_cast(itimezone) / 4.0; + + // Final OK + waitResponse(); // Ends with OK + return true; + } /* * Battery functions diff --git a/src/TinyGsmTime.tpp b/src/TinyGsmTime.tpp index 2750884..fb58c7d 100644 --- a/src/TinyGsmTime.tpp +++ b/src/TinyGsmTime.tpp @@ -59,6 +59,7 @@ class TinyGsmTime { break; case DATE_DATE: res = thisModem().stream.readStringUntil(','); break; } + thisModem().waitResponse(); // Ends with OK return res; } diff --git a/tools/test_build/test_build.ino b/tools/test_build/test_build.ino index 08e6743..f6414cf 100644 --- a/tools/test_build/test_build.ino +++ b/tools/test_build/test_build.ino @@ -68,11 +68,22 @@ void loop() { // Test the GSM location functions #if defined(TINY_GSM_MODEM_HAS_GSM_LOCATION) modem.getGsmLocation(); + float glatitude = -9999; + float glongitude = -9999; + modem.getGsmLocation(&glatitude, &glongitude); #endif // Test the Network time function #if defined(TINY_GSM_MODEM_HAS_TIME) modem.getGSMDateTime(DATE_FULL); + int year3 = 0; + int month3 = 0; + int day3 = 0; + int hour3 = 0; + int min3 = 0; + int sec3 = 0; + float timezone = 0; + modem.getNetworkTime(&year3, &month3, &day3, &hour3, &min3, &sec3, &timezone); #endif // Test the GPS functions