Esempio n. 1
0
    def to_python(self, value):
        if value in EMPTY_VALUES:
            return

        if isinstance(value, arrow.Arrow):
            return value

        default_timezone = datetimeutil.local if self.assume_local else datetimeutil.utc

        if isinstance(value, datetime.datetime):
            if value.tzinfo:
                return arrow.Arrow.fromdatetime(value)
            else:
                return arrow.Arrow.fromdatetime(value, default_timezone)

        try:
            return arrow.Arrow.fromdatetime(
                datetimeutil.parse_iso_datetime_string(value,
                                                       default_timezone))
        except ValueError:
            pass

        try:
            return arrow.Arrow.fromdate(
                datetimeutil.parse_iso_date_string(value))
        except ValueError:
            pass
        msg = self.error_messages['invalid']
        raise exceptions.ValidationError(msg)
Esempio n. 2
0
    def to_python(self, value):
        if value in EMPTY_VALUES:
            return

        if isinstance(value, arrow.Arrow):
            return value

        default_timezone = datetimeutil.local if self.assume_local else datetimeutil.utc

        if isinstance(value, datetime.datetime):
            if value.tzinfo:
                return arrow.Arrow.fromdatetime(value)
            else:
                return arrow.Arrow.fromdatetime(value, default_timezone)

        try:
            return arrow.Arrow.fromdatetime(datetimeutil.parse_iso_datetime_string(value, default_timezone))
        except ValueError:
            pass

        try:
            return arrow.Arrow.fromdate(datetimeutil.parse_iso_date_string(value))
        except ValueError:
            pass
        msg = self.error_messages['invalid']
        raise exceptions.ValidationError(msg)
Esempio n. 3
0
 def test_invalid_values(self):
     with self.assertRaises(ValueError):
         datetimeutil.parse_iso_date_string(123)
     with self.assertRaises(ValueError):
         datetimeutil.parse_iso_date_string('2014-1-13')
     with self.assertRaises(ValueError):
         datetimeutil.parse_iso_date_string('2014/01/13')
Esempio n. 4
0
 def test_invalid_values(self):
     with self.assertRaises(ValueError):
         datetimeutil.parse_iso_date_string(123)
     with self.assertRaises(ValueError):
         datetimeutil.parse_iso_date_string('2014-1-13')
     with self.assertRaises(ValueError):
         datetimeutil.parse_iso_date_string('2014/01/13')
Esempio n. 5
0
 def to_python(self, value):
     if value in EMPTY_VALUES:
         return
     if isinstance(value, datetime.datetime):
         return value.date()
     if isinstance(value, datetime.date):
         return value
     try:
         return datetimeutil.parse_iso_date_string(value)
     except ValueError:
         pass
     msg = self.error_messages['invalid']
     raise exceptions.ValidationError(msg)
Esempio n. 6
0
 def to_python(self, value):
     if value in EMPTY_VALUES:
         return
     if isinstance(value, datetime.datetime):
         return value.date()
     if isinstance(value, datetime.date):
         return value
     try:
         return datetimeutil.parse_iso_date_string(value)
     except ValueError:
         pass
     msg = self.error_messages['invalid']
     raise exceptions.ValidationError(msg)
Esempio n. 7
0
 def test_valid_values(self):
     self.assertEqual(
         datetime.date(2014, 1, 13),
         datetimeutil.parse_iso_date_string('2014-01-13')
     )
Esempio n. 8
0
 def test_valid_values(self):
     self.assertEqual(datetime.date(2014, 1, 13),
                      datetimeutil.parse_iso_date_string('2014-01-13'))
Esempio n. 9
0
 def test_valid_values(self, value):
     assert datetime.date(2014, 1,
                          13) == datetimeutil.parse_iso_date_string(value)
Esempio n. 10
0
 def test_valid_values(self, value):
     assert datetime.date(2014, 1, 13) == datetimeutil.parse_iso_date_string(value)
Esempio n. 11
0
 def test_valid_values(self):
     assert datetime.date(2014, 1, 13) == datetimeutil.parse_iso_date_string('2014-01-13')