Skip to content

Instantly share code, notes, and snippets.

@fjfish
Created December 11, 2011 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjfish/1461638 to your computer and use it in GitHub Desktop.
Save fjfish/1461638 to your computer and use it in GitHub Desktop.
Converting a JSON string date into a Ruby Time object
# Convert string of the form 2011-12-31T23:59:59+00:00 to a Ruby Time object
# I'm sure there's an easier way to do this.
def self.json_to_ruby_time(the_string)
string_elements = the_string.split /[-T:+]+/
string_elements.pop
Time.new *(string_elements.map(&:to_i))
end
@caius
Copy link

caius commented Dec 11, 2011

irb(main):001:0> require "time"
=> false
irb(main):002:0> Time.parse("2011-12-31T23:59:59+00:00")
=> 2011-12-31 23:59:59 +0000

@tombell
Copy link

tombell commented Dec 11, 2011

Too many braces @caius.

@paulingham
Copy link

Heh, just gave him the same response on Facebook @cauis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment