quarterly conversions

This commit is contained in:
fawce
2012-09-26 21:19:47 -04:00
committed by Eddie Hebert
parent d98190daf7
commit d2e639c2da
+47
View File
@@ -41,6 +41,53 @@ def parse_iso8061(date_string):
dt = dt.replace(tzinfo=pytz.utc)
return dt
# quarter utilities
# ---------------------
def get_quarter(dt):
"""
convert the given datetime to an integer representing
the number of calendar quarters since 0.
"""
quarters = dt.year * 4
month = dt.month
if month <= 3:
return quarters + 1
elif month <= 6:
return quarters + 2
elif month <= 9:
return quarters + 3
else:
return quarters + 4
def dates_of_quarter(quarter_num):
year = quarter_num / 4
quarter = quarter_num % 4
if quarter == 0:
quarter = 4
if quarter == 1:
start = datetime(year, 1, 1, 0, 0, tzinfo=pytz.utc)
end = datetime(year, 3, 31, 23, 59, tzinfo=pytz.utc)
return start, end
elif quarter == 2:
start = datetime(year, 4, 1, 0, 0, tzinfo=pytz.utc)
end = datetime(year, 6, 30, 23, 59, tzinfo=pytz.utc)
return start, end
elif quarter == 3:
start = datetime(year, 7, 1, 0, 0, tzinfo=pytz.utc)
end = datetime(year, 9, 30, 23, 59, tzinfo=pytz.utc)
return start, end
elif quarter == 4:
start = datetime(year, 10, 1, 0, 0, tzinfo=pytz.utc)
end = datetime(year, 12, 31, 23, 59, tzinfo=pytz.utc)
return start, end
# Epoch utilities
# ---------------------
UNIX_EPOCH = datetime(1970, 1, 1, 0, 0, tzinfo=pytz.utc)