Skip to content

Commit

Permalink
fix timezone for py27
Browse files Browse the repository at this point in the history
  • Loading branch information
holgern committed Feb 27, 2018
1 parent 3f32e38 commit 9e65837
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
13 changes: 8 additions & 5 deletions beem/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from .blockchainobject import BlockchainObject
from .utils import formatTimeString
from beem.amount import Amount
from datetime import datetime, timedelta, timezone
from datetime import datetime, timedelta
import pytz
from beembase import operations
from beembase.account import PrivateKey, PublicKey
import json
Expand Down Expand Up @@ -149,7 +150,8 @@ def voting_power(self, precision=2, with_regeneration=True):
""" Returns the account voting power
"""
if with_regeneration:
diff_in_seconds = (datetime.utcnow().replace(tzinfo=timezone.utc) - formatTimeString(self["last_vote_time"])).total_seconds()
utc = pytz.timezone('UTC')
diff_in_seconds = (utc.localize(datetime.utcnow()) - formatTimeString(self["last_vote_time"])).total_seconds()
regenerated_vp = diff_in_seconds * 10000 / 86400 / 5 / 100
else:
regenerated_vp = 0
Expand Down Expand Up @@ -384,12 +386,12 @@ def interest(self):
"sbd_interest_rate"] / 100 # percent
interest_amount = (interest_rate / 100) * int(
int(self["sbd_seconds"]) / (60 * 60 * 24 * 356)) * 10**-3

utc = pytz.timezone('UTC')
return {
"interest": interest_amount,
"last_payment": last_payment,
"next_payment": next_payment,
"next_payment_duration": next_payment - datetime.now(timezone.utc),
"next_payment_duration": next_payment - utc.localize(datetime.now()),
"interest_rate": interest_rate,
}

Expand Down Expand Up @@ -421,7 +423,8 @@ def get_bandwidth(self, bandwidth_type=1, account=None, raw_data=False):

total_seconds = 604800
date_bandwidth = formatTimeString(self["last_bandwidth_update"])
seconds_since_last_update = datetime.utcnow().replace(tzinfo=timezone.utc) - date_bandwidth
utc = pytz.timezone('UTC')
seconds_since_last_update = utc.localize(datetime.utcnow()) - date_bandwidth
seconds_since_last_update = seconds_since_last_update.total_seconds()
average_bandwidth = float(self["average_bandwidth"])
used_bandwidth = 0
Expand Down
13 changes: 8 additions & 5 deletions beem/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from builtins import next
import re
import time
from datetime import datetime, timezone
from datetime import datetime, tzinfo
import pytz
import difflib
from .exceptions import ObjectNotInProposalBuffer

Expand All @@ -27,7 +28,8 @@ def formatTimeString(t):
"""
if isinstance(t, datetime):
return t.strftime(timeFormat)
return datetime.strptime(t, timeFormat).replace(tzinfo=timezone.utc)
utc = pytz.timezone('UTC')
return utc.localize(datetime.strptime(t, timeFormat))


def formatTimeFromNow(secs=0):
Expand All @@ -47,7 +49,8 @@ def parse_time(block_time):
"""Take a string representation of time from the blockchain, and parse it
into datetime object.
"""
return datetime.strptime(block_time, timeFormat).replace(tzinfo=timezone.utc)
utc = pytz.timezone('UTC')
return utc.localize(datetime.strptime(block_time, timeFormat))


def assets_from_string(text):
Expand Down Expand Up @@ -108,7 +111,7 @@ def construct_authorperm(*args):
construct_authorperm({'author': 'username',
'permlink': 'permlink'})
"""
username_prefix='@'
username_prefix = '@'
if len(args) == 1:
op = args[0]
author, permlink = op['author'], op['permlink']
Expand Down Expand Up @@ -155,7 +158,7 @@ def construct_authorpermvoter(*args):
construct_authorpermvoter({'author': 'username',
'permlink': 'permlink', 'voter': 'voter'})
"""
username_prefix='@'
username_prefix = '@'
if len(args) == 1:
op = args[0]
if "authorperm" in op:
Expand Down

0 comments on commit 9e65837

Please sign in to comment.