java可识别的规范时区
So you have made the decision to use timezone-aware dates and now you are building your cool REST API using Tastypie. Of course timezones are important to your application, so you want to expose them when Tastypie exposes dates in the API.
因此,您已经决定使用可识别 时区的日期 ,现在您正在使用Deliciouspie构建出色的 REST API。 当然,时区对您的应用程序很重要,因此您希望在Tastypie在API中公开日期时公开时区。
You have a very simple resource that exposes a Django model that has an attribute, for example:
您有一个非常简单的资源来公开具有属性的Django模型,例如:
Out of the box, you notice that the dates displayed by Tastypie are converted to naive format, no matter if your USE_TZ
variable is set to True
in Django settings!
开箱即用,您注意到无论在Django设置中USE_TZ
变量是否设置为True
,Tastypie所显示的日期都将转换为幼稚格式!
Searching the Internet you find that there is a Tastypie setting called TASTYPIE_DATETIME_FORMATTING
. This might fix it…
在Internet上搜索,您发现有一个称为TASTYPIE_DATETIME_FORMATTING
的Deliciouspie设置。 这可能会解决...
Before, our timestamps were formatted like
之前,我们的时间戳格式如下
2013-02-28T16:42:55.08
-
2013-02-28T16:42:55.08
If we set TASTYPIE_DATETIME_FORMATTING
to 'rfc-2822'
(it defaults to ISO–8601), our timestamps are now displayed like this
如果我们将TASTYPIE_DATETIME_FORMATTING
设置为'rfc-2822'
(默认为ISO – 8601 ),则我们的时间戳现在将显示为
Thu, 28 Feb 2013 16:42:55 +0000
-
Thu, 28 Feb 2013 16:42:55 +0000
This is correct, but I think kind of ugly for an API (though good perhaps for email messages). I want my datetimes to be formatted using ISO-8601, but include the UTC offset. The solution? Write your own Tastypie serializer and override the behavior when serializing dates.
这是正确的,但是我认为API有点难看(尽管对于电子邮件消息可能很好)。 我希望我的日期时间使用ISO-8601进行格式化,但要包含UTC偏移量 。 解决方案? 编写自己的Tastypie 序列化程序,并在序列化日期时覆盖行为。
Of course, you now make all the resources in your application inherit from MyModelResource
. Finally our dates will be printed like this:
当然,现在您可以使应用程序中的所有资源都继承自MyModelResource
。 最后,我们的日期将像这样打印:
2013-02-28T16:42:55.08+00:00
-
2013-02-28T16:42:55.08+00:00
翻译自: https://www.pybloggers.com/2013/03/displaying-timezone-aware-dates-with-tastypie/
java可识别的规范时区