Python dateparser.parse() Examples

The following are 30 code examples of dateparser.parse(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module dateparser , or try the search function .
Example #1
Source File: binance.py    From archon with MIT License 6 votes vote down vote up
def date_to_milliseconds(date_str):
    """Convert UTC date to milliseconds
    If using offset strings add "UTC" to date string e.g. "now UTC", "11 hours ago UTC"
    See dateparse docs for formats http://dateparser.readthedocs.io/en/latest/
    :param date_str: date in readable format, i.e. "January 01, 2018", "11 hours ago UTC", "now UTC"
    :type date_str: str
    """
    # get epoch value in UTC
    epoch = datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc)
    # parse our date string
    d = dateparser.parse(date_str)
    # if the date is not timezone aware apply UTC timezone
    if d.tzinfo is None or d.tzinfo.utcoffset(d) is None:
        d = d.replace(tzinfo=pytz.utc)

    # return the difference in time
    return int((d - epoch).total_seconds() * 1000.0) 
Example #2
Source File: field_types.py    From lexpredict-contraxsuite with GNU Affero General Public License v3.0 6 votes vote down vote up
def extract_from_possible_value_text(self, possible_value_or_text):
        """
        Check if possible_value_or_text is also containing the value in this field type's data type.
        If not parse the possible_value_text var for extracting the first suitable value of this type.
        This method does not try to pick value hint or anything similar assuming that there is no
        source text available but only the value which is possibly in text form.
        :param field:
        :param possible_value_or_text:
        :return:
        """
        try:
            maybe_value = self.extract_from_possible_value(possible_value_or_text)
        except Exception as e:
            raise RuntimeError(f'Incorrect value ("{possible_value_or_text}") for field "{self.field.code}" ({self.type_code})') from e
        if maybe_value:
            return maybe_value
        variants = self._extract_variants_from_text(possible_value_or_text)
        if variants:
            return variants[0]
        return None 
Example #3
Source File: GoogleVault.py    From content with MIT License 6 votes vote down vote up
def timeframe_to_utc_zulu_range(timeframe_str):
    """
    Converts a time-frame to UTC Zulu format that can be used for startTime and endTime in various Google Vault requests.
    """
    try:
        parsed_str = dateparser.parse(timeframe_str)
        end_time = datetime.utcnow().isoformat() + 'Z'  # Current time
        start_time = parsed_str.isoformat() + 'Z'
        return (start_time, end_time)
    except Exception as ex:
        err_msg = str(ex)
        if 'Quota exceeded for quota metric' in err_msg:
            err_msg = 'Quota for Google Vault API exceeded'
            return_error('Unable to parse date correctly: {}'.format(err_msg))
        else:
            raise ex 
Example #4
Source File: Expanse.py    From content with MIT License 6 votes vote down vote up
def arg_to_timestamp(arg, arg_name: str, required: bool = False):
    if arg is None:
        if required is True:
            raise ValueError(f'Missing "{arg_name}"')
        return None

    if isinstance(arg, str) and arg.isdigit():
        # timestamp that str - we just convert it to int
        return int(arg)
    if isinstance(arg, str):
        # if the arg is string of date format 2019-10-23T00:00:00 or "3 days", etc
        date = dateparser.parse(arg, settings={'TIMEZONE': 'UTC'})
        if date is None:
            # if d is None it means dateparser failed to parse it
            raise ValueError(f'Invalid date: {arg_name}')

        return int(date.timestamp())
    if isinstance(arg, (int, float)):
        return arg 
Example #5
Source File: core.py    From maya with MIT License 6 votes vote down vote up
def parse_iso8601_duration(cls, duration, start=None, end=None):
        match = re.match(
            r"(?:P(?P<weeks>\d+)W)|(?:P(?:(?:(?P<years>\d+)Y)?(?:(?P<months>\d+)M)?(?:(?P<days>\d+)D))?(?:T(?:(?P<hours>\d+)H)?(?:(?P<minutes>\d+)M)?(?:(?P<seconds>\d+)S)?)?)",  # noqa
            duration,
        )

        time_components = {}
        if match:
            time_components = match.groupdict(0)
            for key, value in time_components.items():
                time_components[key] = int(value)

            duration = relativedelta(**time_components)

            if start:
                return parse(start.datetime() + duration)

            if end:
                return parse(end.datetime() - duration)

        return None 
Example #6
Source File: core.py    From maya with MIT License 6 votes vote down vote up
def from_iso8601(cls, s):
        # # Start and end, such as "2007-03-01T13:00:00Z/2008-05-11T15:30:00Z"
        start, end = s.split("/")
        try:
            start = parse(start)
        except pendulum.parsing.exceptions.ParserError:
            # start = self._parse_iso8601_duration(start, end=end)
            raise NotImplementedError()

        try:
            end = parse(end)
        except (pendulum.parsing.exceptions.ParserError, TypeError):
            end = cls.parse_iso8601_duration(end, start=start)

        return cls(start=start, end=end)

        # # Start and duration, such as "2007-03-01T13:00:00Z/P1Y2M10DT2H30M"
        # # Duration and end, such as "P1Y2M10DT2H30M/2008-05-11T15:30:00Z" 
Example #7
Source File: utils.py    From parsedmarc with Apache License 2.0 6 votes vote down vote up
def human_timestamp_to_datetime(human_timestamp, to_utc=False):
    """
    Converts a human-readable timestamp into a Python ``DateTime`` object

    Args:
        human_timestamp (str): A timestamp string
        to_utc (bool): Convert the timestamp to UTC

    Returns:
        DateTime: The converted timestamp
    """

    human_timestamp = human_timestamp.replace("-0000", "")
    human_timestamp = parenthesis_regex.sub("", human_timestamp)
    settings = {}

    if to_utc:
        settings = {"TO_TIMEZONE": "UTC"}

    return dateparser.parse(human_timestamp, settings=settings) 
Example #8
Source File: poll.py    From pollmaster with MIT License 6 votes vote down vote up
def get_user_reply(self, ctx):
        """Pre-parse user input for wizard"""
        def check(m):
            return m.author == self.author
        try:
            reply = await self.bot.wait_for('message', timeout=600, check=check)
        except asyncio.TimeoutError:
            raise StopWizard

        if reply and reply.content:
            self.wizard_messages.append(reply)
            if reply.content.startswith(await get_pre(self.bot, reply)):
                await self.wizard_says(ctx, f'You can\'t use bot commands during the Poll Creation Wizard.\n'
                                       f'Stopping the Wizard and then executing the command:\n`{reply.content}`',
                                       footer=False)
                raise StopWizard
            elif reply.content.lower() == 'stop':
                await self.wizard_says(ctx, 'Poll Wizard stopped.', footer=False)
                raise StopWizard

            else:
                return reply.content
        else:
            raise InvalidInput 
Example #9
Source File: middlewares.py    From realestate-scraper with MIT License 6 votes vote down vote up
def process_spider_output(self, response, result, spider):
        # Called with the results returned from the Spider, after
        # it has processed the response.

        # Must return an iterable of Request, dict or Item objects.
        ts = datetime.now()
        stored_meta = response.meta.get('stored_meta')
        if stored_meta and 'timestamp' in stored_meta:
            ts = datetime.fromtimestamp(stored_meta['timestamp'])


        for i in result:
            if isinstance(i, (dict, Item)):
                i['scraped_time'] = ts
                i['scraped_time'] = ts.strftime('%d/%m/%Y')

                if 'DataAtualizacaoHumanizada' in i:
                    updated = parse(i['DataAtualizacaoHumanizada'],
                                    languages=['pt'],
                                    settings={'RELATIVE_BASE': ts})
                    i['updated_time'] = updated.strftime('%d/%m/%Y')
            yield i 
Example #10
Source File: save_historical_data_Roibal.py    From Cryptocurrency-Trading-Bots-Python-Beginner-Advance with MIT License 6 votes vote down vote up
def date_to_milliseconds(date_str):
    """Convert UTC date to milliseconds

    If using offset strings add "UTC" to date string e.g. "now UTC", "11 hours ago UTC"

    See dateparse docs for formats http://dateparser.readthedocs.io/en/latest/

    :param date_str: date in readable format, i.e. "January 01, 2018", "11 hours ago UTC", "now UTC"
    :type date_str: str
    """
    # get epoch value in UTC
    epoch = datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc)
    # parse our date string
    d = dateparser.parse(date_str)
    # if the date is not timezone aware apply UTC timezone
    if d.tzinfo is None or d.tzinfo.utcoffset(d) is None:
        d = d.replace(tzinfo=pytz.utc)

    # return the difference in time
    return int((d - epoch).total_seconds() * 1000.0) 
