Skip to content

Commit

Permalink
Add new method 'pytool.time.ago()'
Browse files Browse the repository at this point in the history
  • Loading branch information
shakefu committed Aug 3, 2015
1 parent a933199 commit 3003d0c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
60 changes: 60 additions & 0 deletions pytool/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,63 @@ def floor_month(stamp=None):
return datetime.datetime(stamp.year, stamp.month, 1, tzinfo=stamp.tzinfo)


def ago(stamp=None, **kwargs):
""" Return the current time as UTC minutes the specified timeframe.
This is a helper for simplifying the common pattern of
`pytool.time.utcnow() - datetime.timedelta(mintues=15)`.
:param stamp: An optional timestamp instead of `utcnow()`
:param days: Days previous
:param hours: Hours previous
:param minutes: Minutes previous
:param seconds: Days previous
:returns: UTC timestamp
If you like brevity in your arguments, you can use the shorter
versions, `hrs=`, `mins=` and `secs=`.
Or if you really want a short signature, you can use `d=`, `h=`, `m=`,
`s=`.
::
import pytool
yesterday = pytool.time.ago(days=1)
a_little_while_ago = pytool.time.ago(minutes=1)
a_little_before = pytool.time.ago(my_date, hours=1)
# Shorter arguments
shorter = pytool.time.ago(hrs=1, mins=1, secs=1)
# Shorthand argument names
short = pytool.time.ago(d=1, h=1, m=1, s=1)
"""
_map = {
'days': 'days',
'd': 'days',
'hours': 'hours',
'hrs': 'hours',
'h': 'hours',
'minutes': 'minutes',
'mins': 'minutes',
'm': 'minutes',
'seconds': 'seconds',
'secs': 'seconds',
's': 'seconds',
}

args = {}

for key in kwargs:
args[_map[key]] = kwargs[key]

stamp = stamp or utcnow()
stamp -= datetime.timedelta(**args)

return stamp

31 changes: 30 additions & 1 deletion test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from nose import SkipTest

import pytool
from .util import *
from .util import eq_


def test_utc_singleton():
Expand Down Expand Up @@ -227,3 +227,32 @@ def test_timer_mark_works():
utcnow.return_value = datetime(2010, 1, 1, 0, 3, 0)
eq_(t.mark(), timedelta(seconds=2*60))


def test_ago_regular():
unix = 14386000000
stamp = pytool.time.fromutctimestamp(unix)
# Previous stamp minus 1 day, 1 hour, 1 minute and 1 second
ago = unix - (24*60*60) - (60*60) - 60 - 1
ago = pytool.time.fromutctimestamp(ago)

eq_(pytool.time.ago(stamp, days=1, hours=1, minutes=1, seconds=1), ago)


def test_ago_shorter():
unix = 14386000000
stamp = pytool.time.fromutctimestamp(unix)
# Previous stamp minus 1 day, 1 hour, 1 minute and 1 second
ago = unix - (24*60*60) - (60*60) - 60 - 1
ago = pytool.time.fromutctimestamp(ago)

eq_(pytool.time.ago(stamp, days=1, hrs=1, mins=1, secs=1), ago)


def test_ago_shorthand():
unix = 14386000000
stamp = pytool.time.fromutctimestamp(unix)
# Previous stamp minus 1 day, 1 hour, 1 minute and 1 second
ago = unix - (24*60*60) - (60*60) - 60 - 1
ago = pytool.time.fromutctimestamp(ago)

eq_(pytool.time.ago(stamp, d=1, h=1, m=1, s=1), ago)

0 comments on commit 3003d0c

Please sign in to comment.