Skip to content

Commit 2f3cd05

Browse files
committed
FEAT: Localize Datetimes
1 parent 760010b commit 2f3cd05

File tree

5 files changed

+28
-17
lines changed

5 files changed

+28
-17
lines changed

diagnostic_interface/canvas/plot_canvas.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
from diagnostic_interface import settings
99
from typing import Optional
1010
import mplcursors
11-
11+
from dateutil import tz
1212

1313
plt.style.use("seaborn-v0_8-darkgrid")
14+
local_tz = tz.tzlocal()
1415

1516

1617
class PlotCanvas(FigureCanvas):
@@ -42,8 +43,13 @@ def plot(self, ts: TimeSeries, title: str, y_label: str) -> None:
4243
self.ax.set_title(title)
4344
self.ax.set_xlabel("Time")
4445
self.ax.set_ylabel(y_label)
45-
self.ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
46-
self.ax.xaxis.set_major_locator(mdates.HourLocator())
46+
47+
locator = mdates.AutoDateLocator()
48+
formatter = mdates.DateFormatter("%H:%M", tz=local_tz)
49+
50+
self.ax.xaxis.set_major_formatter(formatter)
51+
self.ax.xaxis.set_major_locator(locator)
52+
4753
self.figure.autofmt_xdate()
4854

4955
else:
@@ -59,7 +65,7 @@ def plot(self, ts: TimeSeries, title: str, y_label: str) -> None:
5965
@cursor.connect("add")
6066
def _(sel):
6167
x, y = sel.target # x is a float (matplotlib date), y is the y-value
62-
dt = mdates.num2date(x)
68+
dt = mdates.num2date(x, tz=local_tz)
6369
sel.annotation.set_text(
6470
f"{y:.2f} {ts.units} at {dt.strftime('%H:%M')}"
6571
)
@@ -129,9 +135,10 @@ def plot(self, data, data2):
129135
self.ax.legend([self.line1, self.line2], ["Wind Speed", "Precipitation Rate"])
130136

131137
locator = mdates.AutoDateLocator()
132-
formatter = mdates.ConciseDateFormatter(locator)
133-
self.ax.xaxis.set_major_locator(locator)
138+
formatter = mdates.DateFormatter("%H:%M", tz=local_tz)
139+
134140
self.ax.xaxis.set_major_formatter(formatter)
141+
self.ax.xaxis.set_major_locator(locator)
135142

136143
else:
137144
# Only update data
@@ -154,7 +161,7 @@ def plot(self, data, data2):
154161
@cursor.connect("add")
155162
def _(sel):
156163
x, y = sel.target # x is a float (matplotlib date), y is the y-value
157-
dt = mdates.num2date(x)
164+
dt = mdates.num2date(x, tz=local_tz)
158165
sel.annotation.set_text(
159166
f"{y:.2f} at {dt.strftime('%H:%M')}"
160167
)
@@ -238,9 +245,10 @@ def plot(self, data, plot_title, ylabel):
238245

239246
# Improve datetime formatting
240247
locator = mdates.AutoDateLocator()
241-
formatter = mdates.ConciseDateFormatter(locator)
242-
self.ax.xaxis.set_major_locator(locator)
248+
formatter = mdates.DateFormatter("%H:%M", tz=local_tz)
249+
243250
self.ax.xaxis.set_major_formatter(formatter)
251+
self.ax.xaxis.set_major_locator(locator)
244252

245253
else:
246254
# Only update data
@@ -259,7 +267,7 @@ def plot(self, data, plot_title, ylabel):
259267
@cursor.connect("add")
260268
def _(sel):
261269
x, y = sel.target # x is a float (matplotlib date), y is the y-value
262-
dt = mdates.num2date(x)
270+
dt = mdates.num2date(x, tz=local_tz)
263271
sel.annotation.set_text(
264272
f"{y/1e6:.2f} MJ/m^2 at {dt.strftime('%H:%M')}"
265273
)

diagnostic_interface/canvas/realtime_canvas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def plot(self, ts: TimeSeries, title: str, y_label: str) -> None:
6868
def _(sel):
6969
xdate, yval = sel.target
7070
# Interpret xdate as UTC, then convert to local
71-
dt_utc = mdates.num2date(xdate, tz=timezone.utc)
71+
dt_utc = mdates.num2date(xdate, tz=local_tz)
7272
dt_local = dt_utc.astimezone(local_tz)
7373
sel.annotation.set_text(
7474
f"{yval:.2f} {ts.units} at {dt_local.strftime('%H:%M:%S')}"

diagnostic_interface/start_application.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
window.show()
2020

2121
window._splash.showMessage("Loading...")
22-
QTimer.singleShot(500, lambda: window._splash.showMessage("Crimping connectors..."))
23-
QTimer.singleShot(1500, lambda: window._splash.showMessage("Recrimping connectors..."))
22+
QTimer.singleShot(1500, lambda: window._splash.showMessage("Loading..."))
2423
QTimer.singleShot(2000, window.finishSplash)
2524

2625
sys.exit(app.exec_())

diagnostic_interface/tabs/array_tab.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
1111
from data_tools import SunbeamClient
1212
import mplcursors
13+
from dateutil import tz
14+
15+
16+
local_tz = tz.tzlocal()
1317

1418

1519
class PlotRefreshWorkerSignals(QObject):
@@ -58,8 +62,8 @@ def plot(self, data, data2):
5862

5963
self.ax.legend([self.line1, self.line2], ["Array Power", "GHI"])
6064

61-
locator = mdates.AutoDateLocator()
62-
formatter = mdates.ConciseDateFormatter(locator)
65+
locator = mdates.AutoDateLocator(tz=local_tz)
66+
formatter = mdates.ConciseDateFormatter(locator, tz=local_tz)
6367
self.ax.xaxis.set_major_locator(locator)
6468
self.ax.xaxis.set_major_formatter(formatter)
6569

@@ -84,7 +88,7 @@ def plot(self, data, data2):
8488
@cursor.connect("add")
8589
def _(sel):
8690
x, y = sel.target # x is a float (matplotlib date), y is the y-value
87-
dt = mdates.num2date(x)
91+
dt = mdates.num2date(x, tz=local_tz)
8892
sel.annotation.set_text(
8993
f"{y:.2f} at {dt.strftime('%H:%M')}"
9094
)

prescriptive_interface/prescriptive_ui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def update_output(self, message):
265265
window.show()
266266

267267
window._splash.showMessage("Crimping connectors...")
268-
QTimer.singleShot(500, lambda: window._splash.showMessage("Recrimping connectors..."))
268+
QTimer.singleShot(500, lambda: window._splash.showMessage("Loading..."))
269269
QTimer.singleShot(1500, window.finishSplash)
270270

271271
sys.exit(app.exec_())

0 commit comments

Comments
 (0)