[Tutor] Converting timestamp read as str to "full date and time"

Juan Christian juan0christian at gmail.com
Sat Sep 20 01:46:57 CEST 2014


On Fri, Sep 19, 2014 at 8:22 PM, Danny Yoo <dyoo at hashcollision.org> wrote:

> Huh.  I should have read your message more closely.  Sorry; it's been
> one of those days for me, it seems...
>
> Anyway, can you use ctime()?  It looks like that might be almost right:
>
>     >>> t = 1411167038
>     >>> datetime.datetime.utcfromtimestamp(t).ctime()
>     'Fri Sep 19 22:50:38 2014'
>

The thing is that I get the timestamp as string and I need to print it
using something like this: print("Random text here: {}".format(my_date))

Real example:

API returns ->
http://backpack.tf/api/IGetUsers/v3/?steamids=76561198052911020

I get 'backpack_update' > '440' correctly, no problems and, opsss, my bad,
the timestamp is int, not string. I did some debug and I was wrong.

<class 'int'>
1411044719

So, what I need is to get this 1411044719 (int) and convert to something
like this 'Fri Sep 19 22:50:38 2014' and print it using format()

The line I'll print is like that: print('\t\tBACKPACK VALUE: {} ( {}
)'.format(status['backpack_value'], status['backpack_update']))

status['backpack_value'] is 100% OK
status['backpack_update'] is the guy...



> though if you really do want more control over what's being printed,
> yes, strftime() is what's you're looking for, I think:
>
>     >>> t = 1411167038
>     >>> datetime.datetime.utcfromtimestamp(t).strftime("%a, %d %b %Y
> %H:%M:%S")
>     'Fri, 19 Sep 2014 22:50:38'
>
> https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140919/61b2bf1e/attachment-0001.html>


More information about the Tutor mailing list