scrapinghub / scrapinghub/dateparser

Unable to use date_formats argument if timezone already found

Open Beginner friendly
#376 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Status: Bug confirmed Type: Bug
Dominant language
Python
Stars
2.9k
Forks
520
Avg merge
22h 56m
Merged PRs (30d)
6

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the failure with dateparser.parse using the date string and date_formats shown. Read parse_with_formats in date.py and apply_timezone_from_settings in dateparser/utils/init.py, then verify that the same input completes without the ValueError and returns the expected timezone-aware date.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.