Parsing date and time with Arrow - jordy33/turbogears_tutorial GitHub Wiki
Date and time usually are are managed in UTC time, but How to render this date/time as local-server-timezone?
Well we must use python arrow library
Click here to check library.
Open file setup.py and modify install_requires with the following code:
install_requires = [
"TurboGears2 >= 2.3.11",
"Beaker >= 1.8.0",
"Kajiki >= 0.6.3",
"Mako",
"zope.sqlalchemy >= 0.4",
"sqlalchemy",
"alembic",
"repoze.who",
"tw2.forms",
"tgext.admin >= 0.6.1",
"WebHelpers2",
"transaction <= 2.0.2",
"requests",
"pygal",
"tzlocal",
"arrow"
]
Activate the virtual environment and reinstall imports
cd ~
cd myprojectenv
workon myprojectenv
pip install -e .
Create the file dateformat.mak in templates folder with the following code:
<%inherit file="local:templates.master"/>
<%def name="title()">
Learning TurboGears 2.3: Quick guide to the Quickstart pages.
</%def>
<%def name="head_content()">
</%def>
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Time conversion Example</h2>
</div>
<div class="imgContainer">
<p>UTC time: ${mydate}</p>
<p>Local time: ${localtime}</p>
</div>
</div>
</div>
<%def name="bottom_scripts()">
</%def>
In root.py add the following code at top:
from tzlocal import get_localzone
import arrow
at bottom:
@expose('myprojectname.templates.arrow')
def arrow(self):
mydate =datetime.utcnow()
local_tz = get_localzone()
utc = arrow.get(mydate)
localtime = utc.to(local_tz)
print(localtime)
return dict(mydate=mydate.strftime('%Y-%m-%d %H:%M:%S'),localtime=localtime.strftime('%Y-%m-%d %H:%M:%S'))
Test the code:
http://localhost:8080/arrow