Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47c33e0

Browse files
authored
Merge pull request matplotlib#15478 from ihincks/fix-15468-concisedateformatter-timezone
FIX: Make ConciseDateFormatter obey timezone
2 parents 09bcbdc + b880979 commit 47c33e0

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

lib/matplotlib/dates.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ def __call__(self, x, pos=None):
752752
return formatter(x, pos=pos)
753753

754754
def format_ticks(self, values):
755-
tickdatetime = [num2date(value) for value in values]
755+
tickdatetime = [num2date(value, tz=self._tz) for value in values]
756756
tickdate = np.array([tdt.timetuple()[:6] for tdt in tickdatetime])
757757

758758
# basic algorithm:
@@ -818,7 +818,7 @@ def get_offset(self):
818818
return self.offset_string
819819

820820
def format_data_short(self, value):
821-
return num2date(value).strftime('%Y-%m-%d %H:%M:%S')
821+
return num2date(value, tz=self._tz).strftime('%Y-%m-%d %H:%M:%S')
822822

823823

824824
class AutoDateFormatter(ticker.Formatter):

lib/matplotlib/tests/test_dates.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,49 @@ def _create_auto_date_locator(date1, date2):
511511
assert strings == expected
512512

513513

514+
def test_concise_formatter_tz():
515+
def _create_auto_date_locator(date1, date2, tz):
516+
fig, ax = plt.subplots()
517+
518+
locator = mdates.AutoDateLocator(interval_multiples=True)
519+
formatter = mdates.ConciseDateFormatter(locator, tz=tz)
520+
ax.yaxis.set_major_locator(locator)
521+
ax.yaxis.set_major_formatter(formatter)
522+
ax.set_ylim(date1, date2)
523+
fig.canvas.draw()
524+
sts = []
525+
for st in ax.get_yticklabels():
526+
sts += [st.get_text()]
527+
return sts, ax.yaxis.get_offset_text().get_text()
528+
529+
d1 = datetime.datetime(1997, 1, 1).replace(tzinfo=datetime.timezone.utc)
530+
results = ([datetime.timedelta(hours=40),
531+
['03:00', '07:00', '11:00', '15:00', '19:00', '23:00',
532+
'03:00', '07:00', '11:00', '15:00', '19:00'],
533+
"1997-Jan-02"
534+
],
535+
[datetime.timedelta(minutes=20),
536+
['03:00', '03:05', '03:10', '03:15', '03:20'],
537+
"1997-Jan-01"
538+
],
539+
[datetime.timedelta(seconds=40),
540+
['03:00', '05', '10', '15', '20', '25', '30', '35', '40'],
541+
"1997-Jan-01 03:00"
542+
],
543+
[datetime.timedelta(seconds=2),
544+
['59.5', '03:00', '00.5', '01.0', '01.5', '02.0', '02.5'],
545+
"1997-Jan-01 03:00"
546+
],
547+
)
548+
549+
new_tz = datetime.timezone(datetime.timedelta(hours=3))
550+
for t_delta, expected_strings, expected_offset in results:
551+
d2 = d1 + t_delta
552+
strings, offset = _create_auto_date_locator(d1, d2, new_tz)
553+
assert strings == expected_strings
554+
assert offset == expected_offset
555+
556+
514557
def test_auto_date_locator_intmult_tz():
515558
def _create_auto_date_locator(date1, date2, tz):
516559
locator = mdates.AutoDateLocator(interval_multiples=True, tz=tz)

0 commit comments

Comments
 (0)