I am using the telethon library to making telegram bots.
when I using event.date it was printing the time on +00.00 time zone.
How can I print time on specific timezone
See python - Convert UTC datetime string to local datetime. Stealing the top answer which uses python-dateutil:
from dateutil import tz
local_datetime = event.date.astimezone(tz.tzlocal())
from datetime import datetime
print(datetime.now().isoformat(timespec='minutes'))
have a look in datetime library
Related
I would like to get the EST time with Python. I have the following code:
import datetime
from pytz import timezone
import time
now_EST = datetime.datetime.today().astimezone(timezone('EST'))
print(now_EST)
And the output is:
2022-03-29 09:52:55.130992-05:00
But when I google the EST time zone, I find out that the time right now is 10:52 am EST, which essentially is the right time.
Why does my code show the 1 hour earlier time compared to the correct one?
use a proper IANA time zone name to avoid ambiguities of the abbreviations.
from datetime import datetime
import pytz
print(datetime.now(pytz.timezone("America/New_York")))
# 2022-03-29 11:20:30.917144-04:00
If you happen to use Python 3.9 or higher, use the built-in zoneinfo module to set the time zone (pytz is deprecated):
from datetime import datetime
from zoneinfo import ZoneInfo
print(datetime.now(ZoneInfo("America/New_York")))
# 2022-03-29 11:20:30.917144-04:00
Daylight Saving Time. Try "EST5EDT"
This question already has answers here:
Python datetime object show wrong timezone offset
(2 answers)
Closed 5 years ago.
Converting a timezone naive date time to a specific timezone gives a completely incorrect result.
import dateutil as du
import pytz
du.parser.parse('2017-05-31T15:00:00').replace(tzinfo=pytz.timezone('Europe/London')).isoformat()
returns a one minute not one hour offset vs UTC
'2017-05-31T15:00:00-00:01'
I've seen a few datetime peculiarities before but this one is breathtaking.
The main problem here is that you are using a pytz time zone. pytz zones do not follow the tzinfo interface and cannot be simply attached to datetime objects (either through the constructor or through replace). If you would like to use pytz time zones, you should use pytz.timezone.localize with a naive datetime. If the datetime is already timezone-aware, you can use datetime.astimezone to convert it between zones.
from dateutil import parser
import pytz
LON = pytz.timezone('Europe/London')
dt = parser.parse('2017-05-31T15:00:00')
dt = LON.localize(dt)
print(dt) # 2017-05-31 15:00:00+01:00
This is because pytz's interface uses localize to attach a static time zone to a datetime. For the same reason, if you do arithmetic on the now-localized datetime object, it may give similar improper results and you'll have to use pytz.timezone.normalize to fix it. The reason this is done this way is that, historically, it has not been possible to handle ambiguous datetimes using a Pythonic tzinfo interface, which changed with PEP 495 in Python 3.6, making pytz's workaround less necessary.
If you would like to pass a tzinfo to a datetime using replace or the constructor, or you would prefer to use the pythonic interface, dateutil's time zone suite implements a PEP 495-compliant tzinfo interface. The equivalent using a dateutil zone is:
from dateutil import parser
from dateutil import tz
LON = tz.gettz('Europe/London')
dt = parser.parse('2017-05-31T15:00:00').replace(tzinfo=LON)
print(dt) # 2017-05-31 15:00:00+01:00
I have often had bad luck using replace() with tzinfo objects. I have however found this construct to be reliable:
Code:
def naive_to_aware(ts, tz):
return tz.localize(ts)
Update from Comments:
From the (pytz DOCS)
Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones.
It is safe for timezones without daylight saving transitions though, such as UTC
So it is not just bad luck, it is problematic for pytz objects with timezones having DST.
Test Code:
import dateutil as du
import pytz
print(naive_to_aware(du.parser.parse('2017-05-31T15:00:00'),
pytz.timezone('Europe/London')).isoformat())
Results:
2017-05-31T15:00:00+01:00
I'm trying to convert a UNIX time stamp to UTC+9. I've been searching for hours and it's all very confusing what with the different libraries etc
Here's what I've got so far
from datetime import datetime
from pytz import timezone
import datetime
time = 1481079600
utc_time = datetime.datetime.fromtimestamp(time)#.strftime('%Y-%m-%d %H:%M:%S')
print utc_time.strftime(fmt)
tz = timezone('Japan')
print tz.localize(utc_time).strftime(fmt)
This just prints the same time, what am I doing wrong
I am going to shamelessly plug this new datetime library I am obsessed with, Pendulum.
pip install pendulum
import pendulum
t = 1481079600
pendulum.from_timestamp(t).to_datetime_string()
>>> '2016-12-07 03:00:00'
And now to change it to your timezone super quick and easy!
pendulum.from_timestamp(t, 'Asia/Tokyo').to_datetime_string()
>>> '2016-12-07 12:00:00'
Your utc_time datetime is naive - it has no timezone associated with it. localize assigns a timezone to it, it doesn't convert between timezones. The simplest way to do that is probably to construct a timezone-aware datetime:
import pytz
utc_time = datetime.datetime.fromtimestamp(time, pytz.utc)
Then convert to the timezone you want when you're ready to display it:
print utc_time.astimezone(tz).strftime(fmt)
I have a script with TwitterSearch using different variables, so that I can convert the retrieved tweets in local time.
However, when I try to add the set_until condition, it begins to search at 8pm instead of midnight (the difference between UTC and my timezone). Can I force it to begin 4 hours later?
Here are the relevant portions of my code :
from time import strftime
from email.utils import parsedate, parsedate_tz, mktime_tz
from datetime import datetime, date
import pytz
The timestamp conversion
timestamp = mktime_tz(parsedate_tz(tweet_time_string))
return datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
The search criteria
tso.set_until(date(2015,04,18))
Thank you in advance!
Facebook returns 'created_time' in this format:
2012-07-23T08:52:04+0000
I want to convert this timestamp to a normal Python DateTime object.
Have you tried dateutil
It's extremely easy to use
import dateutil.parser as dateparser
dateparser.parse('2012-07-23T08:52:04+0000')
dateutil is very helpful to deal with timezone info, and it can handle lots of time formats.
s = "2005-12-06T12:13:14"
from datetime import datetime
from time import strptime
print datetime(*strptime(s, "%Y-%m-%dT%H:%M:%S")[0:6])