Convert Time to printable or localtime format
Question: In my python code I get start and end time some thing like:

    
    
    end = int(time.time())
    start = end - 1800
    

Now start and end variables holds values like 1460420758 and 1460422558.

I am trying to convert it in a meaningful format like :

    
    
    Mon Apr 11 17:50:25 PDT 2016
    

But am unable to do so, I tried:

    
    
    time.strftime("%a %b %d %H:%M:%S %Y", time.gmtime(start))
    

Gives me

    
    
    Tue Apr 12 00:25:58 2016
    

But not only the timezone but the H:M:S are wrong

As date returns me the below information:

    
    
    $ date
    Mon Apr 11 18:06:27 PDT 2016
    

How to correct it?


Answer: This one involves utilizing datetime to great the format you wish with the
strftime module.

What's important is that the time information you get 'MUST' be UTC in order
to do this. Otherwise, you're doomed D:

I'm using timedelta to 'add' hours to the time. It will also increments the
date, too. I still would recommend using the module I shared above to handle
time zones.

    
    
    import time
    
    # import datetime so you could play with time
    import datetime
    
    print int(time.time())
    date = time.gmtime(1460420758)
    
    # Transform time into datetime
    new_date = datetime.datetime(*date[:6])
    
    new_date = new_date + datetime.timedelta(hours=8)
    
    # Utilize datetime's strftime and manipulate it to what you want
    print new_date.strftime('%a %b %d %X PDT %Y')
    

