From 9e65837c1e14e9fad95b6786fe1852a43abc23a0 Mon Sep 17 00:00:00 2001 From: Holger Nahrstaedt Date: Tue, 27 Feb 2018 13:04:33 +0100 Subject: [PATCH] fix timezone for py27 --- beem/account.py | 13 ++++++++----- beem/utils.py | 13 ++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/beem/account.py b/beem/account.py index b9e223ce..d0610c5d 100644 --- a/beem/account.py +++ b/beem/account.py @@ -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 @@ -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 @@ -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, } @@ -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 diff --git a/beem/utils.py b/beem/utils.py index 404021ed..7884bd4b 100644 --- a/beem/utils.py +++ b/beem/utils.py @@ -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 @@ -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): @@ -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): @@ -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'] @@ -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: