From c2312a44ea8224095d21a69eb1aa9b16d75800f6 Mon Sep 17 00:00:00 2001 From: undeath Date: Tue, 3 Aug 2021 17:40:11 +0200 Subject: [PATCH] clean up timestamp_to_time_number() --- jmclient/jmclient/wallet.py | 15 +++++++++++---- jmclient/jmclient/wallet_utils.py | 4 +--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/jmclient/jmclient/wallet.py b/jmclient/jmclient/wallet.py index 24c74f3..216bbfd 100644 --- a/jmclient/jmclient/wallet.py +++ b/jmclient/jmclient/wallet.py @@ -2221,13 +2221,10 @@ class FidelityBondMixin(object): return timegm(datetime(year, month, *cls.TIMELOCK_DAY_AND_SHORTER).timetuple()) @classmethod - def timestamp_to_time_number(cls, timestamp): + def datetime_to_time_number(cls, dt): """ converts a datetime object to a time number """ - #workaround for the year 2038 problem on 32 bit systems - #see https://stackoverflow.com/questions/10588027/converting-timestamps-larger-than-maxint-into-datetime-objects - dt = datetime.utcfromtimestamp(0) + timedelta(seconds=timestamp) if (dt.month - cls.TIMELOCK_EPOCH_MONTH) % cls.TIMENUMBER_UNIT != 0: raise ValueError() day_and_shorter_tuple = (dt.day, dt.hour, dt.minute, dt.second, dt.microsecond) @@ -2239,6 +2236,16 @@ class FidelityBondMixin(object): raise ValueError("datetime out of range") return timenumber + @classmethod + def timestamp_to_time_number(cls, timestamp): + """ + converts a unix timestamp to a time number + """ + #workaround for the year 2038 problem on 32 bit systems + #see https://stackoverflow.com/questions/10588027/converting-timestamps-larger-than-maxint-into-datetime-objects + dt = datetime.utcfromtimestamp(0) + timedelta(seconds=timestamp) + return cls.datetime_to_time_number(dt) + @classmethod def is_timelocked_path(cls, path): return len(path) > 4 and path[4] == cls.BIP32_TIMELOCK_ID diff --git a/jmclient/jmclient/wallet_utils.py b/jmclient/jmclient/wallet_utils.py index 111ce6e..68fc236 100644 --- a/jmclient/jmclient/wallet_utils.py +++ b/jmclient/jmclient/wallet_utils.py @@ -5,7 +5,6 @@ import os import sqlite3 import sys from datetime import datetime, timedelta -from calendar import timegm from optparse import OptionParser from numbers import Integral from collections import Counter @@ -1226,8 +1225,7 @@ def wallet_gettimelockaddress(wallet, locktime_string): m = FidelityBondMixin.FIDELITY_BOND_MIXDEPTH address_type = FidelityBondMixin.BIP32_TIMELOCK_ID lock_datetime = datetime.strptime(locktime_string, "%Y-%m") - timenumber = FidelityBondMixin.timestamp_to_time_number(timegm( - lock_datetime.timetuple())) + timenumber = FidelityBondMixin.datetime_to_time_number(lock_datetime) index = timenumber path = wallet.get_path(m, address_type, index, timenumber)