# Turning on Imp during set time periods

Hi All,

I’m trying to set the imp up so a user could set it to run during a specified time interval throughout the day(e.g. set an LED to on between 7:00PM and 8:00PM).

It would seem that the best way to accomplish this would be to compare the user specified start and end time to the integer the time() function spits out. However, I don’t see an easy way to get the user specified start and end time in time()'s integer format. What is the simplest way to do this? I’m afraid I may be missing something.

Try Squirrel’s date() function instead. This returns a table with keys for seconds, minutes, hour, day, month and year.

This is perhaps a naive question but I was more so wondering if there was a function that turns an arbitrary date into the integer format that time() spits out?

Currently, I’m just calculating the seconds that have elapsed during the day and using those. e.g. I calculate an integer representing my start and end times using the hours, minutes, and seconds value specified by the user. Then I just compare those two values to an integer calculated using the current values for hours, minutes, and seconds.

I was just curious if there was a better way someone more experienced would do it.

The date() object has a time property that returns the unix timestamp for that date.

`server.log(date().time);`

So for example:
`
local start = date();

start.day=04;
start.hour = 13;
start.min = 30;
start.sec = 0;
`
After instantiating that date, start.time should produce the integer corresponding to that time in the future?

As I’m attempting to use it now, it only ever returns the integer corresponding to the current time.

That may be a bug - I’m not 100% sure what the expected behaviour there would be… @peter?

I would assume start.time is generated when you do start = date(), so changing the value of start.hour will not affect the time property. The only way around this I can see is to use time() to generate an integer representation of the current time, modify this to your needs then put the value into date():
`local now = time() local then = now + (24 * 60 * 60) // 24 hours from now local targetTime = date(then)`

and used targetTime.day, targetTime.hour etc.

You might even write a function to compare an arbitrary date to date(), multiply out the values and add to time(), and then used date(time) to convert back into the preferred format.

Squirrel’s date() function works like ISO C “gmtime()”: it turns an integer Unix time_t (seconds since 1970) into a structure containing “broken-down time”. Unfortunately Squirrel appears to lack the inverse function, ISO C’s “mktime()”, which computes the time_t value corresponding to a structure.

For your purposes, though, kamongear, you don’t really need mktime() in order to deal with repeating, daily events properly. All you need to do is convert “7pm in your timezone” to seconds since midnight GMT, and then consult “time() % (246060)” to determine the current seconds since the most recent midnight GMT.

Peter