Convert Ruby JSON Date to Objective C NSDate
I have a Ruby service which receives a JSON date like this: /Date(1311706800000-0500)/ I need to format it in a way so that it can be read into an Objective C NSDate.
I have tried to use:
seconds_since_epoch = datestring.scan(/[0-9]+/)[0].to_i
return Time.at(seconds_since_epoch)
bu开发者_StackOverflow社区t that returns Mon Apr 03 10:00:00 -0600 43533.
Whats the best way to convert the JSON into a readable format for NSDate? Thanks for the help.
Looks like the JSON has the time in microseconds. Ruby's Time.at needs seconds and optionally, as a second argument, microseconds.
str = "1311706800000-0500"
# ignore the -0500 (might be a timezone)
secs, microsecs = str.scan(/[0-9]+/)[0].to_i.divmod(1000)
p Time.at(secs, microsecs) #=> 2011-07-26 21:00:00 +0200 with my locale
精彩评论