Skip to content

Add option to format dates as UTC #1956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
brian428 opened this issue Aug 16, 2017 · 2 comments
Open

Add option to format dates as UTC #1956

brian428 opened this issue Aug 16, 2017 · 2 comments
Labels
feature something new P2 considered for next cycle

Comments

@brian428
Copy link

Currently, Plotly offers the tickformat property to specify date formats. But the results are always shown in local time. I see no way to format the dates as UTC other than manually transforming all of the dates to offset based on the current time zone so they look like the equivalent UTC time. And obviously, this is a major pain.

D3 can certainly handle UTC dates, so Plotly really needs to expose something to allow dates to be shown as UTC.

@alexcjohnson
Copy link
Collaborator

To be precise (and yes, we're aware that this is a mess), Plotly ONLY shows dates in UTC (you'll notice every day on a plotly plot has 24 hours, for example), but if you feed in native js Date objects or milliseconds to mean dates (rather than date strings) we assume what you really wanted was the (UTC) date with the same representation as the date you provided has in the local timezone.

See #1532 for more info - one of these days we will need to build in real time zone support, but that's a substantial project (largely due to DST issues)

@schwabts
Copy link

schwabts commented Nov 19, 2022

How to simply show the current (real-) time of two (or more) different timezones simultaneously?

About half a year ago I wrote the code "Timezone Table" below to show a table of all 24 hours of a day correctly aligned for several selected timezones in a Jupyter Notebook. My reason for mentioning that is to point out that

  • the package pendulum (as well as pytz) was of greatest help for getting this done. It provides the abstraction of timezones from any local time to Python but making use of that in Plotly probaly involves bringing this functionality to Javascript, React, or something else I am not familiar with.
  • Clearly, if having all display updates needed to be triggered over the net is to be avoided this involves clientside_callbacks, AFAIK.
  • But I don't even manage to run the "example with two Outputs" in the answers to Display correct time in browsers timezone. After adding dcc.Interval(id="clock", interval=1000) to the layout and brackets around the Output-s the remaining Error is
dc[namespace] is undefined
(This error originated from the built-in JavaScript code that runs Dash apps. Click to see the full stack trace or open your browser's console.)

But even if "the example with two Outputs" worked I believe the two Outputs would display the same time twice. Could anyone run it sucessfully?

Timezone Table

import pandas as pd
import pytz
import pendulum

parameters = {
    'cities': ['Los_Angeles',
               'Chicago',
               'New_York',
               'London',
               'Berlin',
               'Moscow',
               'Dubai',
               'Shanghai',
               'Tokyo'],
    'cities_west_of_chicago': 1,
    'first_column_chicago': 17,
    'separator_chicago': 9
}

timezone_cities = [ [tz for tz in pytz.all_timezones if city in tz][0] for city in parameters['cities']]
pendulum_for_each_city = [pendulum.today(tz) for tz in timezone_cities]

index_chicago = parameters['cities'].index('Chicago')
p_chicago = pendulum_for_each_city[index_chicago]

def get_offset_city(i):
    if i<parameters['cities_west_of_chicago']:
        return(-p_chicago.diff(pendulum_for_each_city[i]).in_hours())
    else:
        return(p_chicago.diff(pendulum_for_each_city[i]).in_hours())

def get_entry(h,o):
    return((parameters['first_column_chicago']+o+h)%24)

offset_cities = [get_offset_city(i) for i in range(len(pendulum_for_each_city))]
data = {_: [get_entry(_,o) for o in offset_cities] for _ in range(24)}
df = pd.DataFrame(data)
df.rename(index={_: parameters['cities'][_] for _ in range(len(parameters['cities']))}, inplace=True)

# Using DataFrame.insert() to add a column
index_separator=int(df.loc[parameters['cities'][index_chicago],[df[col][index_chicago]==parameters['separator_chicago'] for col in df.columns]].index[0])
df.insert(index_separator, "|", ["|" for  city in parameters['cities']], True)

pd.options.display.max_columns = None
display(df)

@gvwilson gvwilson self-assigned this Jun 10, 2024
@gvwilson gvwilson removed their assignment Aug 2, 2024
@gvwilson gvwilson added feature something new P2 considered for next cycle labels Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature something new P2 considered for next cycle
Projects
None yet
Development

No branches or pull requests

4 participants