Search code examples
pythonunix-timestamppython-babel

using unix timestamp generate timedelta for babel's format_timedelta


I am not sure I worded that correctly but python and time always confuses me. This is what I am trying. Given a unix timestamp (INT) which is definitely in the past (can be seconds ago or years ago) I want to generate a babel format_timedelta

My problem is

  1. Babel format_timedelta takes timedelta as first argument
  2. so I guess I need to generate a timedelta using time.time() (now) and the unix timestamp I have.

I can't figure out the part 2 and I believe there must be an easier/correct way to do this. Please share the best possible way to do this, also I need it to be fast in calculating since I have to use it in a web page.

def format_starttime(value, granularity="day"):
 delta = datetime.timedelta(seconds=time.time() - value)
 return format_timedelta(delta, granularity)

gives error in date.format_timedelta() AttributeError: 'module' object has no attribute 'format_timedelta'


Solution

  • import datetime
    
    td = datetime.timedelta(seconds=time.time()-a_unix_timestamp)