Page 1 of 1

Simple little ruby code for getting epoch time

Posted: Mon Mar 18, 2013 7:41 pm
by Quentin
I need to send out unix time to a micro and it seems easy to get windows epoch time using the ruby command and then do basic maths to convert to unix time-- I am still getting used to the ruby module in flowstone- I wrote this little piece of code which outputs the time in seconds but it is not updating the time each time I trigger the module- What am I doing wrong? The module is connected by a single trigger in and a single int out.

def event(in_id,value) # Input Events
case in_id
when 0 # update value
time = (Time.now.to_f * 1000.0).to_i
output 0, time
end
end

Thanks
Quentin

Re: Simple little ruby code for getting epoch time

Posted: Mon Mar 18, 2013 8:15 pm
by MyCo
The unix time is just:
Time.now.to_i

There is no need to multiply that by 1000.

If you do need microseconds, you have to multiply by 1000, but then you have to take care, because FlowStones Integers are only 32bit, so you exceed the integer range.

Re: Simple little ruby code for getting epoch time

Posted: Mon Mar 18, 2013 8:20 pm
by Quentin
aha, I see... the value was too big for the int so I wasn't seeing it change- thanks so much for helping me on this one..
Quentin

Re: Simple little ruby code for getting epoch time

Posted: Mon Mar 18, 2013 8:35 pm
by trogluddite
Hi Quentin,

It would be a good idea to change your variable name too - the name 'time' is already used by the FlowStone API as a method for getting at the primitive's own internal clock - so it would depend how the interpreter resolves the name conflict which value you would get.