Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamespeterschinner committed Dec 21, 2017
1 parent 1b8fd57 commit 8a16e86
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
15 changes: 8 additions & 7 deletions async_v20/definitions/primitives.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pandas as pd
import numpy as np

from .helpers import domain_check

__all__ = ['AcceptDatetimeFormat', 'AccountFinancingMode', 'AccountID', 'AccountUnits', 'CancellableOrderType',
Expand Down Expand Up @@ -27,7 +27,6 @@ class Specifier(object):
pass



class DateTime(Primitive):
"""A date and time value using either RFC3339 or UNIX time representation.
"""
Expand All @@ -44,29 +43,31 @@ def __new__(cls, value, **kwargs):
if not fraction and len(seconds) > 10:
seconds, fraction = value[:10], value[10:]
# value has decimal number
accuracy = len(fraction)
fraction = fraction + '000000000'[:-accuracy]
if fraction:
fraction = fraction + '000000000'[:-len(fraction)]
else:
fraction = '000000000'

value = int(seconds + fraction)
kwargs.update(tz='UTC')


return pd.Timestamp(value, **kwargs)


def _datetime_to_json(self, datetime_format):

if datetime_format == 'RFC3339':
nanoseconds = str(self.nanosecond)
nanoseconds = nanoseconds + '000'[:-len(nanoseconds)]
result = self.strftime(f'%Y-%m-%dT%H:%M:%S.%f{nanoseconds}Z')
elif datetime_format == 'UNIX':
result = str(self.value)
result = f'{result[:-9]}.{result[-9:]}'
result = f'{result[:-9]}.{result[-9:]}'
else:
raise ValueError(f'{datetime_format} is not a valid value. It must be either "RFC3339" or "UNIX"')

return result


pd.Timestamp.json = _datetime_to_json


Expand Down
12 changes: 12 additions & 0 deletions tests/test_definitions/test_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@
from async_v20.definitions.primitives import TransactionID, PriceValue, DecimalNumber
from tests.test_definitions.helpers import get_valid_primitive_data

from time import time
from datetime import datetime

import pandas as pd

def test_date_time_can_be_created_from_time_and_datetime():
assert DateTime(time())
assert DateTime(datetime.now())

def test_date_time_can_be_created_from_integer():
t = time()
assert DateTime(t).second == DateTime(int(t)).second
assert DateTime(t).second == DateTime(str(t).replace('.', '')).second

def test_datetime_converts_between_different_representations():
unix_example = '1502463871.639182000'
rfc3339_example = '2017-08-11T15:04:31.639182000Z'
Expand Down

0 comments on commit 8a16e86

Please sign in to comment.