# Convert to time value

**URL:** <https://www.ruby-forum.com/t/convert-to-time-value/109542>\
**Category:** Ruby\
**Created:** [July 16, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/convert-to-time-value/109542 "2007-07-16T18:05:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [July 16, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/convert-to-time-value/109542/1 "2007-07-16T18:05:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [July 16, 2007, 6:17pm UTC](https://www.ruby-forum.com/t/convert-to-time-value/109542/2 "2007-07-16T18:17:40Z")

</div>

On 7/16/07, Andrea C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://blog.gnufied.org)

---

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [July 16, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/convert-to-time-value/109542/3 "2007-07-16T18:23:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [July 16, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/convert-to-time-value/109542/4 "2007-07-16T18:40:29Z")

</div>

> 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
