Jinja2日期和时间延长
jinja2-time的Python项目详细描述
Jinja2日期和时间的扩展
用法
现在标记
扩展名带有一个now标记,可以方便地访问 arrow.now()来自模板的api。
可以通过指定将传递给的格式来控制输出 Python的strftime():
fromjinja2importEnvironmentenv=Environment(extensions=['jinja2_time.TimeExtension'])# Timezone 'local', default format -> "2015-12-10"template=env.from_string("{% now 'local' %}")# Timezone 'utc', explicit format -> "Thu, 10 Dec 2015 15:49:01"template=env.from_string("{% now 'utc', '%a, %d %b %Y %H:%M:%S' %}")# Timezone 'Europe/Berlin', explicit format -> "CET +0100"template=env.from_string("{% now 'Europe/Berlin', '%Z %z' %}")# Timezone 'utc', explicit format -> "2015"template=env.from_string("{% now 'utc', '%Y' %}")template.render()
默认日期时间格式
timeextension使用datetime_format属性扩展环境。
如果省略now的格式,它将用作回退。
fromjinja2importEnvironmentenv=Environment(extensions=['jinja2_time.TimeExtension'])env.datetime_format='%a, %d %b %Y %H:%M:%S'# Timezone 'utc', default format -> "Thu, 10 Dec 2015 15:49:01"template=env.from_string("{% now 'utc' %}")template.render()
时间偏移
jinja2 time实现了一个方便的接口,可以通过 相对时间偏移量:
# Examples for now "2015-12-09 23:33:01"# "Thu, 10 Dec 2015 01:33:31""{% now 'utc' + 'hours=2, seconds=30' %}"# "Wed, 09 Dec 2015 23:22:01""{% now 'utc' - 'minutes=11' %}"# "07 Dec 2015 23:00:00""{% now 'utc' - 'days=2, minutes=33, seconds=1', '%d %b %Y %H:%M:%S' %}"
有关底层功能的进一步文档可以在 arrow replace docs。
问题
如果您遇到任何问题,请file an issue连同详细说明。
行为准则
在Jinja2 Time项目的代码库中进行交互的每个人,发布追踪器,聊天 房间和邮件列表应遵循PyPA Code of Conduct。