changeset 13:988e511a5f29

Run strftime on filename. Use localtime for display / filename.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 11 Sep 2017 20:57:54 +0930
parents b610ce4ec081
children db4951d2d303
files agl.py
diffstat 1 files changed, 8 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/agl.py	Mon Sep 11 20:57:10 2017 +0930
+++ b/agl.py	Mon Sep 11 20:57:54 2017 +0930
@@ -27,7 +27,7 @@
     parser = argparse.ArgumentParser()
     parser.add_argument('-u', '--update', help = 'Update data', action="store_true")
     parser.add_argument('-g', '--graph', help = 'Produce graph', action="store_true")
-    parser.add_argument('-f', '--filename', help = 'Filename to save graph as', type = str)
+    parser.add_argument('-f', '--filename', help = 'Filename to save graph as (uses strftime on start)', type = str)
     parser.add_argument('-s', '--start', help = 'Start date for graph (YYYY-MM-DD)', type = valid_date)
     parser.add_argument('-e', '--end', help = 'End date for graph (YYYY-MM-DD)', type = valid_date)
 
@@ -55,7 +55,6 @@
     if end is None:
         end = start + datetime.timedelta(days = 1)
         end = datetime.datetime(end.year, end.month, end.day)
-
     dbh = sqlite3.connect(dbfn, detect_types = sqlite3.PARSE_DECLTYPES)
     cur = dbh.cursor()
     if args.update:
@@ -80,7 +79,9 @@
             date += datetime.timedelta(days = 1)
 
     if args.graph:
-        graph(args.filename, cur, ['battery_charge', 'battery_power', 'power_imported', 'power_exported', 'power_consumed', 'power_generated'], start, end)
+        graph(args.filename, cur,
+                  ['battery_charge', 'battery_power', 'power_imported', 'power_exported', 'power_consumed', 'power_generated'],
+                  start, end)
 
 def mkdb(cur):
     cur.execute('''
@@ -187,7 +188,8 @@
         start = start.replace(tzinfo = lt)
     if end.tzinfo == None:
         end = end.replace(tzinfo = lt)
-
+    startlt = start
+    endlt = end
     start = start.astimezone(utc)
     end = end.astimezone(utc)
 
@@ -264,7 +266,7 @@
                  ha = 'left', va = 'top')
     ndays = int(max(1, round(((end - start).total_seconds()) / 86400)))
     for ax in fig.get_axes():
-        ax.set_title('%s to %s' % (start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d')))
+        ax.set_title('%s to %s' % (startlt.strftime('%Y-%m-%d'), endlt.strftime('%Y-%m-%d')))
         ax.set_xlim([start, end])
         ax.format_xdata = lambda d: matplotlib.dates.num2date(d).strftime('%d %b %H:%M')
         ax.xaxis.grid(True)
@@ -281,7 +283,7 @@
         matplotlib.pyplot.show()
     else:
         canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig) # Sets canvas in fig too
-        fig.savefig(fname)
+        fig.savefig(startlt.strftime(fname))
 
 def updatedb(cur, data):
     mkdb(cur)