Example #11
Source File: cleanup_history.py    From caluma with GNU General Public License v3.0 6 votes vote down vote up
def handle(self, *args, **options):
        force = options["force"]

        lt = parse(options["keep"])
        if lt is not None:
            lt = timezone.make_aware(lt)

        for _, model in registered_models.items():
            qs = model.history.all()
            if lt is not None:
                qs = model.history.filter(history_date__lt=lt)

            action_str = "Deleting" if force else "Would delete"
            self.stdout.write(
                f'{action_str} {qs.count()} historical records from model "{model.__name__}"'
            )
            if force:
                qs.delete() 
Example #12
Source File: save_historical_data.py    From python-binance with MIT License 6 votes vote down vote up
def date_to_milliseconds(date_str):
    """Convert UTC date to milliseconds

    If using offset strings add "UTC" to date string e.g. "now UTC", "11 hours ago UTC"

    See dateparse docs for formats http://dateparser.readthedocs.io/en/latest/

    :param date_str: date in readable format, i.e. "January 01, 2018", "11 hours ago UTC", "now UTC"
    :type date_str: str
    """
    # get epoch value in UTC
    epoch = datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc)
    # parse our date string
    d = dateparser.parse(date_str)
    # if the date is not timezone aware apply UTC timezone
    if d.tzinfo is None or d.tzinfo.utcoffset(d) is None:
        d = d.replace(tzinfo=pytz.utc)

    # return the difference in time
    return int((d - epoch).total_seconds() * 1000.0) 
Example #13
Source File: loaders.py    From PyFeeds with GNU Affero General Public License v3.0 6 votes vote down vote up
def parse_datetime(date_time, loader_context):
    if isinstance(date_time, datetime):
        return date_time
    elif isinstance(date_time, str):
        try:
            return dateutil_parse(
                date_time.strip(),
                dayfirst=loader_context.get("dayfirst", False),
                yearfirst=loader_context.get("yearfirst", True),
                ignoretz=loader_context.get("ignoretz", False),
            )
        except ValueError:
            # If dateutil can't parse it, it might be a human-readable date.
            return dateparser.parse(date_time)
    else:
        raise ValueError("date_time must be datetime or a str.") 
Example #14
Source File: helpers.py    From python-binance with MIT License 6 votes vote down vote up
def date_to_milliseconds(date_str):
    """Convert UTC date to milliseconds

    If using offset strings add "UTC" to date string e.g. "now UTC", "11 hours ago UTC"

    See dateparse docs for formats http://dateparser.readthedocs.io/en/latest/

    :param date_str: date in readable format, i.e. "January 01, 2018", "11 hours ago UTC", "now UTC"
    :type date_str: str
    """
    # get epoch value in UTC
    epoch = datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc)
    # parse our date string
    d = dateparser.parse(date_str)
    # if the date is not timezone aware apply UTC timezone
    if d.tzinfo is None or d.tzinfo.utcoffset(d) is None:
        d = d.replace(tzinfo=pytz.utc)

    # return the difference in time
    return int((d - epoch).total_seconds() * 1000.0) 
Example #15
Source File: date_util.py    From archon with MIT License 6 votes vote down vote up
def date_to_seconds(date_str):
    """Convert UTC date to seconds
    If using offset strings add "UTC" to date string e.g. "now UTC", "11 hours ago UTC"
    See dateparse docs for formats http://dateparser.readthedocs.io/en/latest/
    :param date_str: date in readable format, i.e. "January 01, 2018", "11 hours ago UTC", "now UTC"
    :type date_str: str
    """
    # get epoch value in UTC
    epoch = datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc)
    # parse our date string
    d = dateparser.parse(date_str)
    # if the date is not timezone aware apply UTC timezone
    if d.tzinfo is None or d.tzinfo.utcoffset(d) is None:
        d = d.replace(tzinfo=pytz.utc)

    # return the difference in time
    return int((d - epoch).total_seconds()) 
