Time comparison


I’m working on a project where I have a set of timers controlling different outputs. Every output can be set to trigger on several times during the day. Also I want to create an app that sends this information.

Normally these apps want to unix format the timestamps, and send these in a JSON string. But i’m doing a hell of a lot of work trying to split it op and put it into new string that I can compare with my timer values.

Does the imp/squirrel have some neat time comparison/interpretation like e.g. .NET C# where you have datatypes like DateTime and TimeSpan, that you can just compare using normal operators? Or anyone found some cool workaround working with time on the imp?

This is much needed in case of easily determine how much time my timer has left, if the imp disconnect and go online again. Right now I’m saving information about if the timer is running or not in the server.save().

Hope it makes sense, and someone is able to help :slight_smile:

Best, bobby

On the agent you can call jsondecode() which does all the hard work of parsing that out; it sounds like you are using textual date/time whereas you should really just use unixtimes (seconds since 1-Jan-1970 00:00 UTC).

Then, you can send the table down to the imp and use time() and the unixtimes from the table to turn things on & off (and how long you need to sleep for). You can just subtract unixtime values to determine the number of seconds between them.

Thanks a lot Hugo.

Does anyone have a nice class for converting unix sec timestamps to a table like the one time() returns? :slight_smile:

If i’m going to startup a timer again after shutdown, i need to find out if i should reactivate it right away if it was running, or just assign it again. To do that i imagine i need to find out if the time right now is between the daily routined start and stop time, thus comparing weekday, hours and minutes.

Also i need to teach my timer that if it turns on at 18:00 and off at 00.10, it will have a turn off weekday +1 to the turn on date, which i probably need to take into consideration when calculating if i should reactivate or assign again.

Any ideas how to set this up?

Wow, I just realized by trying that passing the unix timestring as a parameter do date(), returns all the values :smiley:
Still could be nice with some algorithm suggestions for the timer check on unexpected shutdown :slight_smile: