But I dont get a correct UNIX timestamp. The timestamp I get looks like that: -2678354.
The docs say the time should be synchronized after the handshake with the cloud. So why am I not getting a correct timestamp ?
I would be really happy if somebody can point me to my probleme here
I think the problem is that you’re trying to add an integer, Time.now(), to a String. I’m surprised that the compiler even lets you do this. You should use sprintf to create a string from the integer, rather than adding it to an empty String object.
Ok. Changed %d to %u. Thanks for the info on the behavior of += with a String object. I try to stay away from Strings, so I wasn’t aware of that. I guess that explains why the compiler let him do that.
I recall the time when people did employ their own wait loop to sync, but I thought that issue was cured a while ago.
What system version have you got?
The workaround was
while(Time.year() <= 1970) Particle.process();
You could also try explicitly calling Particle.syncTime(), which you should do from time to time anyway, to correct RTC drift.
I am running 0.6.1 and I am putting a time stamp in the event data (there is a store and send function to ensure no loss events when cloud connection is out). I have noticed recently that some events have a timestamp 01-12-1999 01:00. I am doing a Particle.syncTime(); once every 24 hours and thought that the onboard clock would just provide the correct time even if the cloud connection is lost. I am getting the time/date with Time.now() to populate the event date/time. Any ideas what is going on here?
@armor, Particle.syncTime() is asynchronous and may not sync the time as fast as you need it. As such, you may want to use the (new) Time.isValid() function to check if a valid time has been acquired from the cloud. The onboard RTC will run as long as power to your device is not interrupted.
Thanks - good shout - just had a look at that and I see that Time.now() calls Time.isValid() so I guess it must be returning a false value. That explains why this has just recently started occurring (since 0.6.1)!
Hello, does the system properly maintain real-time between the instant Particle.syncTime() is called, and the instant the time response is received from the cloud and the RTC is updated? Just trying to determine if calls to Time.now() between these two events could ever result in some invalid time (ie 2000-1-1).
I did, but it’s not clear this explicitly covers the case where the clock was already valid before the sync. Based on the previous comments, my impression of the Particle.syncTime() behavior is this:
Particle.syncTime() is called on a system whose RTC is already maintaining valid time.
Subsequent calls to Time.isValid() will indicate false, and Time.now() will return some invalid time.
Some finite time elapses (perhaps a while if network connectivity lost), time data is received from the server, and then…
Subsequent calls to Time.isValid() will indicate truee, and Time.now() will return a valid time
If this behavior is correct, it seems counter-intuitive that an application cannot get valid time data from the firmware during step 2, even thought the RTC would still be maintaining valid time from the last time it was updated (prior to step 1). Let me know if I am misinterpreting the behavior.