Florian Aßmann
7/16/2007 4:22:00 PM
Andrea Campagna schrieb:
> 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!!
>
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