[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

convert to time value

Andrea Campagna

7/16/2007 4:06:00 PM

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

--
Posted via http://www.ruby-....

3 Answers

hemant

7/16/2007 4:16:00 PM

0

On 7/16/07, Andrea Campagna <andreacamp@hotmail.it> wrote:
> 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.g...

Florian Aßmann

7/16/2007 4:22:00 PM

0

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


Andrea Campagna

7/16/2007 4:42:00 PM

0


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

--
Posted via http://www.ruby-....