Simple little ruby code for getting epoch time

For general discussion related FlowStone
Post Reply
Quentin
Posts: 37
Joined: Sat Jul 14, 2012 5:35 am

Simple little ruby code for getting epoch time

Post 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
User avatar
MyCo
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany
Contact:

Re: Simple little ruby code for getting epoch time

Post 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.
Quentin
Posts: 37
Joined: Sat Jul 14, 2012 5:35 am

Re: Simple little ruby code for getting epoch time

Post 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
User avatar
trogluddite
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: Simple little ruby code for getting epoch time

Post 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.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
Post Reply