Skip to content

Commit

Permalink
Use utc timezone in strptime datetime objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed Jul 9, 2024
1 parent 2a40357 commit 88020c3
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
4 changes: 2 additions & 2 deletions bitcoinlib/services/bitgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#

import logging
from datetime import datetime
from datetime import datetime, timezone
from bitcoinlib.main import MAX_TRANSACTIONS
from bitcoinlib.services.baseclient import BaseClient, ClientError
from bitcoinlib.transactions import Transaction
Expand Down Expand Up @@ -72,7 +72,7 @@ def getutxos(self, address, after_txid='', limit=MAX_TRANSACTIONS):
'size': 0,
'value': int(round(utxo['value'] * self.units, 0)),
'script': utxo['script'],
'date': datetime.strptime(utxo['date'], "%Y-%m-%dT%H:%M:%S.%fZ")
'date': datetime.strptime(utxo['date'], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc)
}
)
total = res['total']
Expand Down
4 changes: 2 additions & 2 deletions bitcoinlib/services/blockchair.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def getutxos(self, address, after_txid='', limit=MAX_TRANSACTIONS):
'size': 0,
'value': utxo['value'],
'script': utxo['script_hex'],
'date': datetime.strptime(utxo['time'], "%Y-%m-%d %H:%M:%S")
'date': datetime.strptime(utxo['time'], "%Y-%m-%d %H:%M:%S").replace(tzinfo=timezone.utc)
})
if not len(res['data']) or len(res['data']) < REQUEST_LIMIT:
break
Expand All @@ -114,7 +114,7 @@ def gettransaction(self, tx_id):
input_total = tx['input_total']
t = Transaction(locktime=tx['lock_time'], version=tx['version'], network=self.network,
fee=tx['fee'], size=tx['size'], txid=tx['hash'],
date=None if not confirmations else datetime.strptime(tx['time'], "%Y-%m-%d %H:%M:%S"),
date=None if not confirmations else datetime.strptime(tx['time'], "%Y-%m-%d %H:%M:%S").replace(tzinfo=timezone.utc),
confirmations=confirmations, block_height=tx['block_id'] if tx['block_id'] > 0 else None,
status=status, input_total=input_total, coinbase=tx['is_coinbase'],
output_total=tx['output_total'], witness_type=witness_type)
Expand Down
6 changes: 3 additions & 3 deletions bitcoinlib/services/blockcypher.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def getutxos(self, address, after_txid='', limit=MAX_TRANSACTIONS):
tdate = None
if 'confirmed' in tx:
try:
tdate = datetime.strptime(tx['confirmed'], "%Y-%m-%dT%H:%M:%SZ")
tdate = datetime.strptime(tx['confirmed'], "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc)
except ValueError:
tdate = datetime.strptime(tx['confirmed'], "%Y-%m-%dT%H:%M:%S.%fZ")
tdate = datetime.strptime(tx['confirmed'], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=timezone.utc)
transactions.append({
'address': address.address_orig,
'txid': tx['tx_hash'],
Expand All @@ -94,7 +94,7 @@ def gettransaction(self, txid):
t = Transaction.parse_hex(tx['hex'], strict=self.strict, network=self.network)
if tx['confirmations']:
t.status = 'confirmed'
t.date = datetime.strptime(tx['confirmed'][:19], "%Y-%m-%dT%H:%M:%S")
t.date = datetime.strptime(tx['confirmed'][:19], "%Y-%m-%dT%H:%M:%S").replace(tzinfo=timezone.utc)
else:
t.status = 'unconfirmed'
t.confirmations = tx['confirmations']
Expand Down
5 changes: 3 additions & 2 deletions bitcoinlib/services/blocksmurfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def getutxos(self, address, after_txid='', limit=MAX_TRANSACTIONS):
'size': u['size'],
'value': u['value'],
'script': u['script'],
'date': datetime.strptime(u['date'][:19], "%Y-%m-%dT%H:%M:%S")
'date': datetime.strptime(u['date'][:19], "%Y-%m-%dT%H:%M:%S").replace(tzinfo=timezone.utc)
})
return utxos[:limit]

Expand All @@ -86,7 +86,8 @@ def _parse_transaction(self, tx, block_height=None):
self.latest_block = self.blockcount() if not self.latest_block else self.latest_block
confirmations = self.latest_block - block_height
# FIXME: Blocksmurfer returns 'date' or 'time', should be consistent
tx_date = None if not tx.get('date') else datetime.strptime(tx['date'], "%Y-%m-%dT%H:%M:%S")
tx_date = None if not tx.get('date') else (
datetime.strptime(tx['date'], "%Y-%m-%dT%H:%M:%S").replace(tzinfo=timezone.utc))
if not tx_date and 'time' in tx:
tx_date = datetime.fromtimestamp(tx['time'], timezone.utc)
t = Transaction(locktime=tx['locktime'], version=tx['version'], network=self.network,
Expand Down

0 comments on commit 88020c3

Please sign in to comment.