8

I'd like to a convert unix timestamp I have in a string (ex. 1277722499.82) into a more humanized format (hh:mm:ss or similar). Is there an easy way to do this in python for a django app? This is outside of a template, in the model that I would like to do this. Thanks.

edit I'm using the python function time.time() to generate the timestamp. According to the doc:

time.time()

Return the time as a floating point number expressed in seconds since the epoch, in UTC. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.

2
  • The decimal places in your example look a bit odd - Unix timestamp is usually an integer count of seconds since 1-Jan-1970 isn't it? Commented Jun 28, 2010 at 15:06
  • @joefis: the fractional part means (or should mean) the fraction of the second. Commented Jun 28, 2010 at 15:20

1 Answer 1

19
import datetime
datestring = "1277722499.82"

dt = datetime.datetime.fromtimestamp(float(datestring))
print(dt)
2010-06-28 11:54:59.820000
    
Sign up to request clarification or add additional context in comments.

1 Comment

Or presumably, if you're working with timezones, .utcfromtimestamp().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.