Skip to content

Unable to use date_formats argument if timezone already found #376

Closed

Description

@thernstig

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)
>>>

Activity

  1. changed the title [-]Unable to use date_formats option if timezone already found[/-] [+]Unable to use date_formats argument if timezone already found[/+] on Jan 28, 2018
  2. brooksdavis commented on Jul 26, 2022

    @brooksdavis

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions