So I have datetime objects that I want to show up for users in their local time.
Using answered questions on here, I've come up with a jinja filter to accomplish this:
from tzlocal import get_localzone
import pytz
def local_datetime(utc_dt):
local_tz = get_localzone()
local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz)
return local_dt.strftime('%m/%d/%y @ %I:%M %p')
app.jinja_env.filters['local_dt'] = local_datetime
{{ user.last_login_at|local_dt }} # in my template
My thought was that it would run each time someone views the page (hence the filter) so that it will always show in the user's native timezone.
It shows up right on my development machine, but I'd like to make sure that get_localzone() is actually grabbing the user's local timezone and not always the server's.
My question is: How can I test if this is working correctly?
get_localzone()
will always return the local timezone of the server your application is running on.
There is nothing in the HTTP headers of the request which can tell you the user's timezone. Instead, the standard way of approaching this is to ask the user to tell you their preferred timezone.
See Determine a User's Timezone for more discussion about this.