Skip to content

Commit

Permalink
crude but effective time parser test
Browse files Browse the repository at this point in the history
  • Loading branch information
leifj committed Sep 9, 2014
1 parent 0caac99 commit 3442c77
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/pyff/stats.py
Expand Up @@ -10,7 +10,8 @@
__author__ = 'leifj'

# Initialize the repository
if not hasattr(logging, 'statistics'): logging.statistics = {}
if not hasattr(logging, 'statistics'):
logging.statistics = {}
# Initialize my namespace
stats = logging.statistics.setdefault('pyFF Statistics', {})
# Initialize my namespaces scalars and collections
Expand Down
39 changes: 39 additions & 0 deletions src/pyff/test/test_time.py
@@ -0,0 +1,39 @@
from unittest import TestCase
from pyff.utils import tdelta, duration2timedelta, total_seconds, iso_now, iso2datetime, iso_fmt, totimestamp
from datetime import datetime

class TestDuration(TestCase):

DURATIONS = [
('PT1H', 3600),
('PT1S', 1),
('P1DT1S', 86401),
('P1YT2M', 31536120)
]

def test_duration2timedelta(self):
for expr, secs in TestDuration.DURATIONS:
td = duration2timedelta(expr)
print "timedelta: %s" % td
print "duration: %s" % expr
print "expected seconds: %s" % secs
assert(int(td.total_seconds()) == secs)
assert(int(total_seconds(td)) == secs)


class TestISO(TestCase):

def test_isonow(self):
now = iso_now()
assert(now is not None)
assert(now.endswith('Z'))

def test_iso2datetime(self):
now = datetime.now()
now = now.replace(tzinfo=None)
iso = iso_fmt(totimestamp(now))
other_now = iso2datetime(iso)
other_now = other_now.replace(tzinfo=None)
print now
print other_now
assert ((other_now - now).total_seconds() < 0.1)
9 changes: 8 additions & 1 deletion src/pyff/utils.py
Expand Up @@ -162,7 +162,14 @@ def iso_now():
"""
Current time in ISO format
"""
return strftime("%Y-%m-%dT%H:%M:%SZ", gmtime())
return iso_fmt()


def iso_fmt(tstamp=None):
"""
Timestamp in ISO format
"""
return strftime("%Y-%m-%dT%H:%M:%SZ", gmtime(tstamp))


def iso2datetime(s):
Expand Down

0 comments on commit 3442c77

Please sign in to comment.