Question

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'

Was it helpful?

Solution

import datetime

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

OTHER TIPS

Difference between two datetime instances is a timedelta instance.

from datetime import datetime
from babel.dates import format_timedelta
delta = datetime.now() - datetime.fromtimestamp(your_timestamp)
print format_timedelta(delta, locale='en_US')

See datetime module documentation for details and more examples.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top