Skip to content

Commit

Permalink
Moved round_to_hh_mm() function to outermost scope
Browse files Browse the repository at this point in the history
  • Loading branch information
vthorsteinsson committed Oct 19, 2019
1 parent 55efb8c commit 3cfc010
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/straeto/straeto.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,24 @@ def locfmt(loc):
return "({0:.6f},{1:.6f})".format(loc[0], loc[1])


def round_to_hh_mm(ts, round_down=False):
""" Round a timestamp to a (h, m, s) tuple of the form hh:mm:00 """
h, m, s = ts.hour, ts.minute, ts.second
if round_down:
# Always round down
s = 0
elif s > 30 or (s == 30 and (m % 2)):
# Round up, or to an even number of minutes if seconds == 30
s = 0
m += 1
if m >= 60:
m -= 60
h += 1
if h >= 24:
h -= 24
return h, m, s


class BusCalendar:

""" This class contains a mapping from dates to the BusServices that
Expand Down Expand Up @@ -1262,23 +1280,6 @@ def diff(hms1, hms2):
if not result:
return None

def round_to_hh_mm(ts, round_down=False):
""" Round a timestamp to a (h, m, s) tuple of the form hh:mm:00 """
h, m, s = ts.hour, ts.minute, ts.second
if round_down:
# Always round down
s = 0
elif s > 30 or (s == 30 and (m % 2)):
# Round up, or to an even number of minutes if seconds == 30
s = 0
m += 1
if m == 60:
m = 0
h += 1
if h == 24:
h = 0
return h, m, s

# The result dict is compatible with BusSchedule.arrivals(),
# and contains entries for directions where each entry has a list of hms tuples
# (in this case only one hms tuple for each direction)
Expand Down

0 comments on commit 3cfc010

Please sign in to comment.