Browse Source

clean up timestamp_to_time_number()

master
undeath 4 years ago
parent
commit
c2312a44ea
  1. 15
      jmclient/jmclient/wallet.py
  2. 4
      jmclient/jmclient/wallet_utils.py

15
jmclient/jmclient/wallet.py

@ -2221,13 +2221,10 @@ class FidelityBondMixin(object):
return timegm(datetime(year, month, *cls.TIMELOCK_DAY_AND_SHORTER).timetuple()) return timegm(datetime(year, month, *cls.TIMELOCK_DAY_AND_SHORTER).timetuple())
@classmethod @classmethod
def timestamp_to_time_number(cls, timestamp): def datetime_to_time_number(cls, dt):
""" """
converts a datetime object to a time number 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: if (dt.month - cls.TIMELOCK_EPOCH_MONTH) % cls.TIMENUMBER_UNIT != 0:
raise ValueError() raise ValueError()
day_and_shorter_tuple = (dt.day, dt.hour, dt.minute, dt.second, dt.microsecond) 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") raise ValueError("datetime out of range")
return timenumber 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 @classmethod
def is_timelocked_path(cls, path): def is_timelocked_path(cls, path):
return len(path) > 4 and path[4] == cls.BIP32_TIMELOCK_ID return len(path) > 4 and path[4] == cls.BIP32_TIMELOCK_ID

4
jmclient/jmclient/wallet_utils.py

@ -5,7 +5,6 @@ import os
import sqlite3 import sqlite3
import sys import sys
from datetime import datetime, timedelta from datetime import datetime, timedelta
from calendar import timegm
from optparse import OptionParser from optparse import OptionParser
from numbers import Integral from numbers import Integral
from collections import Counter from collections import Counter
@ -1226,8 +1225,7 @@ def wallet_gettimelockaddress(wallet, locktime_string):
m = FidelityBondMixin.FIDELITY_BOND_MIXDEPTH m = FidelityBondMixin.FIDELITY_BOND_MIXDEPTH
address_type = FidelityBondMixin.BIP32_TIMELOCK_ID address_type = FidelityBondMixin.BIP32_TIMELOCK_ID
lock_datetime = datetime.strptime(locktime_string, "%Y-%m") lock_datetime = datetime.strptime(locktime_string, "%Y-%m")
timenumber = FidelityBondMixin.timestamp_to_time_number(timegm( timenumber = FidelityBondMixin.datetime_to_time_number(lock_datetime)
lock_datetime.timetuple()))
index = timenumber index = timenumber
path = wallet.get_path(m, address_type, index, timenumber) path = wallet.get_path(m, address_type, index, timenumber)

Loading…
Cancel
Save