Example #16
Source File: utils.py    From csvs-to-sqlite with Apache License 2.0 5 votes vote down vote up
def apply_dates_and_datetimes(df, date_cols, datetime_cols, datetime_formats):
    def parse_datetime(datestring, force_date=False):
        if pd.isnull(datestring):
            return datestring
        dt = dateparser.parse(datestring, date_formats=datetime_formats)
        if force_date:
            return dt.date().isoformat()
        else:
            return dt.isoformat()

    for date_col in date_cols:
        df[date_col] = df[date_col].apply(lambda s: parse_datetime(s, force_date=True))
    for datetime_col in datetime_cols:
        df[datetime_col] = df[datetime_col].apply(parse_datetime) 
Example #17
Source File: core.py    From maya with MIT License 5 votes vote down vote up
def parse(string, timezone="UTC", day_first=False, year_first=True, strict=False):
    """"Returns a MayaDT instance for the machine-produced moment specified.

    Powered by pendulum.
    Accepts most known formats. Useful for working with data.

    Keyword Arguments:
        string -- string to be parsed
        timezone -- timezone referenced from (default: 'UTC')
        day_first -- if true, the first value (e.g. 01/05/2016)
                     is parsed as day.
                     if year_first is set to True, this distinguishes
                     between YDM and YMD. (default: False)
        year_first -- if true, the first value (e.g. 2016/05/01)
                      is parsed as year (default: True)
        strict -- if False, allow pendulum to fall back on datetime parsing
                  if pendulum's own parsing fails
    """
    options = {}
    options["tz"] = timezone
    options["day_first"] = day_first
    options["year_first"] = year_first
    options["strict"] = strict

    dt = pendulum.parse(str(string), **options)
    return MayaDT.from_datetime(dt) 
Example #18
Source File: highlevel.py    From pyedflib with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _parse_date(string):
    """
    A simple dateparser that detects common  date formats

    Parameters
    ----------
    string : str
        a date string in format as denoted below.

    Returns
    -------
    datetime.datetime
        datetime object of a time.

    """
    # some common formats.
    formats = ['%Y-%m-%d', '%d-%m-%Y', '%d.%m.%Y', '%Y.%m.%d', '%d %b %Y',
               '%Y/%m/%d', '%d/%m/%Y']
    for f in formats:
        try:
            return datetime.strptime(string, f)
        except:
            pass
    try:
        import dateparser
        return dateparser.parse(string)
    except:
        print('dateparser is not installed. to convert strings to dates'\
              'install via `pip install dateparser`.')
        raise ValueError('birthdate must be datetime object or of format'\
                         ' `%d-%m-%Y`, eg. `24-01-2020`') 
Example #19
Source File: dates.py    From memorious with MIT License 5 votes vote down vote up
def parse_date(text, format_hint=None):
    if text is None:
        return
    if format_hint is not None:
        parsed = datetime.strptime(text, format_hint)
    else:
        # Strip things that don't belong in dates but websites like to wrap
        # their dates with them anyway
        cleaned = str(text).strip('[] ')
        parsed = dateparser.parse(cleaned)
    return naive_datetime(parsed) 
Example #20
Source File: date_converter.py    From texta with GNU General Public License v3.0 5 votes vote down vote up
def convert_date(self,date_field_value,langs=[]):#, **kwargs):
        '''Converts given date field value to standard ES format yyyy-mm-dd
      
        :param date_field_value: date field value to convert
        :param langs: language(s) of the data (optional)
        :type date_field_value: string
        :type langs: list
        :return: date converted to standard ES format
        :rtype: string
        '''
     
        if langs:
            self._languages = langs
        try:
            if self._languages:
                datetime_object = dateparser.parse(date_field_value,languages=self._languages)
                # If fails to parse with given language (returns None)
                if not datetime_object:
                    datetime_object = dateparser.parse(date_field_value)
            else:
                datetime_object = dateparser.parse(date_field_value)
        
            if datetime_object:
                formatted_date = datetime_object.strftime('%Y-%m-%d')

        except Exception as e:
            logging.getLogger(ERROR_LOGGER).exception(e)
            formatted_date = None
        return formatted_date 
