|
|
|
@ -5,37 +5,16 @@
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
import dateutil.parser
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Config(object):
|
|
|
|
|
def load(self,
|
|
|
|
|
run_start_time,
|
|
|
|
|
):
|
|
|
|
|
# PyYAML tries to...ugh...be clever, and parse timestamps if it can work out how.
|
|
|
|
|
# So we should only try to parse if it's not datetime already.
|
|
|
|
|
if not isinstance(run_start_time, datetime.datetime):
|
|
|
|
|
run_start_time = dateutil.parser.parse(run_start_time)
|
|
|
|
|
self.run_start_time = run_start_time
|
|
|
|
|
def dt_to_bustime(start, dt):
|
|
|
|
|
"""Convert a datetime to bus time. Bus time is seconds since the given start point."""
|
|
|
|
|
return (dt - start).total_seconds()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CONF = Config()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_config(path="/etc/wubloader.yaml"):
|
|
|
|
|
with open(path) as f:
|
|
|
|
|
CONF.load(**yaml.safe_load(f))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dt_to_bustime(dt):
|
|
|
|
|
"""Convert a datetime to bus time. Bus time is seconds since the start of the run
|
|
|
|
|
as defined in the config file."""
|
|
|
|
|
return (dt - CONF.run_start_time).total_seconds()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def bustime_to_dt(bustime):
|
|
|
|
|
def bustime_to_dt(start, bustime):
|
|
|
|
|
"""Convert from bus time to a datetime"""
|
|
|
|
|
return CONF.run_start_time + datetime.timedelta(seconds=bustime)
|
|
|
|
|
return start + datetime.timedelta(seconds=bustime)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def format_bustime(bustime, round="millisecond"):
|
|
|
|
|