Skip to content

Commit

Permalink
adding Period.flatten documentation because i am a huge dick and i wr…
Browse files Browse the repository at this point in the history
…ote code that even i couldn't parse, but because i am alone in a software wasteland here, the only one who could suffer from me destroying the world... is me...
  • Loading branch information
leewardbound committed Jul 20, 2015
1 parent 363b90e commit 9d482b7
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions hailwhale/periods.py
Expand Up @@ -14,6 +14,8 @@
'nickname': 'year'},
{'name': 'Last 30 days, by day', 'length': '1mo', 'interval': '1d',
'nickname': 'thirty'},
{'name': 'Last week, by day', 'length': '1w', 'interval': '1d',
'nickname': 'seven'},
{'name': 'Last day, by hour', 'length': '1d', 'interval': '1h',
'nickname': 'day'},
{'name': 'Last hour, by 1 minutes', 'length': '1h', 'interval': '1m',
Expand Down Expand Up @@ -123,7 +125,7 @@ def get_days(cls, period, at=None, tzoffset=0):
ats = period.datetimes_strs(start=start, tzoffset=tzoffset)
if '-' in str(period):
start_s, end_s = period.split('-')
period = 'thirty'
period = 'year'
period = cls.get(period)
end = datetime.strptime(end_s, '%m/%d/%Y').replace(hour=0, minute=0,
second=0, microsecond=0)+timedelta(1)-timedelta(seconds=1)
Expand Down Expand Up @@ -209,15 +211,26 @@ def datetimes_strs(self, start=False, end=False, tzoffset=0):
self.datetimes(start=start, end=end, tzoffset=tzoffset))

def flatten(self, dtf=None):
"""
Take a datetime, flatten it to this period,
return M/D/Y H:00:00 or H:MM:00 and etc
Returns None if out of timerange
"""
# Default to now
if not dtf:
dtf = pytznow()
# Or parse the string
if type(dtf) in (str, unicode):
dtf = self.parse_dt_str(dtf)
offset = dtf.tzinfo and dtf.utcoffset().total_seconds()/36 or 0
dts = list(self.datetimes(end=dtf,
tzoffset=offset))
flat = len(dts) and dts[-1] or False
return flat

# Convert UTCOffsetSeconds to "-0700"
_conv = 3600 * 1/100
offset = dtf.tzinfo and dtf.utcoffset().total_seconds()/_conv or 0

# TODO We should be memoizing this method call
dts = list(self.datetimes(end=dtf, tzoffset=offset))

return len(dts) and dts[-1] or False

def flatten_str(self, dtf):
f = self.flatten(dtf)
Expand Down

0 comments on commit 9d482b7

Please sign in to comment.