Convert to time value

Hi to all,

i’ve a timestamp value stored in a Postgres DB. I’ve to make the
difference between the time value obtained with Time.now function and
the time value that get from the DB. The code is above:

res = conn.query(“SELECT stato,last_access FROM usage WHERE peer=100;”)

time=Time.new
access_time=res[0][1]
time=time - access_time

When i try to run the script i get the following error:

in `-’: no implicit conversion to float from string (TypeError)

i’ve tried to make a casting like this

time=time.to_s - access_time.to_s

but i get another error

undefined method `-’ for “Mon Jul 16 18:02:17 +0200 2007”:String
(NoMethodError)

How can i cast the field???

thanks in advance!!

On 7/16/07, Andrea C. [email protected] wrote:

time=time - access_time

undefined method `-’ for “Mon Jul 16 18:02:17 +0200 2007”:String
(NoMethodError)

How can i cast the field???

thanks in advance!!

It would have helpful if you could have told us your table column data
type. But anyways, i guess Time.parse should help.


Let them talk of their oriental summer climes of everlasting
conservatories; give me the privilege of making my own summer with my
own coals.

http://blog.gnufied.org

Andrea C. schrieb:

time=time - access_time

undefined method `-’ for “Mon Jul 16 18:02:17 +0200 2007”:String
(NoMethodError)

How can i cast the field???

thanks in advance!!

Hi Andrea,
don’t know how res[0][1] looks like, but generally you need to parse the
string with for example ParseDate#parsedate and then make a Time from
the returned array…

require ‘parsedate’
access_time = Time.local(ParseDate.parsedate(res[0][1]))

Regards
Florian

It would have helpful if you could have told us your table column data
type. But anyways, i guess Time.parse should help.

thanks now it’s ok!!!
The table column data is a timestamp…

really thanks