这是我第一次在Stackoverflow上发帖,请原谅我的语法。我想让一个SQLAlchemy数据库中没有时区的date timestamp中的时间戳在用户的浏览器中以他们的时区显示时间,而不是UTC。
这是我的pythonFlask代码(我是初学者)。
timeclockinfo = TimeClock.query.filter_by(parent_id=current_user.parent_id, user_id=current_user.user_id, closed=1).all()
#Then I tuple the data
child_patient = zip(timeclockinfo, user_names, visit_dates )
#I render the data with Flask
return render_template('locationcheckinrpt.html', form=form, child_patient=child_patient, timeclockinfo=timeclockinfo, searchForm=searchForm)
.
.
.
#In the template I have a date time field for the records rendered
{% for times in child_time %}
{{ times[0].checkintime.strftime('%Y/%m/%d @ %I:%M %p') }}
{% endfor %}
谁能告诉我如何让UTC times[0].checkintime显示在浏览器用户的时区而不是UTC。
我让用户输入他们的时区,然后减去适当的小时数。
但是,我无法通过黑客的方式让它显示出来。
如果你有datetime和用户的时区,你可以在SQLAlchemy数据库中创建一个 模板过滤器 来接收一个datetime对象,进行必要的计算,然后打印出字符串。
如果你的问题是关于如何将时区应用到一个天真的日期时间对象上,那么请看一下 薯条 (相关部门):
from datetime import datetime
from pytz import timezone
tz = timezone('saved_user_timezone')
dt = saved_time_from_db
locl_dt = tz.localize(dt)
要显示时区偏移的日期时间,请尝试:
local_dt.replace(hour=local_dt.hour + int(local_dt.utcoffset().total_seconds() / 3600))
local_dt.strftime('your format')