Thursday, 15 March 2012

Time object to String Object to XML and Back Again in Ruby -



Time object to String Object to XML and Back Again in Ruby -

i storing time objects in xml strings. having problem figuring out best way reinitialize them back. strings time objects in order perform subtraction on them.

here how stored in xml

<time> <category> batin </category> <in>2014-10-29 18:20:47 -0400</in> <out>2014-10-29 18:20:55 -0400</out> </time>

using

t = time.now

i accessing them xml

doc = nokogiri::xml(file.open("time.xml")) nodes = doc.xpath("//time").each |node| temp = timeclock.new temp.category = node.xpath('category').inner_text temp.in = node.xpath('in').inner_text. temp.out = node.xpath('out').inner_text. @times << temp end

what best way reconvert them time objects? not see method of time object this. found possible convert date object. seems give me format of mm/dd/yyyy partly want.

in need able subtract

<out>2014-10-29 18:20:55 -0400</out>

from

<in>2014-10-29 18:20:47 -0400</in>

the xml @ point stored based on dates need exact time "hh/mm/ss" perform calculations.

any sugguestions?

the time stdlib extends class parsing/conversion methods.

require 'time' time.parse('2014-10-29 18:20:47 -0400')

ruby xml time

No comments:

Post a Comment