# HG changeset patch # User Daniel O'Connor # Date 1513956645 -3600 # Node ID 3f22fa1f32d5b0f55ebe99fcc740d30f8427cfaa # Parent 2b115732f4bcba2d7e0bcdbf8029761444084ed8 Twiddle with start/end passing, add option for overriding local time zone. diff -r 2b115732f4bc -r 3f22fa1f32d5 graph.py --- a/graph.py Fri Dec 22 13:19:52 2017 +0100 +++ b/graph.py Fri Dec 22 16:30:45 2017 +0100 @@ -59,6 +59,7 @@ 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) parser.add_argument('-c', '--column', help = 'Column to plot (can be specified multiple times)', type = str, action = 'append') + parser.add_argument('-z', '--timezone', help = 'Timezone to operate in in (Oslon format) default: localtime)', type = str) args = parser.parse_args() @@ -101,11 +102,15 @@ # Get local timezone name and convert start/end to it # Why is this so hard... - ltname = tzlocal.get_localzone().zone - ltname = 'Australia/Adelaide' - lt = dateutil.tz.gettz(ltname) + if args.timezone is None: + args.timezone = tzlocal.get_localzone().zone + + lt = dateutil.tz.gettz(args.timezone) + if lt == None: + parser.error('Unknown timezone') + utc = dateutil.tz.gettz('UTC') - matplotlib.rcParams['timezone'] = ltname + matplotlib.rcParams['timezone'] = args.timezone if args.start.tzinfo == None: args.start = args.start.replace(tzinfo = lt) @@ -115,15 +120,15 @@ endlt = args.end args.start = args.start.astimezone(utc) args.end = args.end.astimezone(utc) - graph(args.graphfn, cur, cols, int(args.start.strftime('%s')), int(args.end.strftime('%s')), lt, utc) + graph(args.graphfn, cur, cols, args.start, args.end, lt, utc) def graph(fname, cur, _cols, start, end, lt, utc): import numpy import matplotlib import matplotlib.dates - startdt = datetime.datetime.fromtimestamp(start).replace(tzinfo = utc).astimezone(lt) - enddt = datetime.datetime.fromtimestamp(end).replace(tzinfo = utc).astimezone(lt) + startepoch = int(start.strftime('%s')) + endepoch = int(end.strftime('%s')) colourlist = ['b','g','r','c','m','y','k'] @@ -156,7 +161,7 @@ for c in cols: # Get the data cur.execute('SELECT tstamp, ' + c.rowname + ' FROM ' + c.table + ' WHERE tstamp > ? AND tstamp < ? ORDER BY tstamp', - (start, end)) + (startepoch, endepoch)) ary = numpy.array(cur.fetchall()) if ary.shape[0] == 0: print('No data for ' + c.rowname) @@ -223,13 +228,13 @@ ax1.text(0.02, 0.5, reduce(lambda a, b: a + '\n' + b, annotations), transform = ax1.transAxes, bbox = dict(facecolor = 'blue', alpha = 0.5), ha = 'left', va = 'top') - ndays = int(max(1, round((end - start) / 86400))) + ndays = int(max(1, round(((end - start).total_seconds()) / 86400))) for ax in fig.get_axes(): - if (enddt - startdt).total_seconds() > 86400: - ax.set_title('%s to %s' % (startdt.strftime('%Y-%m-%d'), enddt.strftime('%Y-%m-%d'))) + if ndays > 1: + ax.set_title('%s to %s' % (start.strftime('%Y-%m-%d'), end.strftime('%Y-%m-%d'))) else: - ax.set_title('%s' % (startdt.strftime('%Y-%m-%d'))) - ax.set_xlim([startdt, enddt]) + ax.set_title('%s' % (start.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) ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%d %b\n%H:%M'))