From 7486a50c33c35c42a1f98ad9bc050dc6886faab2 Mon Sep 17 00:00:00 2001 From: JonnyWong16 Date: Thu, 12 Dec 2019 09:12:29 -0800 Subject: [PATCH] Calculate months for graphs using datetime --- plexpy/graphs.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/plexpy/graphs.py b/plexpy/graphs.py index 3abb5b70..27500cfa 100644 --- a/plexpy/graphs.py +++ b/plexpy/graphs.py @@ -369,19 +369,23 @@ class Graphs(object): # create our date range as some months may not have any data # but we still want to display them - base = time.localtime() - month_range = [time.localtime( - time.mktime((base.tm_year, base.tm_mon - n, 1, 0, 0, 0, 0, 0, 0))) for n in range(int(time_range))] + dt_today = datetime.date.today() + dt = dt_today + month_range = [dt] + for n in range(int(time_range)-1): + if not ((dt_today.month-n) % 12)-1: + dt = datetime.date(dt.year-1, 12, 1) + else: + dt = datetime.date(dt.year, dt.month-1, 1) + month_range.append(dt) categories = [] series_1 = [] series_2 = [] series_3 = [] - for month_item in sorted(month_range): - dt = datetime.datetime(*month_item[:6]) + for dt in sorted(month_range): date_string = dt.strftime('%Y-%m') - categories.append(dt.strftime('%b %Y').decode(plexpy.SYS_ENCODING, 'replace')) series_1_value = 0 series_2_value = 0