Bru*_*sky 12
我喜欢@ assem-chelli的回答.我将在Jinja2模板中演示它.
#!/bin/env python3
import datetime
from jinja2 import Template
template = Template("""
# Generation started on {{ now() }}
... this is the rest of my template...
# Completed generation.
""")
template.globals['now'] = datetime.datetime.utcnow
print(template.render())
Run Code Online (Sandbox Code Playgroud)
输出:
# Generation started on 2017-11-14 15:48:06.507123
... this is the rest of my template...
# Completed generation.
Run Code Online (Sandbox Code Playgroud)
您应该使用datetimePython库,获取时间并将其作为变量传递给模板:
>>> import datetime
>>> datetime.datetime.utcnow()
'2015-05-15 05:22:17.953439'
Run Code Online (Sandbox Code Playgroud)