This question already has answers here:
Python pytz timezone function returns a timezone that is off by 9 minutes
(4 answers)
Weird timezone issue with pytz
(3 answers)
Closed 3 months ago.
Working with pytz in Python. I have a datetime objects that I am trying to convert to different timezones. When printing the UTC Offset, I am not getting a -0500. What's the reason for this?
original_utc = datetime.datetime(1900,6,6,10,40, tzinfo=pytz.timezone('UTC'))
in_eastern = lesson.astimezone(pytz.timezone('America/New_York'))
print("UTC time: ", original_utc.strftime("%Y:%m:%d %H:%M:%S %Z %z"))
print("----------")
print("EST time", in_eastern.strftime("%Y:%m:%d %H:%M:%S %Z %z"))
Gives me:
UTC time: 1900:06:06 10:40:00 UTC +0000
----------
EST time 1900:06:06 05:44:00 LMT -0456
Related
This question already has answers here:
Display the time in a different time zone
(12 answers)
Convert UTC datetime string to local datetime
(16 answers)
Closed 5 months ago.
Date = dt.datetime.today().date()
I have a date here but it's in UTC, for example at PST 2022-09-18 5PM it will turn into 2022-09-19, but I want to convert it into PST time.
You need to use a timezone aware datetime - a naive datetime (like datetime.date) has no timezone information.
So, use a datetime.datetime with a tzinfo, and then you can localize to UTC and convert to Pacific time:
from datetime import datetime
from dateutil import tz
pst = tz.gettz('America/Los_Angeles')
dt = datetime(2016, 1, 1, tzinfo=tz.UTC)
pst_dt = dt.astimezone(pst)
This gives you a datetime representing 2016-01-01 00:00:00 in the Pacific timezone.
You can of course start out with a naive datetime and use replace to add a UTC timezone:
dt = datetime(2016, 1, 1).replace(tzinfo=tz.UTC)
This question already has answers here:
Python pytz timezone function returns a timezone that is off by 9 minutes
(4 answers)
Closed 1 year ago.
If I have this code:
import datetime
import pytz
dt = datetime.datetime(
year=2021,
month=3,
day=3,
hour=11,
minute=30,
second=15,
microsecond=0,
tzinfo=pytz.timezone("Europe/Prague")
)
Then this is the result of dt.timestamp():
1614767535.0
Which translates to Wed Mar 03 2021 10:32:15 GMT+0000 while I would expect 10:30:15 instead.
What's the deal here?
The problem is caused by tzinfo=pytz.timezone("Europe/Prague")
Explanation is here: Python pytz timezone function returns a timezone that is off by 9 minutes
This question already has answers here:
Convert timestamp since epoch to datetime.datetime
(3 answers)
Closed 3 years ago.
I have an epoch time that is in Seconds.PartOfSecond format e.g. 1581218900.17436. How can this be used within time.strftime() ?
import time
curTime = time.time()
# Following is not correct but gives idea what i'm looking for
formattedTime = time.strftime('%A %B %e, %Y %t', curTime)
Update The answer this has been closed for is not imo an obvious replacement: how would one know we need to convert to datetime.datetime? The answer below is more to the point.
You need pass your second to gmtime
import time
s=time.gmtime(1581218900.17436)
time.strftime("%Y-%m-%d %H:%M:%S", s)
'2020-02-09 03:28:20'
time.strftime('%A %B %e, %Y %t', s)
'Sunday February 9, 2020 \t'
This question already has answers here:
Convert timestamps with offset to datetime obj using strptime
(4 answers)
ISO to datetime object: 'z' is a bad directive [duplicate]
(2 answers)
Closed 3 years ago.
I'm trying to format the date of an email using python2.7. The value of date is like
Date: "Tue, 9 Apr 2019 11:49:26 -0400"
Here's the code:
"date": datetime.strptime(d_items['Date'], '%a,%d %b %Y %H:%M:%S %z').strftime('%Y-%m-%d %H:%M:%S')
While stripping the timezone and formatting I'm getting the below error using datetime.datetime function.
'z' is a bad directive in format '%a,%d %b %Y %H:%M:%S %z'
Is there any other way to get the format like below:
2019-04-09 11:49:26
This question already has answers here:
How to parse dates with -0400 timezone string in Python?
(6 answers)
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 6 years ago.
I have two string like these:
"Sun 10 May 2015 13:54:36 -0700"
"Sun 10 May 2015 13:54:36 +0000"
(The last part is time zone -0700 means 7 hours less than GMT)
My intention is turn it to a unix timestamp to find their absolute difference easily.
My Question is is there a built-in method for turning these formated texts to Unix timestamp? Or Is there a one that computes difference of two such strings directly??
Edit:
This Question differs from This one since that problem doesn't include time zones and not any of answers to that question mentioned anything about time zone.
This should work on Python 3 & above but not known to work on all platforms on Python 2.
date_str = "Sun 10 May 2015 13:54:36 -0700"
pattern = '%a %d %B %Y %H:%M:%S %z'
dt = int(time.mktime(time.strptime(date_str ,pattern )))
print(dt)
For, Python 2.7+, (without %z)
import datetime
date_str = "Sun 10 May 2015 13:54:36 -0700"
dt_str = date_str[:-5].strip()
dt_zone = int(date_str[-5:])/100
pattern = '%a %d %B %Y %H:%M:%S'
dtobj = datetime.datetime.strptime(dt_str ,pattern)
dt = dtobj + datetime.timedelta(hours=dt_zone)
print( dt.strftime('%s') )
This code answer to your question:
from datetime import timedelta, datetime, tzinfo
date1="Sun 10 May 2015 13:54:36 -0700"
date2=date1[4:-6]
zone = int(date1[-5:])/100
d=datetime.strptime(date2, "%d %B %Y %H:%M:%S")
class TZ(tzinfo):
def utcoffset(self, dt): return timedelta(hours=zone)
d=datetime(d.year, d.month, d.day,d.hour,d.minute,d.second, tzinfo=TZ()).isoformat(' ')
print d