Esempio n. 1
0
def parse_with_formats(date_string, date_formats, settings):
    """ Parse with formats and return a dictionary with 'period' and 'obj_date'.

    :returns: :class:`datetime.datetime`, dict or None

    """
    if isinstance(date_formats, six.string_types):
        warn(_DateLocaleParser.DATE_FORMATS_ERROR_MESSAGE, FutureWarning)
        date_formats = [date_formats]
    period = 'day'
    for date_format in date_formats:
        try:
            date_obj = datetime.strptime(date_string, date_format)
        except ValueError:
            continue
        else:
            if '%d' not in date_format:
                period = 'month'
                date_obj = set_correct_day_from_settings(date_obj, settings)

            if not ('%y' in date_format or '%Y' in date_format):
                today = datetime.today()
                date_obj = date_obj.replace(year=today.year)

            date_obj = apply_timezone_from_settings(date_obj, settings)

            return {'date_obj': date_obj, 'period': period}
    else:
        return {'date_obj': None, 'period': period}
Esempio n. 2
0
def parse_with_formats(date_string, date_formats, settings):
    """ Parse with formats and return a dictionary with 'period' and 'obj_date'.

    :returns: :class:`datetime.datetime`, dict or None

    """
    period = 'day'
    for date_format in date_formats:
        try:
            date_obj = datetime.strptime(date_string, date_format)
        except ValueError:
            continue
        else:
            if '%d' not in date_format:
                period = 'month'
                date_obj = set_correct_day_from_settings(date_obj, settings)

            if not ('%y' in date_format or '%Y' in date_format):
                today = datetime.today()
                date_obj = date_obj.replace(year=today.year)

            date_obj = apply_timezone_from_settings(date_obj, settings)

            return DateData(date_obj=date_obj, period=period)
    else:
        return DateData(date_obj=None, period=period)
Esempio n. 3
0
    def _correct_for_day(self, dateobj):
        if (getattr(self, '_token_day', None)
                or getattr(self, '_token_weekday', None)
                or getattr(self, '_token_time', None)):
            return dateobj

        dateobj = set_correct_day_from_settings(dateobj,
                                                self.settings,
                                                current_day=self.now.day)
        return dateobj