3

I have date in string:

Tue Oct 04 2016 12:13:00 GMT+0200 (CEST)

and I use (according to https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior):

datetime.strptime(datetime_string, '%a %b %m %Y %H:%M:%S %z %Z')

but I get error:

ValueError: 'z' is a bad directive in format '%a %b %m %Y %H:%M:%S %z %Z'

How to do it correctly?

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
Nips
  • 13,162
  • 23
  • 65
  • 103
  • Have a look at http://stackoverflow.com/questions/7992459/parse-cest-cet-time-in-python – Efferalgan Oct 04 '16 at 13:21
  • Possible duplicate of [How to convert string with UTC offset](http://stackoverflow.com/questions/30160143/how-to-convert-string-with-utc-offset) – L3viathan Oct 04 '16 at 13:35

4 Answers4

4

%z is the +0200, %Z is CEST. Therefore:

>>> s = "Tue Oct 04 2016 12:13:00 GMT+0200 (CEST)"
>>> datetime.strptime(s, '%a %b %d %Y %H:%M:%S GMT%z (%Z)')
datetime.datetime(2016, 10, 4, 12, 13, tzinfo=datetime.timezone(datetime.timedelta(0, 7200), 'CEST'))

I also replaced your %m with %d; %m is the month, numerically, so in your case 04 would be parsed as April.

L3viathan
  • 26,748
  • 2
  • 58
  • 81
  • Getting OP's error with Python 2.7, it works with Python 3.5. Do you know why? – Baris Demiray Oct 04 '16 at 13:23
  • Weird. I don't understand why, seeing as the documentation OP linked to is for Python 2.. – L3viathan Oct 04 '16 at 13:25
  • `datetime.timezone` is apparently [only available in Python 3.2+](https://docs.python.org/3/library/datetime.html#available-types). If you're stuck with Python 2, `dateutil` might be the better choice. – L3viathan Oct 04 '16 at 13:28
  • That's because `%Z` only recognises a limited number of codes, and that does not include `CEST`. – Efferalgan Oct 04 '16 at 13:29
  • 1
    Whoa, Python 2.7 documentation seems to claim a feature it doesn't have.. http://stackoverflow.com/questions/30160143/how-to-convert-string-with-utc-offset – Baris Demiray Oct 04 '16 at 13:30
  • @Efferalgan No, `%Z` recognizes CEST, the problem is with `%z`. You can see that by taking away the `%z` part in both format and string and replacing `CEST` with e.g. `FOOBAR`. – L3viathan Oct 04 '16 at 13:34
  • @L3viathan I tried removing only `%Z` and it worked fine, whereas removing only `%z` did not change anything. I guess online interpreters are not that reliable. – Efferalgan Oct 04 '16 at 13:38
2

python datetime can't parse the GMT part (You might want to specify it manually in your format). You can use dateutil instead:

In [16]: s = 'Tue Oct 04 2016 12:13:00 GMT+0200 (CEST)'

In [17]: from dateutil import parser

In [18]: parser.parse(s)
Out[18]: d = datetime.datetime(2016, 10, 4, 12, 13, tzinfo=tzoffset(u'CEST', -7200))
In [30]: d.utcoffset()
Out[30]: datetime.timedelta(-1, 79200)

In [31]: d.tzname()
Out[31]: 'CEST'
Mazdak
  • 105,000
  • 18
  • 159
  • 188
1

Simpler way to achieve this without taking care of datetime formatting identifiers will be the usage of dateutil.parser(). For example:

>>> import dateutil.parser 
>>> date_string = 'Tue Oct 04 2016 12:13:00 GMT+0200 (CEST)'
>>> dateutil.parser.parse(date_string)
datetime.datetime(2016, 10, 4, 12, 13, tzinfo=tzoffset(u'CEST', -7200))
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
0

If you want to parse all you datetime data in a column in pandas DataFrame, you can use apply method to apply together with dateutil.parser.parse to parse whole column:

from dateutil.parser import parse
df['col_name'] = df['col_name'].apply(parse)
Keivan
  • 1,300
  • 1
  • 16
  • 29