How to use the khal.exceptions.DateTimeParseError function in khal

To help you get started, we’ve selected a few khal examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pimutils / khal / khal / parse_datetime.py View on Github external
:type delta_string: str
    :rtype: datetime.timedelta
    """

    tups = re.split(r'(-?\d+)', delta_string)
    if not re.match(r'^\s*$', tups[0]):
        raise ValueError('Invalid beginning of timedelta string "%s": "%s"'
                         % (delta_string, tups[0]))
    tups = tups[1:]
    res = dt.timedelta()

    for num, unit in zip(tups[0::2], tups[1::2]):
        try:
            numint = int(num)
        except ValueError:
            raise DateTimeParseError(
                'Invalid number in timedelta string "%s": "%s"' % (delta_string, num))

        ulower = unit.lower().strip()
        if ulower == 'd' or ulower == 'day' or ulower == 'days':
            res += dt.timedelta(days=numint)
        elif ulower == 'h' or ulower == 'hour' or ulower == 'hours':
            res += dt.timedelta(hours=numint)
        elif (ulower == 'm' or ulower == 'minute' or ulower == 'minutes' or
              ulower == 'min'):
            res += dt.timedelta(minutes=numint)
        elif (ulower == 's' or ulower == 'second' or ulower == 'seconds' or
              ulower == 'sec'):
            res += dt.timedelta(seconds=numint)
        else:
            raise ValueError('Invalid unit in timedelta string "%s": "%s"'
                             % (delta_string, unit))
github pimutils / khal / khal / parse_datetime.py View on Github external
for fun, dtformat, all_day, infer_year in [
            (datefstr_year, locale['datetimeformat'], False, True),
            (datefstr_year, locale['longdatetimeformat'], False, False),
            (timefstr_day, locale['timeformat'], False, False),
            (datetimefstr_weekday, locale['timeformat'], False, False),
            (datefstr_year, locale['dateformat'], True, True),
            (datefstr_year, locale['longdateformat'], True, False),
            (datefstr_weekday, None, True, False),
            (datetimefwords, None, False, False),
    ]:
        # if a `short` format contains a year, treat it as a `long` format
        if infer_year and '97' in dt.datetime(1997, 10, 11).strftime(dtformat):
            infer_year = False
        try:
            dtstart = fun(dtime_list, dtformat, infer_year=infer_year)
        except (ValueError, DateTimeParseError):
            pass
        else:
            return dtstart, all_day
    raise DateTimeParseError(
        "Could not parse \"{}\".\nPlease check your configuration or run "
        "`khal printformats` to see if this does match your configured "
        "[long](date|time|datetime)format.\nIf you suspect a bug, please "
        "file an issue at https://github.com/pimutils/khal/issues/ "
        "".format(dtime_list)
    )
github pimutils / khal / khal / controllers.py View on Github external
def new_interactive(collection, calendar_name, conf, info, location=None,
                    categories=None, repeat=None, until=None, alarms=None,
                    format=None, env=None):
    try:
        info = parse_datetime.eventinfofstr(
            info, conf['locale'],
            conf['default']['default_event_duration'],
            conf['default']['default_dayevent_duration'],
            adjust_reasonably=True, localize=False,
        )
    except DateTimeParseError:
        info = dict()

    while True:
        summary = info.get('summary')
        if not summary:
            summary = None
        info['summary'] = prompt('summary', default=summary)
        if info['summary']:
            break
        echo("a summary is required")

    while True:
        range_string = None
        if info.get('dtstart') and info.get('dtend'):
            start_string = info["dtstart"].strftime(conf['locale']['datetimeformat'])
            end_string = info["dtend"].strftime(conf['locale']['datetimeformat'])
github pimutils / khal / khal / parse_datetime.py View on Github external
(datetimefstr_weekday, locale['timeformat'], False, False),
            (datefstr_year, locale['dateformat'], True, True),
            (datefstr_year, locale['longdateformat'], True, False),
            (datefstr_weekday, None, True, False),
            (datetimefwords, None, False, False),
    ]:
        # if a `short` format contains a year, treat it as a `long` format
        if infer_year and '97' in dt.datetime(1997, 10, 11).strftime(dtformat):
            infer_year = False
        try:
            dtstart = fun(dtime_list, dtformat, infer_year=infer_year)
        except (ValueError, DateTimeParseError):
            pass
        else:
            return dtstart, all_day
    raise DateTimeParseError(
        "Could not parse \"{}\".\nPlease check your configuration or run "
        "`khal printformats` to see if this does match your configured "
        "[long](date|time|datetime)format.\nIf you suspect a bug, please "
        "file an issue at https://github.com/pimutils/khal/issues/ "
        "".format(dtime_list)
    )