PaulStoffregen / Time

Time library for Arduino
http://playground.arduino.cc/code/time
1.25k stars 666 forks source link

If syncProvder is set but does not return time, subsequent calls to now() should try to sync. #15

Open karl-petter opened 9 years ago

karl-petter commented 9 years ago

I use a GSM modem to sync my time. When I initialise my program I call setSyncProvider to set my getTImeFunction. Though at that time the modem is not turned on neither has it connected to the mobile network and thus the sync attempt in now() will fail. But subsequent calls after I have taken the modem online will return a none synced time even if the sync service is available. Once syncInterval has passed now() will make a new sync attempt.

Instead I would like now() to try to sync everytime as long as the status is timeNotSet. The provided code takes care of this.

Also changed the indentation to spaces instead of tabs of my previous commit.

michaelmargolis commented 9 years ago

I do not want to interfere with the existing capability for the caller to recognize that the sync function has not set the time so an alternative sync source can be used if available. What are the disadvantages of adding the functionality you require in the sketch instead of in the library?

karl-petter commented 9 years ago

The main issue is that Time does not provide any forced way to sync. At least not obviously from the API point of view. But of course a new call to setSyncProvider() or change the setSyncInterval to 0 would achieve that. Though I think it would be a bit strange to first setup Time and then change sync interval until I have seen it has synced. What I want to achieve is that I as soon as possible get a correct time and not something like 1970-ish.

And my change does not remove the existing capability for the caller to recognise that sync has not set time. Status is still timeNotSet after now() was called and the caller can check status and decide to either call now() again to try to sync again(which existing code will not allow) or set an alternative sync source. But of course the alternative to achieve this same functionality is to call setSyncInterval(0) and then call now() and once status has changed to timeSet, call setSyncInterval(300).

I guess it depends on how one looks at syncInterval, is that the exact interval at which the getTimeFunction will be called or is it the amount a time that will pass at least until getTimeFunction is called.

wayfarerbasta commented 9 years ago

@karl-petter

Hi I have a similar problem. I'm not sure why, but sometimes the internal sync functionality of the library does not work at all. I implementet an external synchronisation based on the setTime function of the library and I use a global variable to set the sync status in my sycProvider function.

wayfarer

michaelmargolis commented 9 years ago

wayfarerbasta, is the problem that the time source is not available when the system starts (which I believe is the issue raised by karl-petter. Or is the sync functionality intermittently not working? If the latter, can you say more about how to reproduce this problem.

hberg32 commented 7 years ago

I find that I sometimes get timeouts on the first sync attempt after powering up an ESP8266 and connecting to Wifi. I'd like to suggest simply adding the following to Time.cpp (and TimeLib.h). The sketch can determine for itself if the time is not set and kick off another one if needed.

void syncNow() { nextSyncTime = 0; now(); }

scr34m88 commented 5 years ago

I know this thread is a long time ago, but I think this is what I need..

I've got a clock with a RTC and i want to sync it every hour. When I use setSyncProvider(RTC.get); // Zeit vom RTC bekommen setSyncInterval(60000); sometimes it dosent work an the wrong time is displayed and i have to reset the arduino.