C#将JSON日期序列化为Ruby

问题描述:

我有一个C#应用程序,该应用程序将其DTO序列化为JSON并将其发送给Ruby,以便将其处理.现在序列化日期的格式如下:

I have a C# application that serializes its DTOs to JSON and sends them accros the wire to be processed by Ruby. Now the format of the serialized date is like so:

/Date(1250170550493+0100)/

当它到达Ruby应用程序时,我需要将此字符串表示形式转换回日期/日期时间/时间(无论在Ruby中是什么).有什么想法我会怎么做吗?

When this hits the Ruby app I need to cast this string representation back to a date/datetime/time (whatever it is in Ruby). Any ideas how I would go about this?

干杯,克里斯.

您可以解析自该时期以来的毫秒数,例如:

You could parse out the milliseconds since the epoch, something like:

def parse_date(datestring)
  seconds_since_epoch = datestring.scan(/[0-9]+/)[0].to_i / 1000.0
  return Time.at(seconds_since_epoch)
end

parse_date('/Date(1250170550493+0100)/')

您仍然需要处理时区信息(+0100部分),所以这是一个起点.

You'd still need to handle the timezone info (the +0100 part), so this is a starting point.