Skip to content

Commit

Permalink
clean up timestamp_to_time_number()
Browse files Browse the repository at this point in the history
  • Loading branch information
undeath committed Aug 6, 2021
1 parent 7df0427 commit c2312a4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 11 additions & 4 deletions jmclient/jmclient/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2221,13 +2221,10 @@ def _time_number_to_timestamp(cls, timenumber):
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)
Expand All @@ -2239,6 +2236,16 @@ def timestamp_to_time_number(cls, timestamp):
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
Expand Down
4 changes: 1 addition & 3 deletions jmclient/jmclient/wallet_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c2312a4

Please sign in to comment.