I try to parse a text representation of a time into a ruby Time object.
Normally in ruby I would do it like this:
require 'time'
Time.parse('2010-12-15T13:16:45Z')
# => 2010-12-15 13:16:45 UTC
In RubyMotion I am unable to require libs and Time.parse is not available:
(main)> require 'time'
=> #<RuntimeError: #require is not supported in RubyMotion>
(main)>
(main)> Time.parse
=> #<NoMethodError: undefined method `parse' for Time:Class>
Is there a way to require the time library provided by ruby without having to copy and rewrite the whole code to make it compatible with RubyMotion?
It's not as automatic, but you could use
NSDateFormatter
Maybe this has since been fixed. I'm parsing times in RubyMotion no problem.
RubyMotion 4.8
BubbleWrap adds some nice wrappers around this:
This will give you a NSDate instance, simple as that.
While Paul.s answer certainly works, it was giving me the creeps, looking at it in my code, so I kept looking.
Matt Aimonetti's MacRuby book has some good stuff:
http://books.google.ca/books?id=WPhdPzyU1R4C&pg=PA43&lpg=PA43&dq=macruby+nsdate&source=bl&ots=j7Y3J-oBcV&sig=FTr0KyKae-FinH-HNEWBcAAma1s&hl=en&sa=X&ei=ANT0T7mkEM6jqwHx7LjeAw&ved=0CGEQ6AEwBA#v=onepage&q=macruby%20nsdate&f=false
Where parsing is as simple as:
or
And if you absolutely have to have a Time object from that: