dateparser icon indicating copy to clipboard operation
dateparser copied to clipboard

Unable to use date_formats argument if timezone already found

Open thernstig opened this issue 6 years ago • 1 comments

If a date already contains UTC offset (i.e. time zone information) it is not possible to supply a date_formats argument to the parse function:

>>> import dateparser
>>> foo = dateparser.parse('Fri Jan 26 16:32:21 +0000 2018', date_formats=['%a %b %d %H:%M:%S %z %Y'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PATH\venv\lib\site-packages\dateparser\conf.py", line 84, in wrapper
    return f(*args, **kwargs)
  File "PATH\venv\lib\site-packages\dateparser\__init__.py", line 40, in parse
    data = parser.get_date_data(date_string, date_formats)
  File "PATH\venv\lib\site-packages\dateparser\date.py", line 359, in get_date_data
    res = parse_with_formats(date_string, date_formats or [], self._settings)
  File "PATH\venv\lib\site-packages\dateparser\date.py", line 141, in parse_with_formats
    date_obj = apply_timezone_from_settings(date_obj, settings)
  File "PATH\venv\lib\site-packages\dateparser\utils\__init__.py", line 147, in apply_timezone_from_settings
    date_obj = tz.localize(date_obj)
  File "PATH\venv\lib\site-packages\pytz\tzinfo.py", line 304, in localize
    raise ValueError('Not naive datetime (tzinfo is already set)')
ValueError: Not naive datetime (tzinfo is already set)
>>>

thernstig avatar Jan 28 '18 15:01 thernstig

FWIW, as a gross workaround, you can use settings to provide a timezone to at least be able to get a result without needing to manipulate the string. For example, you can parse an nginx log style date with:

>>> dateparser.parse('1/May/2022:12:22:13 +0100', date_formats=['%d/%b/%Y:%H:%M:%S %z'], settings={'TIMEZONE': 'BST'})
datetime.datetime(2022, 5, 1, 12, 22, 13)

Both specifications of the timezone appear to be ignored, but I guess it's better than nothing.

brooksdavis avatar Jul 26 '22 22:07 brooksdavis