Python datetime to Unix timestamp

Posted by Off Rhoden on Stack Overflow See other posts from Stack Overflow or by Off Rhoden
Published on 2010-05-05T18:38:45Z Indexed on 2010/05/05 19:08 UTC
Read the original article Hit count: 272

Filed under:
|
|

I have to create an "Expires" value 5 minutes in the future, but I have to supply it in UNIX Timestamp format. I have this so far, but it seems like a hack.

def expires():
    '''return a UNIX style timestamp representing 5 minutes from now'''
    epoch = datetime.datetime(1970, 1, 1)
    seconds_in_a_day = 60 * 60 * 24
    five_minutes = datetime.timedelta(seconds=5*60)
    five_minutes_from_now = datetime.datetime.now() + five_minutes
    since_epoch = five_minutes_from_now - epoch
    return since_epoch.days * seconds_in_a_day + since_epoch.seconds

Is there a module or function that does the timestamp conversion for me?

© Stack Overflow or respective owner

Related posts about python

Related posts about datetime