diff --git a/wxmplot/basepanel.py b/wxmplot/basepanel.py index 6d9cb41..8b84317 100644 --- a/wxmplot/basepanel.py +++ b/wxmplot/basepanel.py @@ -15,9 +15,12 @@ from matplotlib.widgets import Lasso from matplotlib import dates from matplotlib.backends.backend_wx import RendererWx +import pytz from .utils import Printer, MenuItem +tzname = os.environ.get('TZ', 'UTC') +TIMEZONE = pytz.timezone(tzname) class BasePanel(wx.Panel): """ @@ -46,6 +49,7 @@ def __init__(self, parent, messenger=None, self._yfmt = self._y2fmt = self._xfmt = None self._y3fmt = self._y4fmt = None self.use_dates = False + self.dates_tzinfo = TIMEZONE self.show_config_popup = show_config_popup self.launch_dir = get_cwd() @@ -324,8 +328,8 @@ def __date_format(self, x): span = self.axes.xaxis.get_view_interval() tmin = max(1.0, span[0]) tmax = max(2.0, span[1]) - tmin = dates.num2date(tmin).timestamp() - tmax = dates.num2date(tmax).timestamp() + tmin = dates.num2date(tmin, tz=self.dates_tzinfo).timestamp() + tmax = dates.num2date(tmax, tz=self.dates_tzinfo).timestamp() nsec = (tmax - tmin) fmt = "%H:%M\n%S" frac = None @@ -344,7 +348,7 @@ def __date_format(self, x): else: fmt = "%m/%d" - dtval = dates.num2date(x) + dtval = dates.num2date(x, tz=self.dates_tzinfo) try: out = dtval.strftime(fmt) except ValueError: diff --git a/wxmplot/plotpanel.py b/wxmplot/plotpanel.py index 9b9f9df..ceaf4de 100644 --- a/wxmplot/plotpanel.py +++ b/wxmplot/plotpanel.py @@ -11,6 +11,7 @@ from numpy import nonzero, where import matplotlib as mpl from matplotlib.dates import date2num, datestr2num, num2date +from matplotlib.dates import AutoDateFormatter, AutoDateLocator from matplotlib.figure import Figure from matplotlib.ticker import FuncFormatter @@ -26,7 +27,7 @@ to_rgba = colorConverter.to_rgba -def format_date(x, xrange): +def format_date(x, xrange, tz=None): if xrange > 12: # 12 days dtformat = "%Y-%m-%d" elif xrange > 3: # 3 days @@ -39,7 +40,7 @@ def format_date(x, xrange): dtformat = "%H:%M:%S.%f" else: dtformat = "%M:%S.%f" - return datetime.strftime(num2date(x), dtformat) + return datetime.strftime(num2date(x, tz=tz), dtformat) class PlotPanel(BasePanel): @@ -136,8 +137,8 @@ def oplot(self, xdata, ydata, label=None, xlabel=None, ylabel=None, bgcolor=None, framecolor=None, gridcolor=None, textcolor=None, labelfontsize=None, titlefontsize=None, legendfontsize=None, fullbox=None, axes_style=None, zorder=None, viewpad=None, - theme=None, use_dates=None, dates_style=None, yaxes=1, side=None, - yaxes_tracecolor=None, **kws): + theme=None, use_dates=None, dates_style=None, timezone=None, + yaxes=1, side=None, yaxes_tracecolor=None, **kws): """ basic plot method, adding to an existing display @@ -161,6 +162,7 @@ def oplot(self, xdata, ydata, label=None, xlabel=None, ylabel=None, self.dates_style = ifnot_none(dates_style, self.dates_style) self.use_dates = ifnot_none(use_dates, self.use_dates) + if isinstance(xdata[0], datetime): self.use_dates = True @@ -174,9 +176,12 @@ def oplot(self, xdata, ydata, label=None, xlabel=None, ylabel=None, if dstyle is None: dstyle = '' if isinstance(x0, datetime): + self.dates_tzinfo = xdata[0].tzinfo xdata = date2num(xdata) elif isinstance(x0, str) or dstyle.lower().startswith('str'): xdata = datestr2num(xdata) + if timezone is not None: + self.dates_tzinfo = timezone linewidth = ifnot_none(linewidth, 2) conf.viewpad = ifnot_none(viewpad, conf.viewpad) @@ -931,7 +936,7 @@ def report_leftdown(self, event=None): if self.use_dates: xlims = ax.get_xlim() xrange = abs(xlims[1] - xlims[0]) - x = format_date(x, xrange) + x = format_date(x, xrange, tz=self.dates_tzinfo) else: x = f"{x:g}" msg = f"X,Y= {x}, {y:g}" @@ -981,7 +986,7 @@ def report_motion(self, event=None): ax = self.canvas.figure.get_axes()[0] xlims = ax.get_xlim() xrange = abs(xlims[1] - xlims[0]) - x = format_date(x, xrange) + x = format_date(x, xrange, tz=self.dates_tzinfo) else: x = f"{x:g}" msg = f"X,Y= {x}, {y:g}"