最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【基本解决】设置flask-restful中的marshal中fields.DateTime的参数dt_format

Flask crifan 3805浏览 0评论

之前看到介绍:

Output Fields — Flask-RESTful 0.2.1 documentation

<code>'date_updated': fields.DateTime(dt_format='rfc822'),
</code>

dt_format只支持rfc822

现在去测试了后:

发现默认的,Datetime不带参数的话,会返回这样的结果:

“Thu, 18 Aug 2016 14:14:26 -0000”

现在希望:

能返回更好看,方便解析的结果

所以想要去找到dt_format的参数怎么设置。

搜:

python – issue with dates and flask-restful – Stack Overflow

关于flask-restful扩展中field.DataTime使用的求助 – 开源中国社区

-》

http://flask-restful.readthedocs.io/en/latest/api.html#fields.DateTime

-》

API Docs — Flask-RESTful 0.3.5 documentation

class fields.DateTime(dt_format=’rfc822′**kwargs)

Return a formatted datetime string in UTC. Supported formats are RFC 822 and ISO 8601.

See email.utils.formatdate() for more info on the RFC 822 format.

See datetime.datetime.isoformat() for more info on the ISO 8601 format.

Parameters:dt_format (str) – ‘rfc822’ or ‘iso8601’

format(value)

-》

8.1. datetime — Basic date and time types — Python 3.5.2 documentation

datetime.isoformat(sep=’T’)

Return a string representing the date and time in ISO 8601 format, YYYY-MM-DDTHH:MM:SS.mmmmmm or, if microsecond is 0, YYYY-MM-DDTHH:MM:SS

If utcoffset() does not return None, a 6-character string is appended, giving the UTC offset in (signed) hours and minutes: YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM or, if microsecond is 0 YYYY-MM-DDTHH:MM:SS+HH:MM

The optional argument sep (default ‘T’) is a one-character separator, placed between the date and time portions of the result. For example,

<code>&gt;&gt;&gt; from datetime import tzinfo, timedelta, datetime
&gt;&gt;&gt; class TZ(tzinfo):
...     def utcoffset(self, dt): return timedelta(minutes=-399)
...
&gt;&gt;&gt; datetime(2002, 12, 25, tzinfo=TZ()).isoformat(' ')
'2002-12-25 00:00:00-06:39'
</code>

19.1.12. email.utils: Miscellaneous utilities — Python 3.5.2 documentation

https://docs.python.org/3/library/email.util.html#email.utils.formatdate

email.utils.formatdate(timeval=Nonelocaltime=Falseusegmt=False)

Returns a date string as per RFC 2822, e.g.:

<code>Fri, 09 Nov 2001 01:08:47 -0000
</code>

Optional timeval if given is a floating point time value as accepted by time.gmtime() and time.localtime(), otherwise the current time is used.

Optional localtime is a flag that when True, interprets timeval, and returns a date relative to the local timezone instead of UTC, properly taking daylight savings time into account. The default is False meaning UTC is used.

Optional usegmt is a flag that when True, outputs a date string with the timezone as an ascii string GMT, rather than a numeric -0000. This is needed for some protocols (such as HTTP). This only applies when localtime is False. The default is False.

-》

暂时,用:

<code>    'created_at': fields.DateTime(dt_format='iso8601'),
</code>

输出:

<code>"created_at": "2016-10-10T16:29:43",
</code>

-》

但是看来要是自定义的话,还是需要自己手动去格式化输出才行。。。

转载请注明:在路上 » 【基本解决】设置flask-restful中的marshal中fields.DateTime的参数dt_format

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.173 seconds, using 22.09MB memory