Does Streamlit offer a built‑in option to return timezone‑aware datetime values from its date and time widgets?
0 reputation · 01 Mar 2023, 04:30 UTC
0 reputation · 01 Mar 2023, 04:30 UTC
Goal: Ensure that the date and time values returned by Streamlit’s st.date_input and st.time_input widgets can be reliably interpreted in a uniform timezone (e.g., UTC) without requiring each developer to write custom conversion logic.
Constraint: The widgets currently return naive datetime.date and datetime.time objects that reflect the user's browser‑reported local time, with no built‑in tz argument to shift them to a specific zone. This leaves room for inconsistency when the client clock is misconfigured, a VPN alters the perceived offset, or the application runs across multiple regions.
Questions: Should Streamlit add a tz parameter to st.date_input and st.time_input to automatically produce timezone‑aware datetime objects? Would such a change break existing code that expects the current naive return type? How should the library behave when the browser does not provide a trustworthy timezone offset?
As of Streamlit 1.28‑1.35, st.date_input and st.time_input return naive datetime.date and datetime.time objects that reflect the browser’s local time. There is no tz or timezone parameter, and the widgets do not automatically produce timezone‑aware datetime values.
datetime.date and datetime.time directly.tz argument for either widget.tz keyword raises a TypeError.import streamlit as st
from datetime import datetime, date, time
from zoneinfo import ZoneInfo # Python 3.9+
datetime:d: date = st.date_input('Select date')
t: time = st.time_input('Select time')
naive_dt: datetime = datetime.combine(d, t)
aware_dt: datetime = naive_dt.replace(tzinfo=ZoneInfo('UTC'))
aware_dt in your logic; ensure all datetimes in the calculation are either all naive or all aware to avoid TypeError.If you are using a Streamlit version newer than 1.35, please confirm the exact version, as a future release could introduce a tz parameter that would change the recommendation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.