Newer
Older
""" FireTime
This module include functions used to handle the time conversion
The time step in the tracking system is defined as a list (year, month, day, ampm).
The following functions are used to convert times between different formats.
t : time steps, tuple (year,month,day,ampm)
d : date, datetime.date()
ampm : ampm, str()
dt : time steps, datetime.datetime()
"""
def t_nb(t, nb="next"):
""" Calculate the next or previous time step (year, month, day, ampm)
Parameters
----------
t : tuple, (int,int,int,str)
the year, month, day and 'AM'|'PM' for present time
nb : str, 'next'|'previous'
option to extract next or previous time step
Returns
-------
t_out : tuple, (int,int,int,str)
the year, month, day and 'AM'|'PM' for next/previous time
from datetime import date, timedelta
# the next time step
# if current time is 'AM', set next time as the current day and 'PM'
# if current time is 'PM', set next time as the following day and 'AM'
else:
d = date(*t[:-1])
d_out = d + timedelta(days=1)
# if current time is 'PM', set previous time as the current day and 'AM'
# if current time is 'AM', set previous time as the previous day and 'PM'
else:
d = date(*t[:-1])
d_out = d + timedelta(days=-1)
def t_dif(t1, t2):
""" calculate the time difference between two time steps
Parameters
----------
t1 : tuple, (int,int,int,str)
the year, month, day and 'AM'|'PM' for time 1
t2 : tuple, (int,int,int,str)
the year, month, day and 'AM'|'PM' for time 2
Returns
-------
dt : float
time difference in days (t2-t1), half day as 0.5
from datetime import date
# calculate the day difference
d1 = date(*t1[:-1])
d2 = date(*t2[:-1])
# adjust according to ampm difference
if t1[-1] != t2[-1]:
def dt_dif(dt1,dt2):
''' given two datatime values, calculate the differences (in days)
'''
daysdif = (dt1-dt2).seconds/24/3600 + (dt1-dt2).days
return daysdif
Parameters
----------
t : tuple, (int,int,int,str)
the year, month, day and 'AM'|'PM'
Returns
-------
d : datetime date
date
ampm : str, 'AM'|'PM'
ampm indicator
d = date(*t[:-1]) # current date, datetime date
ampm = t[-1] # current ampm, 'AM'|'PM'
Parameters
----------
t : tuple, (int,int,int,str)
the year, month, day and 'AM'|'PM'
Returns
-------
dt : datetime datetime
datetime
dlh = {"AM": 0, "PM": 12}
dhl = {0: "AM", 12: "PM"}
dt = datetime(*t[:-1], dlh[t[-1]])
def d2t(year, month, day, ampm):
""" convert year, month, day, ampm to a t tuple
Parameters
----------
year : int
year
month : int
month
day : int
day
ampm : str, 'AM'|'PM'
ampm indicator
Returns
-------
t : list, (int,int,int,str)
the year, month, day and 'AM'|'PM'
Parameters
----------
dt : datetime datetime
datetime
Returns
-------
t : tuple, (int,int,int,str)
the year, month, day and 'AM'|'PM'
"""
dlh = {"AM": 0, "PM": 12}
dhl = {0: "AM", 12: "PM"}
def ftrange(firstday, lastday):
""" get datetime range for given first and last t tuples (both ends included)
Parameters
----------
firstday : tuple, (int,int,int,str)
the year, month, day and 'AM'|'PM'
lastday : tuple, (int,int,int,str)
the year, month, day and 'AM'|'PM'
Returns
-------
trange : pandas date range
date range defined by firstday and lastday
trange = pd.date_range(t2dt(firstday), t2dt(lastday), freq="12h")
Parameters
----------
t : tuple, (int,int,int,str)
the year, month, day and 'AM'|'PM'
Returns
-------
Ture: at new year start
False: not at new year start
if (t[1] == 1) & (t[2] == 1) & (t[3] == "AM"):