Example #21
Source File: test_pickle.py    From dateparser with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_pickling_basic_parsed_object(self):
        now = parse('now')
        self.assertIsInstance(now, datetime)
        pickle_dumps = pickle.dumps(now)
        self.assertIsInstance(pickle_dumps, bytes) 
Example #22
Source File: test_settings.py    From dateparser with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def when_date_is_parsed(self):
        self.result = parse(self.given_ds, settings=(self.confs or {})) 
Example #23
Source File: core.py    From maya with MIT License 5 votes vote down vote up
def from_iso8601(klass, iso8601_string):
        """Returns MayaDT instance from iso8601 string."""
        return parse(iso8601_string) 
Example #24
Source File: helpers.py    From datastream.io with Apache License 2.0 5 votes vote down vote up
def detect_time(dataframe):
    """ Attempt to detect the time dimension in a dataframe"""
    columns = set(dataframe.columns)
    timefield = unix = None
    for tfname in ['time', 'datetime', 'date', 'timestamp']:
        if tfname in columns:
            prev = current = None
            unix = True # Assume unix timestamp format unless proven otherwise
            for i in dataframe[tfname][:10]: # FIXME this seems arbitrary
                try:
                    current = dateparser.parse(str(i))
                    # timefield needs to be parsable and always increasing
                    if not current or (prev and prev > current):
                        tfname = ''
                        break
                    if unix and not (isinstance(i, float) or
                                     isinstance(i, int)):
                        unix = False
                except TypeError:
                    tfname = ''
                    break
            prev = current
            if tfname:
                timefield = tfname
                if isinstance(i, float) or isinstance(i, int):
                    unix = True
                break

    return timefield, unix 
Example #25
Source File: common.py    From tiktok-crawler with MIT License 5 votes vote down vote up
def parse_datetime(string):
    """
    parse string to datetime safely
    :param string: str to parse
    :return: datetime
    """
    if not string:
        return None
    return dateparser.parse(str(string)) 
Example #26
Source File: utils.py    From timetracker with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def parse_date(date):
    return parse(date, date_formats=['%d/%m/%Y'], settings={'TIMEZONE': get_localzone().zone}) 
Example #27
Source File: test_readers_s3.py    From exporters with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_date_prefix_yesterday(self):
        reader = S3Reader(self.options_dateparser, meta())
        yesterday = dateparser.parse('yesterday').strftime('%Y-%m-%d')
        expected = ['test_prefix/{yesterday}'.format(yesterday=yesterday)]
        self.assertEqual(expected, reader.keys_fetcher.prefixes) 
Example #28
Source File: s3_reader.py    From exporters with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def format_prefixes(prefixes, start, end):
    import dateparser
    start_date = dateparser.parse(start or 'today')
    end_date = dateparser.parse(end or 'today')
    if start_date > end_date:
        raise InvalidDateRangeError

    dates = []
    while start_date <= end_date:
        dates.append(start_date)
        start_date += datetime.timedelta(days=1)

    return [date.strftime(p) for date in dates for p in prefixes] 
Example #29
Source File: test_readers_s3.py    From exporters with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_prefix_list_using_date(self):
        reader = S3Reader(self.options_prefix_list_using_date, meta())
        yesterday = dateparser.parse('yesterday').strftime('%Y-%m-%d')
        expected = ['a_prefix/daily/{}'.format(yesterday),
                    'b_prefix/daily/{}'.format(yesterday),
                    'c_prefix/daily/{}'.format(yesterday)]
        self.assertEqual(expected, reader.keys_fetcher.prefixes) 
Example #30
Source File: test_readers_s3.py    From exporters with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_date_range_prefixes(self):
        reader = S3Reader(self.options_dateparser_range_3_days, meta())
        expected = ['test_prefix/{}'.format(dateparser.parse('2 days ago').strftime('%Y-%m-%d')),
                    'test_prefix/{}'.format(dateparser.parse('yesterday').strftime('%Y-%m-%d')),
                    'test_prefix/{}'.format(dateparser.parse('today').strftime('%Y-%m-%d'))]
        self.assertEqual(expected, reader.keys_fetcher.prefixes)