[Tutor] Converting timestamp read as str to "full date and time"
Cameron Simpson
cs at zip.com.au
Sat Sep 20 03:50:13 CEST 2014
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>
If this experiment we're doing works, then I will follow up and push it as
hard as possible. And if it doesn't work, I will write a science-fiction
novel where it does work. It's a win-win situation.
- John Cramer on his experiment for possible cuasality violation
More information about the Tutor
mailing list