Wednesday 1 November 2017

Python get current time in right timezone

Right now I
use



import
datetime
print(datetime.datetime.now().strftime("%X"))


to
display the current time as a string.
Problem is, my computer is running in
Europe/Berlin time zone, and the offset of +2 to UTC is not
accounted here.
Instead of 19:22:26 it should display
21:22:26

Also different to the other
answers I found here, I do not store it by
calling



datetime.datetime(2014, 7,
10, 18, 44, 59, 193982,
tzinfo=)


but



datetime.datetime.now()



so
I tried (and failed) the
following:



>>> from pytz
import timezone
>>>
datetime.datetime.now().astimezone(timezone('Europe/Berlin'))
ValueError:
astimezone() cannot be applied to a naive
datetime




Edit:






Can't
post as answer, as this question is marked
closed



The server I had this issue
with doesn't exists any longer.
Anyway, here are some other things worth
checking:




  • Is the timezone
    of your server/system set up
    correctly?



    • VMs or docker
      containers might be out of sync with the host, that's worth
      checking.


  • Is the time on
    that computer correct? You don't ended up with +2 hours after changing the
    timezone?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...