Skip to content
Snippets Groups Projects
Commit ead017d3 authored by roadscape's avatar roadscape
Browse files

fix tz issues, add ts tests

parent abca60bf
No related branches found
No related tags found
No related merge requests found
"""Block scheduler.""" """Block scheduler."""
import logging import logging
from time import time, sleep from time import time, sleep
from pytz import utc from hive.utils.normalize import block_date, utc_timestamp
from hive.utils.normalize import block_date
from hive.utils.stats import Stats from hive.utils.stats import Stats
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -72,7 +71,7 @@ class BlockSchedule: ...@@ -72,7 +71,7 @@ class BlockSchedule:
we can identify this case by comparing current time to latest we can identify this case by comparing current time to latest
received block time.""" received block time."""
if num == self._head_num: if num == self._head_num:
gap = time() - date.replace(tzinfo=utc).timestamp() gap = time() - utc_timestamp(date)
assert gap > -60, 'system clock is %ds behind chain' % gap assert gap > -60, 'system clock is %ds behind chain' % gap
if gap > 60: if gap > 60:
raise StaleHeadException("chain gap is %fs" % gap) raise StaleHeadException("chain gap is %fs" % gap)
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import math import math
import decimal import decimal
from datetime import datetime from datetime import datetime
from pytz import utc
import ujson as json import ujson as json
NAI_MAP = { NAI_MAP = {
...@@ -73,6 +74,10 @@ def parse_time(block_time): ...@@ -73,6 +74,10 @@ def parse_time(block_time):
"""Convert chain date into datetime object.""" """Convert chain date into datetime object."""
return datetime.strptime(block_time, '%Y-%m-%dT%H:%M:%S') return datetime.strptime(block_time, '%Y-%m-%dT%H:%M:%S')
def utc_timestamp(date):
"""Convert datetime to UTC unix timestamp."""
return date.replace(tzinfo=utc).timestamp()
def load_json_key(obj, key): def load_json_key(obj, key):
"""Given a dict, parse JSON in `key`. Blank dict on failure.""" """Given a dict, parse JSON in `key`. Blank dict on failure."""
if not obj[key]: if not obj[key]:
......
...@@ -5,7 +5,7 @@ import math ...@@ -5,7 +5,7 @@ import math
import ujson as json import ujson as json
from funcy.seqs import first from funcy.seqs import first
from hive.utils.normalize import sbd_amount, rep_log10, safe_img_url, parse_time from hive.utils.normalize import sbd_amount, rep_log10, safe_img_url, parse_time, utc_timestamp
def post_basic(post): def post_basic(post):
...@@ -99,7 +99,7 @@ def post_payout(post): ...@@ -99,7 +99,7 @@ def post_payout(post):
csvotes = "\n".join(map(_vote_csv_row, post['active_votes'])) csvotes = "\n".join(map(_vote_csv_row, post['active_votes']))
# trending scores # trending scores
_timestamp = parse_time(post['created']).timestamp() _timestamp = utc_timestamp(parse_time(post['created']))
sc_trend = _score(rshares, _timestamp, 480000) sc_trend = _score(rshares, _timestamp, 480000)
sc_hot = _score(rshares, _timestamp, 10000) sc_hot = _score(rshares, _timestamp, 10000)
......
...@@ -12,6 +12,7 @@ from hive.utils.normalize import ( ...@@ -12,6 +12,7 @@ from hive.utils.normalize import (
amount, amount,
legacy_amount, legacy_amount,
parse_time, parse_time,
utc_timestamp,
load_json_key, load_json_key,
trunc, trunc,
rep_log10, rep_log10,
...@@ -56,6 +57,15 @@ def test_parse_time(): ...@@ -56,6 +57,15 @@ def test_parse_time():
block_time = '2018-06-22T20:34:30' block_time = '2018-06-22T20:34:30'
assert parse_time(block_time) == datetime(2018, 6, 22, 20, 34, 30) assert parse_time(block_time) == datetime(2018, 6, 22, 20, 34, 30)
def test_utc_timestamp():
assert utc_timestamp(parse_time('1970-01-01T00:00:00')) == 0
assert utc_timestamp(parse_time('1970-01-01T00:00:01')) == 1
block_time = '2018-06-22T20:34:30'
date = parse_time(block_time)
timestamp = utc_timestamp(date)
assert timestamp == 1529699670
def test_load_json_key(): def test_load_json_key():
obj = {'profile':'{"foo":"bar"}'} obj = {'profile':'{"foo":"bar"}'}
loaded = load_json_key(obj, 'profile') loaded = load_json_key(obj, 'profile')
......
...@@ -124,10 +124,10 @@ def test_post_legacy(): ...@@ -124,10 +124,10 @@ def test_post_legacy():
def test_post_payout(): def test_post_payout():
ret = post_payout(POST_1) ret = post_payout(POST_1)
expect = {'payout': Decimal('0.044'), expect = {'payout': Decimal('0.044'),
'rshares': 0, 'rshares': 2731865444,
'csvotes': 'test-safari,1506388632,10000,49.03\ndarth-cryptic,110837437,200,49.23\ntest25,621340000,10000,25\nmysqlthrashmetal,493299375,10000,41.02', 'csvotes': 'test-safari,1506388632,10000,49.03\ndarth-cryptic,110837437,200,49.23\ntest25,621340000,10000,25\nmysqlthrashmetal,493299375,10000,41.02',
'sc_trend': 3120.81673125, 'sc_trend': 3123.215690554685,
'sc_hot': 149799.2031} 'sc_hot': 149799.83955930467}
assert ret == expect assert ret == expect
def test_post_stats(): def test_post_stats():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment