In a web application I’m writing for an existing database I need to calculate the difference between now and a timestamp stored in the database (in a text field, it’s stupid, I know). Here’s my sqlalchemy Ban class and the relevant method.
class Ban(base):
__tablename__= 'bans'
id= Column('banID', Integer, primary_key=True)
unban_timestamp= Column('UNBAN', Text)
banned_steamid= Column('ID', Text, ForeignKey('rp_users.steamid'))
banned_name= Column('NAME', Text)
banner_steamid= Column('BANNER_STEAMID', Text, ForeignKey('rp_users.steamid'))
banner_name= Column('BANNER', Text)
reason= Column('REASON', Text)
def unbanned_in(self, mode):
if self.unban_timestamp == '0':
return 'Never'
else:
now= datetime.datetime.utcnow()
time= datetime.datetime.fromtimestamp(int(self.unban_timestamp))
if now > time:
return 'Expired'
else:
remaining= time - now
if mode=='readable':
return remaining
elif mode=='int':
return str(remaining.seconds).zfill(10)
I need both the integer and the pretty string representations because I’ll be presenting this in a html table and javascript needs a simple way to sort it. The problem I’m facing is that the integers and strings are not matching up, as you can see in this screenshot here:

if anyone can make sense of why the output is so screwed up that would be appreciated! if there’s any more information that you need to answer my question I’ll gladly add it.
edit
for the record at the top of the screenshot the unbanned_in timestamp is 1320247970 if I run that through my function this is the result I get
>>> ban = session.query(db.Ban).filter_by(id=3783).one()
>>> print ban.unbanned_in('int'), ban.unbanned_in('readable')
0000049044 2 days, 13:37:24.179045
If you want to get the number of seconds between
timeandnow, useremaining.days * 24 * 3600 + remaining.secondsinstead of just
remaining.seconds