scrapinghub / scrapinghub/dateparser

DateParser does not currently recognize EPOCHs

Open
#905 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Dateparser currently does not handle EPOCHs very well. When parsing through a document, I sometimes of EPOCH with datetimes that I need to parse. Dateparser currently brings back null values instead of making the dateparser translation. I suggest incorporating something like the following code to EPOCHs and then making the translation to UTC. Something like the example below will help to keep your dateparser format working if the value is a string or float EPOCH.

import re
import logging
import pytz
import datetime
import dateparser
from dateparser import parse

def dateparser(dt_input): 
    epoch = None
    utc_zone = pytz.timezone('UTC')
    if dt_input is not None:
        epoch = float(dt_input)
        if epoch:
            try:
                dt_input_epoch = datetime.datetime.utcfromtimestamp(epoch)
                dt_input_epoch = utc_zone.localize(dt_input_epoch)
            except Exception as e:
                logging.warning(f"Error converting epoch to timestamp for {dt_input}: {e}")
        else:
            try:
                #Timestamp to Epoch
                if 'utc' not in dt_input.lower() and "+" not in dt_input:
                    dt_input_parsed = parse(str(dt_input), settings={'TIMEZONE':'UTC'})
                    if dt_input_parsed is not None:
                        dt_input_parsed = utc_zone.localize(dt_input_parsed)
                else:
                    dt_input_parsed = parse(str(dt_input))
            except Exception as e:
                logging.warning(f"Error parsing timestamp for {dt_input}: {e}")
   return dt_input_parsed

example:
dateparser("1616005693")
datetime.datetime(2021, 3, 17, 18, 28, 13, tzinfo=<StaticTzInfo 'UTC+00:00'>)
dateparser(1616005693)
datetime.datetime(2021, 3, 17, 18, 28, 13, tzinfo=<StaticTzInfo 'UTC+00:00'>)

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

No source file or test is named. Start by reproducing the two examples in the issue with string and numeric epoch inputs, then locate the public dateparser parsing entry point. Done means both inputs produce timezone-aware UTC datetimes while existing date-string parsing continues to work.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.