posit-dev / py-shiny

Shiny for Python
https://shiny.posit.co/py/
MIT License
1.3k stars 78 forks source link

datetime slider returns UTC, regardless of timezone specified #556

Open bartverweire opened 1 year ago

bartverweire commented 1 year ago

Hello,

I've noticed that value of an input_slider always return utc datetimes. The input_slider has a property timezone, but apparently this is only for display reasons.

I would expect to have the slider return datetimes in the chosen timezone, or, in case of a None value for timezone, in the local timezone, but this is not the case.

A sample app :

from shiny import *
from datetime import datetime, timedelta

app_ui = ui.page_fluid(
    ui.h2("Timerange Slider"),
    ui.input_slider(id="in_timerange",
                    label="Time Range",
                    min=datetime.now() - timedelta(hours=24),
                    max=datetime.now(),
                    step=timedelta(hours=1),
                    time_format="%d-%H:00",
                    value=[datetime.now() - timedelta(hours=6), datetime.now() - timedelta(hours=4)],
                    width="1600px",
                    drag_range=True
    ),
    ui.output_text_verbatim("out_timerange")
)

def server(input, output, session):
    @output
    @render.text
    def out_timerange():
        # add (datetime.now() - datetime.utcnow() to convert to local timezone
        # timerange = [t + (datetime.now() - datetime.utcnow()) for t in input.in_timerange()]
        timerange = input.in_timerange()

        return timerange

app = App(app_ui, server)

def main():
    run_app(app)

if __name__ == "__main__":
    main()

Kind Regards

Bart

nealrichardson commented 1 year ago

To illustrate, I put the above app code in shinylive.io. See here.