python - Parse string with date and timezone to UTC datetime -


i'm coding python 3 script receives full date offset , want able compare date without offset. main problem i'm facing python doesn't seem different offset datetime objects complains when try operation those:

>>> date_string 'wed, 8 may 2013 15:33:29 +0200' >>> new_date = datetime.strptime(date_string, '%a, %d %b %y %h:%m:%s %z') >>> new_date datetime.datetime(2013, 5, 8, 15, 33, 29, tzinfo=datetime.timezone(datetime.timedelta(0, 7200))) >>> new_date - datetime.today() traceback (most recent call last):   file "<stdin>", line 1, in <module> typeerror: can't subtract offset-naive , offset-aware datetimes 

as workaround i've stripped date_string 2 strings, once date , 1 offset, creating 2 objects: 1 date , 1 delta, them sum them up:

>>> date_string 'wed, 8 may 2013 15:33:29 +0200' >>> match = re.match(r'(.*)(\s\+\d{4})',date_string) >>> match.group(1) 'wed, 8 may 2013 15:33:29' >>> match.group(2) ' +0200' >>> parsed_date = datetime.strptime(match.group(1), '%a, %d %b %y %h:%m:%s') >>> match_delta = re.match(r'\s\+(\d{2})(\d{2})',match.group(2)) >>> parsed_date_delta = timedelta(minutes=int(match_delta.group(2)),hours=int(match_delta.group(1))) >>> parsed_date_complete = parsed_date + parsed_date_delta >>> parsed_date_complete datetime.datetime(2013, 5, 8, 17, 33, 29) 

with i'm able final hour correct offset applied, comparision normal datetime object wouldn't raise error.

the thing i'm wondering if there's easier or more efficient way of achieving this. idea receive string one: wed, 8 may 2013 15:33:29 +0200 , able convert in datetime object without offset, can work utc times.

edit: explain little more issue, new_date has offset value of +0200 while datetime.today() , datetime.utcnow() doesn't have offset, trying compare or operation gives following error in python: typeerror: can't subtract offset-naive , offset-aware datetimes.

if date wed, 8 may 2013 15:33:29 +0200, want way calculate date this: wed, 8 may 2013 17:33:29, without offset value right time (offset applied time). way, since don't have offset, can freely stuff datetime.today() , datetime.utcnow()

i no python guru, according these docs:

classmethod datetime.today()

return current local datetime, tzinfo none. ...

there no time zone associated return value. it's not @ utc, offset unspecified. therefore makes sense wouldn't allow compare two. result meaningless.

in other words, expect result of be?

10 may 2013 13:00 +0200  >  10 may 2013 12:00 
  • it could true, because 13:00 greater value 12:00.
  • it could false, because maybe local time zone offset -0100, comparing moments 11:00z > 13:00z. knows if meant use local offset since didn't specify.

since referring exact moment on left side, ambiguous 1 on right, operation gives error.

it's python gives error when try this. other frameworks such .net make assumptions , return results might not expecting. (read here if interested.)

so going question, said:

the idea receive string one: wed, 8 may 2013 15:33:29 +0200 , able convert in datetime object without offset, can work utc times.

the string have reflecting offset utc. way parsing fine. need compare more meaningful, such datetime.now(timezone.utc)


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -