I'm trying to convert a UTC time to the appropriate local time in a flask application. I'm wondering if I can detect the user's timezone and dynamically set it.
This is my code so far which works for everyone in the US/Pacific timezone only (datetimefilter pulled from this SO question)
from flask import Flask, render_template_string
import datetime
import pytz
app = Flask(__name__)
@app.template_filter('datetimefilter')
def datetimefilter(value, format='%b %d %I:%M %p'):
    tz = pytz.timezone('US/Pacific')  # timezone you want to convert to from UTC (America/Los_Angeles)
    utc = pytz.timezone('UTC')
    value = utc.localize(value, is_dst=None).astimezone(pytz.utc)
    local_dt = value.astimezone(tz)
    return local_dt.strftime(format)
@app.route('/')
def index():
    dt = datetime.datetime.utcnow()
    return render_template_string(
        '''
        <h4>UTC --> Local Time</h4>
        Datetime right now is:
        <p>{{ dt | datetimefilter }} (utc --> local)</p>
        ''',
        dt=dt, now=now)
if __name__ == '__main__':
    app.run(debug=True)
I imagine this can't be handled server-side because the timezone will always be set to wherever the server is located? Is there a way to handle this by changing datetimefilter?
 
     
    