convert to time value

A

Andrea Campagna

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!!
 
H

hemant

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!!

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
 
F

Florian Aßmann

Andrea said:
Hi to all,
=20
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:
=20
res =3D conn.query("SELECT stato,last_access FROM usage WHERE peer=3D10= 0;")
=20
time=3DTime.new
access_time=3Dres[0][1]
time=3Dtime - access_time
=20
When i try to run the script i get the following error:
=20
in `-': no implicit conversion to float from string (TypeError)
=20
i've tried to make a casting like this
=20
time=3Dtime.to_s - access_time.to_s
=20
but i get another error
=20
undefined method `-' for "Mon Jul 16 18:02:17 +0200 2007":String
(NoMethodError)
=20
How can i cast the field???
=20
thanks in advance!!
=20

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 =3D Time.local(ParseDate.parsedate(res[0][1]))

Regards
Florian
 
A

Andrea Campagna

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top