Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python get current time in right timezone [duplicate]

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=<UTC>) 

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:

Answer

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?
like image 312
luckydonald Avatar asked Sep 14 '14 20:09

luckydonald


People also ask

How do you get the time in a specific timezone Python?

To get the current time of a timezone, you need to create a timezone object by using the pytz. timezone() method and pass it to the datetime. now() method. Then, it'll return the current time of that specific timezone.

How do I get the current time in Python est?

from datetime import datetime from pytz import timezone tz = timezone('EST') datetime. now(tz) ## this returns a datetime object pointing to right now ## according to the timezone info object handed in as the tz variable.

What is Tzlocal in Python?

tzlocal looks for the timezone name in /etc/timezone, /var/db/zoneinfo, /etc/sysconfig/clock and /etc/conf. d/clock. If your /etc/localtime is a symlink it can also extract the name from that symlink. If you need the name of your local time zone, then please make sure your system is properly configured to allow that.


1 Answers

To get the current time in the local timezone as a naive datetime object:

from datetime import datetime naive_dt = datetime.now() 

If it doesn't return the expected time then it means that your computer is misconfigured. You should fix it first (it is unrelated to Python).

To get the current time in UTC as a naive datetime object:

naive_utc_dt = datetime.utcnow() 

To get the current time as an aware datetime object in Python 3.3+:

from datetime import datetime, timezone  utc_dt = datetime.now(timezone.utc) # UTC time dt = utc_dt.astimezone() # local time 

To get the current time in the given time zone from the tz database:

import pytz  tz = pytz.timezone('Europe/Berlin') berlin_now = datetime.now(tz) 

It works during DST transitions. It works if the timezone had different UTC offset in the past i.e., it works even if the timezone corresponds to multiple tzinfo objects at different times.

like image 86
jfs Avatar answered Oct 16 '22 09:10

jfs