为什么Time.strptime()返回当前的日期? [英] Why is Time.strptime() returning the current date?

查看:277
本文介绍了为什么Time.strptime()返回当前的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

制作一个GET请求私人(没有公共文档)API返回的数据以JSON格式。

Making a GET request to a private (no public documentation) API returns data in JSON format.

该值Im感兴趣的是日期。它返回ASP.NET JSON日期格式。这里是什么样子:

The value im interested in is the date. It returns the date in ASP.NET JSON Date format. Here's what it looks like:

AanmeldDatum: "/Date(1406675114000+0200)/"

还有一个叫AangebodenSindsTekst变量,这意味着OfferedSinceText,它的值是8奥古斯2014年。所以,未知日期格式应该得到解析为特定的值。

There's another variable called AangebodenSindsTekst which means OfferedSinceText and it's value is "8 augustus 2014". So the unknown Date format should get parsed into that specific value.

我试过这样:

require 'time'

foo = Time.strptime('1406675114000+0200', '%N') # => 2014-08-12 13:38:46 +0200
foo = Time.strptime('1406675114000+0200', '%N%z') # => 2014-08-12 14:38:58 +0200

但它只是返回当前时间。

But it simply returns the current time.

我知道100%地肯定 /日期(1406675114000 + 0200)/ 解析的时候,应该返回日期 2014年7月30日

I know 100% sure that /Date(1406675114000+0200)/ when parsed, should return the date 2014-07-30.

问题是我如何确保它呢?

The question is how do I make sure it does?

推荐答案

使用'%N''%N%Z似乎触发中的错误 strptime ,造成了目前的日期/时间:

Using '%N' or '%N%z' seems to trigger a bug in strptime, resulting in the current date/time:

Time.strptime('1262300400000+0100', '%N') # => 2014-08-14 16:30:01 -0700
Time.strptime('1262300400000+0100', '%N%z') # => 2014-08-14 08:30:01 -0700

使用'%Q%Z解析为:

Time.strptime('1262300400000+0100', '%Q%z') # => 2010-01-01 00:00:00 +0100

Time.strptime 文档没有提到'%Q ,而 DateTime.strptime 文档一样。

The Time.strptime documentation doesn't mention '%Q', but the DateTime.strptime doc does.

如果您已经解析日期字符串,然后格式化很容易:

If you have parsed the date string, then formatting it is easy:

foo = Time.strptime('1262300400000+0100', '%Q%z') # => 2010-01-01 00:00:00 +0100
foo.strftime('%Y-%m-%d %H:%M:%S %z') # => "2010-01-01 00:00:00 +0100"

请参阅的strftime 文档有关格式选项的更多信息。

See the strftime documentation for more information about your formatting options.

请注意,有像'%Y-%M-%D''%H一些快捷键的事情:%M:% S'

foo.strftime('%F %T %z') # => "2010-01-01 00:00:00 +0100"

真正的问题是.NET是如何在序列化JSON的日期。翻遍了谷歌的显示了一些谈论这个网页:

The real problem is how .NET is serializing the date in JSON. A search of the googles shows a number of pages talking about this:

.NET不是一个人在这样做。 Ruby的JSON和YAML串行偶尔会做类似的事情,而解决方法是看JSON被输出,然后提供 to_json to_s to_h 的处理程序,这将导致最重presents数据JSON输出。 JSON应该是JavaScript和后端系统之间的转让,而.NET不提供这种能力的唯一语言。 Perl中,红宝石,Python和谁知道其他语言的多少可以分析并发出JSON,他们都需要通过相同的规则玩。

.NET isn't alone in doing this. Ruby's JSON and YAML serializers will occasionally do similar things, and the fix is to look at the JSON being output, and then provide to_json or to_s or to_h handlers that will result in JSON output that best represents the data. JSON is supposed to be transferable between JavaScript and backend systems, and .NET isn't the only language offering that capability. Perl, Ruby, Python, and who knows how many other languages can parse and emit JSON, and they all need to play by the same rules.

这篇关于为什么Time.strptime()返回当前的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