Reputation: 21
This is my first Stackoverflow post, pardon my syntax.
I am trying to have a time stamp in date timestamp without time zone in a SQLAlchemy database display the time in the user's browser with the time in the their timezone, not UTC.
Here is my python/Flask code (I'm a beginner):
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 %}
Can anyone advise me on how to have the UTC times[0].checkintime display in the browser users timezone and not UTC.
I do have the User enter their time zone so I subtract the appropriate number of hours.
But, I cannot hack my way through getting this to display.
Upvotes: 2
Views: 2291
Reputation: 5682
If you have the datetime and the user's time zone, you could create a template filter that takes a datetime object, does the required computation, and then prints the string.
If your questions is about actually about applying a time zone to a naive datetime object, then take a look at pytz (relevant section):
from datetime import datetime
from pytz import timezone
tz = timezone('saved_user_timezone')
dt = saved_time_from_db
locl_dt = tz.localize(dt)
To display the datetime with the timezone offset, try:
local_dt.replace(hour=local_dt.hour + int(local_dt.utcoffset().total_seconds() / 3600))
local_dt.strftime('your format')
Upvotes: 1