Note that there are some explanatory texts on larger screens.

plurals
  1. POParse string with date and timezone to UTC datetime
    text
    copied!<p>I'm coding a Python 3 script that receives a full date with offset and I want to be able to compare it to another date without offset. The main problem I'm facing is that Python doesn't seem to like different offset datetime objects as it complains when you try to do any operation with those:</p> <pre><code>&gt;&gt;&gt; date_string 'Wed, 8 May 2013 15:33:29 +0200' &gt;&gt;&gt; new_date = datetime.strptime(date_string, '%a, %d %b %Y %H:%M:%S %z') &gt;&gt;&gt; new_date datetime.datetime(2013, 5, 8, 15, 33, 29, tzinfo=datetime.timezone(datetime.timedelta(0, 7200))) &gt;&gt;&gt; new_date - datetime.today() Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; TypeError: can't subtract offset-naive and offset-aware datetimes </code></pre> <p>As a workaround I've stripped <code>date_string</code> into two strings, once with the date and one with the offset, creating two objects: one date and one delta, them sum them up:</p> <pre><code>&gt;&gt;&gt; date_string 'Wed, 8 May 2013 15:33:29 +0200' &gt;&gt;&gt; match = re.match(r'(.*)(\s\+\d{4})',date_string) &gt;&gt;&gt; match.group(1) 'Wed, 8 May 2013 15:33:29' &gt;&gt;&gt; match.group(2) ' +0200' &gt;&gt;&gt; parsed_date = datetime.strptime(match.group(1), '%a, %d %b %Y %H:%M:%S') &gt;&gt;&gt; match_delta = re.match(r'\s\+(\d{2})(\d{2})',match.group(2)) &gt;&gt;&gt; parsed_date_delta = timedelta(minutes=int(match_delta.group(2)),hours=int(match_delta.group(1))) &gt;&gt;&gt; parsed_date_complete = parsed_date + parsed_date_delta &gt;&gt;&gt; parsed_date_complete datetime.datetime(2013, 5, 8, 17, 33, 29) </code></pre> <p>With this I'm able to get a final hour with the correct offset applied, so any comparision with another normal datetime object wouldn't raise an error.</p> <p>The thing I'm wondering is if there's an easier or more efficient way of achieving this. The idea is to receive a string like this one: <code>Wed, 8 May 2013 15:33:29 +0200</code> and be able to convert it in a datetime object without offset, so I can work with UTC times.</p> <p>EDIT: To explain a little more the issue, <code>new_date</code> has an offset value of <code>+0200</code> while <code>datetime.today()</code> and <code>datetime.utcnow()</code> doesn't have that offset, so trying to compare it or do any operation gives the following error in Python: <code>TypeError: can't subtract offset-naive and offset-aware datetimes</code>.</p> <p>If date is <code>Wed, 8 May 2013 15:33:29 +0200</code>, what I want is a way to calculate date like this: <code>Wed, 8 May 2013 17:33:29</code>, without the offset value but with the right time (offset applied to time). That way, since I don't have the offset, I can freely do stuff with <code>datetime.today()</code> and <code>datetime.utcnow()</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload