arduino-libraries / MKRGSM

GNU Lesser General Public License v2.1
55 stars 51 forks source link

return local time and UTC #33

Closed alanoatwork closed 6 years ago

alanoatwork commented 6 years ago

The current implementation of getTime returns UTC time. Many applications prefer local time and other arduino libraries that could be used to translate like Time.h, TimeLib.h and Timezone.h aren't compatible with MKRGSM due to the use of time.h

How about overloading getTime() with a flag to indicate localtime, (getTime(boolean, bLocaltime), or adding a new function like getLocalTime()? This simple change let's the network manage the struggle of converting timezones.

FrancMunoz commented 6 years ago

Hi! For me gsm::getTime() returns the right value. I looked at the code and it seems right to me: the returned time is corrected by timezone as it's shown in manual. I guess your problem is about AT+CTZU=1 command send in gsm:ready() through gsm:begin().

From manual:

AT+CTZU Configures the automatic time zone update via NITZ. The Time Zone information is provided after the network registration (if the network supports the time zone information).

So I guess your provider doesn't support timezone or doesn't provide your time zone so your time is wrong. I think there's an AT command to manually override timezone...

FrancMunoz commented 6 years ago

@alanoatwork , I made a mistake reading values and you're right: the returned value of GSM::getTime() seems to be UTC time. I'm using this but didn't noticed that has this deviation. I made this function modifying the original function and now I get local time.

unsigned long GSM::getLocalTime()
{
 String response;

 MODEM.send("AT+CCLK?");
 if (MODEM.waitForResponse(100, &response) != 1) {
   return 0;
 }

 struct tm now;

if (strptime(response.c_str(), "+CCLK: \"%y/%m/%d,%H:%M:%S", &now) != NULL) {
   time_t result = mktime(&now);
   return result;
 }

 return 0;
}

Could you try and confirm it to me, please?

Also about time.h, I solved it just renaming Time.h and Time.cpp in libraries\time to TimeW.h and TimeW.cpp and then included #include in my sketch.

alanoatwork commented 6 years ago

Yes, getLocalTime works fine. Will this change make it into the next release? Thanks for the tip regarding timew. I hate to mess with the libraries since it'll break my code on the next library release. Now that I can get localtime I don't need to use timezone.h, etc. Thanks for looking into this!

FrancMunoz commented 6 years ago

It would be great... since I use it too. What do you think @Rocketct , could I prepare a PR with this?

sandeepmistry commented 6 years ago

@FrancMunoz sure, a PR would be great.

FrancMunoz commented 6 years ago

@sandeepmistry done! take a look at #47 ...

sandeepmistry commented 6 years ago

Closed via #47.