This question already has answers here:
Comparing dates in Python - how to handle time zone modifiers
(3 answers)
Closed 10 years ago.
I have timestamps in the following format
"Sat Feb 06 07:00:13 -0800 2010"
What is the idiomatic/pythonic way to convert this kind of timestamp into tz aware datetime instance? ... I've fiddled with the single parts of the timestamp but looks all ugly
You can't parse time zones with datetime. But you can use the dateutil.parser module instead, as shown below. It returns a datetime.datetime object.
>>> dateutil.parser.parse('Sat Feb 06 07:00:13 -0800 2010')
datetime.datetime(2010, 2, 6, 7, 0, 13, tzinfo=tzoffset(None, -28800))
And to be fair, see this complete answer.
Related
This question already has answers here:
Convert string to datetime in python and compare to current time
(2 answers)
Closed 5 months ago.
I have this string Thu 15 Sep 2022 11:43:49 PM UTC and I want the difference in seconds between it and the current time.
How do I accomplish this in python?
You would use the datetime.strptime function from the datetime module (don't be confused, you import datetime from datetime and then call datetime.strptime--I know). You'd apply to that the correct date-string formatting.
Here's a good overview of the different formatting elements you can use with strptime.
I've made a few assumptions about zero-padding numbers (e.g., would 1 p.m. be 01:00 or 1:00?), but with that in mind, the following should work:
from datetime import datetime
datetime_str = "Thu 15 Sep 2022 11:43:49 PM UTC"
fmt = r"%a %d %b %Y %H:%M:%S %p %Z"
parsed_datetime = datetime.strptime(datetime_str, fmt)
seconds_diff = (parsed_datetime - datetime.now()).seconds
This question already has answers here:
Python datetime strptime() does not match format
(4 answers)
Closed 1 year ago.
I have timestamps of the format "25-Oct-20 1:00:00 AM GMT" and "25-Oct-20 12:00:00 AM BST" and I wish to parse them into a timezone aware datetime. I have tried using the strptime format "%d-%b-%y %I:%M:%S %p %Z"
However the %Z argument of datetime.strptime does not recognize BST as a valid input, and I can not figure out the "correct" way to handle parsing daylight savings times.
If you look at the error from dateutil, it says something like:
UnknownTimezoneWarning: tzname BST identified but not understood.
Pass `tzinfos` argument in order to correctly return a timezone-aware
datetime. In a future version, this will raise an exception.
In other words, you can pass in a dictionary that maps timezone aliases (like "BST") to appropriate timezone information. Maybe something like:
>>> import dateutil.parser
>>> import dateutil.tz
>>> BST = dateutil.tz.gettz('Europe/London')
>>> dateutil.parser.parse('25-Oct-20 12:00:00 AM BST', tzinfos={'BST': BST})
datetime.datetime(2020, 10, 25, 0, 0, tzinfo=tzfile('/usr/share/zoneinfo/Europe/London'))
This question already has answers here:
Converting unix timestamp string to readable date
(19 answers)
Closed 7 years ago.
The last.fm API using user.getRecentTracks method's response supplies a date in the following format:
"date": {
"#text": "11 Dec 2015, 01:41",
"uts": "1449798068"
},
What is this "uts" field and how do I convert it into datetime string for a MySql database in python? Would your suggested answer be more efficient than using datetime.datetime.strptime() method to convert the text string given?
uts looks like a timestamp. The abbreviation probably stands for UTC timestamp or Unix Timestamp. I'm not sure which, but, it simple to convert it to a datetime object
>>> from datetime import datetime
>>> dt = datetime.fromtimestamp(1449798068)
>>> print(dt)
datetime.datetime(2015, 12, 10, 20, 41, 8)
It seems the #text key has the time in a local timezone, which is five hours ahead.
This question already has answers here:
Python datetime object show wrong timezone offset
(2 answers)
Closed 8 years ago.
I am using the '2014.2' version of pytz. I am converting Asia/Kuwait timezone i.e local time to UTC time using the following process:
>>> from_date = "2014/05/06 17:07"
>>> from_date = dateutil.parser.parse(from_date)
>>> utc=timezone('UTC')
>>> from_date = from_date.replace(tzinfo=timezone('Asia/Kuwait')).astimezone(utc)
>>> from_date
datetime.datetime(2014, 5, 6, 13, 55, tzinfo=<UTC>)
>>> from_date.strftime("%b %d %Y %H:%M:%S" )
'May 06 2014 13:55:00'
The actual UTC time was May 06 2014 14:06:00 which I found in: http://www.worldtimeserver.com/current_time_in_UTC.aspx Why pytz is not exactly converting to the actual time. As you can see there is a time difference between 10-11 minutes.
Don't use datetime.replace() with pytz timezones. From the pytz documentation:
Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones.
The reason it doesn't work is that pytz timezones include historical data and datetime is not equipped to handle these.
Use the dedicated timezone.localize() method instead:
>>> import dateutil.parser
>>> from pytz import timezone
>>> from_date = "2014/05/06 17:07"
>>> from_date = dateutil.parser.parse(from_date)
>>> from_date = timezone('Asia/Kuwait').localize(from_date).astimezone(timezone('UTC'))
>>> from_date
datetime.datetime(2014, 5, 6, 14, 7, tzinfo=<UTC>)
>>> from_date.strftime("%b %d %Y %H:%M:%S" )
'May 06 2014 14:07:00'
The timezone.localize() method applies a timezone to a naive datetime object correctly.
This question already has answers here:
Parse date string and change format
(10 answers)
Closed 8 years ago.
I have two dates:
Sat Mar 15 19:47:17 +0000 2014
2014-03-12 19:50:22.159411+00:00
I need to compare these two dates but I get the error
TypeError: can't compare datetime.datetime to unicode
How should I convert one of them?
The easiest method is to use the 3rd party dateutil lib, and do:
from dateutil.parser import parse as parse_date
unicode_text = 'Sat Mar 15 19:47:17 +0000 2014'
dt = parse_date(unicode_text)
# 2014-03-15 19:47:17+00:00
if dt == other_datetime:
# do something