How to use the jdatetime.date function in jdatetime

To help you get started, we’ve selected a few jdatetime 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 slashmili / django-jalali / django_jalali / db / models.py View on Github external
if isinstance(date_obj, datetime.date):
            return jdatetime.date.fromgregorian(date=date_obj)

        if not ansi_date_re.search(date_obj):
            raise exceptions.ValidationError(self.error_messages['invalid'])
        # Now that we have the date string in YYYY-MM-DD format, check to make
        # sure it's a valid date.
        # We could use time.strptime here and catch errors, but datetime.date
        # produces much friendlier error messages.
        year, month, day = map(int, date_obj.split('-'))
        try:
            if year > 1500:
                return jdatetime.date.fromgregorian(
                    date=datetime.date(year, month, day))
            else:
                return jdatetime.date(year, month, day)
        except ValueError as e:
            msg = self.error_messages['invalid_date'] % _(str(e))
            raise exceptions.ValidationError(msg)
github slashmili / django-jalali / django_jalali / forms / widgets.py View on Github external
def _has_changed(self, initial, data):
        # If our field has show_hidden_initial=True, initial will be a string
        # formatted by HiddenInput using formats.localize_input, which is not
        # necessarily the format used for this widget. Attempt to convert it.
        try:
            input_format = formats.get_format('DATE_INPUT_FORMATS')[0]
            initial = jdatetime.date(*time.strptime(initial, input_format)[:3])
        except (TypeError, ValueError):
            pass
        return super(jDateInput, self)._has_changed(self._format_value(initial), data)
github slashmili / django-jalali / django_jalali / forms / __init__.py View on Github external
def to_python(self, value):
        """
        Validates that the input can be converted to a date. Returns a Python
        datetime.date object.
        """
        if value in validators.EMPTY_VALUES:
            return None
        if isinstance(value, jdatetime.datetime):
            return value.date()
        if isinstance(value, jdatetime.date):
            return value

        groups = re.search(
            r'(?P[\d]{1,4})-(?P[\d]{1,2})-(?P[\d]{1,2})',
            value,
        )
        try:
            return jdatetime.date(
                year=int(groups.group(1)),
                month=int(groups.group(2)),
                day=int(groups.group(3)),
            )

        except (ValueError, AttributeError):
            pass