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

Juan Christian juan0christian at gmail.com
Sat Sep 20 04:35:42 CEST 2014


On Fri, Sep 19, 2014 at 10:50 PM, Cameron Simpson <cs at zip.com.au> wrote:

> On 19Sep2014 20:46, Juan Christian <juan0christian at gmail.com> wrote:
>
>> On Fri, Sep 19, 2014 at 8:22 PM, Danny Yoo <dyoo at hashcollision.org>
>> wrote:
>>    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))
>>
>
>     # here's your timestamp as a string, however obtained
>     t = <timestamp as string>
>
>     # just an int, convert it to an int
>     seconds_since_epoch = int(t)
>
>     # get a <datetime> from the timestamp
>     dt = datetime.datetime.utcfromtimestamp(t)
>
>     # write the <datetime> out in desired format as a string
>     my_date = dt.strftime("%a, %d %b %Y %H:%M:%S")
>
>     # use the formatted string
>     print("Random text here: {}".format(my_date))
>
> Seriously, keep that steps all broken out separately like the above.
> Easier to read, easier to debug, easier to move relevant parts off into
> utility functions later when you need to do this from several places in
> your code.
>
> Cheers,
> Cameron Simpson <cs at zip.com.au>


You should read the last posts before posting. (:

The problem was already solved, and as I said, it was already an int, not a
str as I wrongly said on the title!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140919/f277b54b/attachment.html>


More information about the Tutor mailing list