Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a date string is in UTC format

I have a date string like "2011-11-06 14:00:00+00:00". Is there a way to check if this is in UTC format or not ?. I tried to convert the above string to a datetime object using utc = datetime.strptime('2011-11-06 14:00:00+00:00','%Y-%m-%d %H:%M%S+%z) so that i can compare it with pytz.utc, but i get 'ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M%S+%z'

How to check if the date string is in UTC ?. Some example would be really appreciated.

Thank You

like image 319
James Avatar asked Feb 25 '26 05:02

James


2 Answers

A simple regular expression will do:

>>> import re
>>> RE = re.compile(r'^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}[+-]\d{2}:\d{2}$')
>>> bool(RE.search('2011-11-06 14:00:00+00:00'))
True
like image 94
DzinX Avatar answered Feb 27 '26 17:02

DzinX


By 'in UTC format' do you actually mean ISO-8601?. This is a pretty common question.

like image 34
SingleNegationElimination Avatar answered Feb 27 '26 19:02

SingleNegationElimination



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!