nlformats py has customized timeinputformats h m s h m s h m h m parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip parse a time in a valid but nondefault format get a parsed result the parsed result does a round trip to default format iso formats are accepted even if not specified in formats py parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format localized timefields with manually specified input formats can accept those formats parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip parse a time in a valid but nondefault format get a parsed result the parsed result does a round trip to default format parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format localized timefields with manually specified input formats can accept those formats parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid but nondefault format get a parsed result the parsed result does a round trip to default format parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format localized timefields with manually specified input formats can accept those formats parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error iso formats are accepted even if not specified in formats py parse a date in a valid format get a parsed result the parsed result does a round trip parse a date in a valid but nondefault format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format localized datefields with manually specified input formats can accept those formats parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip parse a date in a valid but nondefault format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format localized datefields with manually specified input formats can accept those formats parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid but nondefault format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format localized datefields with manually specified input formats can accept those formats parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error iso formats are accepted even if not specified in formats py parse a date in a valid format get a parsed result the parsed result does a round trip parse a date in a valid but nondefault format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format localized datetimefields with manually specified input formats can accept those formats parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result iso format is always valid the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip parse a date in a valid but nondefault format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format localized datetimefields with manually specified input formats can accept those formats parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid but nondefault format get a parsed result the parsed result does a round trip to default format localized datetimefields in a nonlocalized environment act as unlocalized widgets parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format localized datetimefields with manually specified input formats can accept those formats parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format nl formats py has customized time_input_formats h m s h m s h m h m parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip parse a time in a valid but non default format get a parsed result the parsed result does a round trip to default format iso formats are accepted even if not specified in formats py parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format localized timefields with manually specified input formats can accept those formats parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip parse a time in a valid but non default format get a parsed result the parsed result does a round trip to default format parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format localized timefields with manually specified input formats can accept those formats parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid but non default format get a parsed result the parsed result does a round trip to default format parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format localized timefields with manually specified input formats can accept those formats parse a time in an unaccepted format get an error parse a time in a valid format get a parsed result the parsed result does a round trip to the same format parse a time in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error iso formats are accepted even if not specified in formats py parse a date in a valid format get a parsed result the parsed result does a round trip parse a date in a valid but non default format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format localized datefields with manually specified input formats can accept those formats parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip parse a date in a valid but non default format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format localized datefields with manually specified input formats can accept those formats parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid but non default format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format localized datefields with manually specified input formats can accept those formats parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error iso formats are accepted even if not specified in formats py parse a date in a valid format get a parsed result the parsed result does a round trip parse a date in a valid but non default format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format localized datetimefields with manually specified input formats can accept those formats parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result iso format is always valid the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip parse a date in a valid but non default format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format localized datetimefields with manually specified input formats can accept those formats parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid but non default format get a parsed result the parsed result does a round trip to default format localized datetimefields in a non localized environment act as unlocalized widgets parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format localized datetimefields with manually specified input formats can accept those formats parse a date in an unaccepted format get an error parse a date in a valid format get a parsed result the parsed result does a round trip to the same format parse a date in a valid format get a parsed result the parsed result does a round trip to default format
from datetime import date, datetime, time

from django import forms
from django.core.exceptions import ValidationError
from django.test import SimpleTestCase, override_settings
from django.utils import translation
from django.utils.translation import activate, deactivate


class LocalizedTimeTests(SimpleTestCase):
    def setUp(self):
        
        
        activate("nl")

    def tearDown(self):
        deactivate()

    def test_timeField(self):
        "TimeFields can parse dates in the default format"
        f = forms.TimeField()
        
        with self.assertRaises(ValidationError):
            f.clean("1:30:05 PM")

        
        result = f.clean("13:30:05")
        self.assertEqual(result, time(13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:05")

        
        result = f.clean("13:30")
        self.assertEqual(result, time(13, 30, 0))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:00")

        
        result = f.clean("13:30:05.000155")
        self.assertEqual(result, time(13, 30, 5, 155))

    def test_localized_timeField(self):
        "Localized TimeFields act as unlocalized widgets"
        f = forms.TimeField(localize=True)
        
        with self.assertRaises(ValidationError):
            f.clean("1:30:05 PM")

        
        result = f.clean("13:30:05")
        self.assertEqual(result, time(13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:05")

        
        result = f.clean("13:30")
        self.assertEqual(result, time(13, 30, 0))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:00")

    def test_timeField_with_inputformat(self):
        "TimeFields with manually specified input formats can accept those formats"
        f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"])
        
        with self.assertRaises(ValidationError):
            f.clean("1:30:05 PM")
        with self.assertRaises(ValidationError):
            f.clean("13:30:05")

        
        result = f.clean("13.30.05")
        self.assertEqual(result, time(13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:05")

        
        result = f.clean("13.30")
        self.assertEqual(result, time(13, 30, 0))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:00")

    def test_localized_timeField_with_inputformat(self):
        
        f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"], localize=True)
        
        with self.assertRaises(ValidationError):
            f.clean("1:30:05 PM")
        with self.assertRaises(ValidationError):
            f.clean("13:30:05")

        
        result = f.clean("13.30.05")
        self.assertEqual(result, time(13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:05")

        
        result = f.clean("13.30")
        self.assertEqual(result, time(13, 30, 0))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:00")


@translation.override(None)
@override_settings(TIME_INPUT_FORMATS=["%I:%M:%S %p", "%I:%M %p"])
class CustomTimeInputFormatsTests(SimpleTestCase):
    def test_timeField(self):
        "TimeFields can parse dates in the default format"
        f = forms.TimeField()
        
        with self.assertRaises(ValidationError):
            f.clean("13:30:05")

        
        result = f.clean("1:30:05 PM")
        self.assertEqual(result, time(13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:05 PM")

        
        result = f.clean("1:30 PM")
        self.assertEqual(result, time(13, 30, 0))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:00 PM")

    def test_localized_timeField(self):
        "Localized TimeFields act as unlocalized widgets"
        f = forms.TimeField(localize=True)
        
        with self.assertRaises(ValidationError):
            f.clean("13:30:05")

        
        result = f.clean("1:30:05 PM")
        self.assertEqual(result, time(13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:05 PM")

        
        result = f.clean("01:30 PM")
        self.assertEqual(result, time(13, 30, 0))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:00 PM")

    def test_timeField_with_inputformat(self):
        "TimeFields with manually specified input formats can accept those formats"
        f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"])
        
        with self.assertRaises(ValidationError):
            f.clean("1:30:05 PM")
        with self.assertRaises(ValidationError):
            f.clean("13:30:05")

        
        result = f.clean("13.30.05")
        self.assertEqual(result, time(13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:05 PM")

        
        result = f.clean("13.30")
        self.assertEqual(result, time(13, 30, 0))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:00 PM")

    def test_localized_timeField_with_inputformat(self):
        
        f = forms.TimeField(input_formats=["%H.%M.%S", "%H.%M"], localize=True)
        
        with self.assertRaises(ValidationError):
            f.clean("1:30:05 PM")
        with self.assertRaises(ValidationError):
            f.clean("13:30:05")

        
        result = f.clean("13.30.05")
        self.assertEqual(result, time(13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:05 PM")

        
        result = f.clean("13.30")
        self.assertEqual(result, time(13, 30, 0))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:00 PM")


class SimpleTimeFormatTests(SimpleTestCase):
    def test_timeField(self):
        "TimeFields can parse dates in the default format"
        f = forms.TimeField()
        
        with self.assertRaises(ValidationError):
            f.clean("1:30:05 PM")

        
        result = f.clean("13:30:05")
        self.assertEqual(result, time(13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:05")

        
        result = f.clean("13:30")
        self.assertEqual(result, time(13, 30, 0))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:00")

    def test_localized_timeField(self):
        "Localized TimeFields in a non-localized environment act as unlocalized widgets"
        f = forms.TimeField()
        
        with self.assertRaises(ValidationError):
            f.clean("1:30:05 PM")

        
        result = f.clean("13:30:05")
        self.assertEqual(result, time(13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:05")

        
        result = f.clean("13:30")
        self.assertEqual(result, time(13, 30, 0))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:00")

    def test_timeField_with_inputformat(self):
        "TimeFields with manually specified input formats can accept those formats"
        f = forms.TimeField(input_formats=["%I:%M:%S %p", "%I:%M %p"])
        
        with self.assertRaises(ValidationError):
            f.clean("13:30:05")

        
        result = f.clean("1:30:05 PM")
        self.assertEqual(result, time(13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:05")

        
        result = f.clean("1:30 PM")
        self.assertEqual(result, time(13, 30, 0))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:00")

    def test_localized_timeField_with_inputformat(self):
        
        f = forms.TimeField(input_formats=["%I:%M:%S %p", "%I:%M %p"], localize=True)
        
        with self.assertRaises(ValidationError):
            f.clean("13:30:05")

        
        result = f.clean("1:30:05 PM")
        self.assertEqual(result, time(13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:05")

        
        result = f.clean("1:30 PM")
        self.assertEqual(result, time(13, 30, 0))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "13:30:00")


class LocalizedDateTests(SimpleTestCase):
    def setUp(self):
        activate("de")

    def tearDown(self):
        deactivate()

    def test_dateField(self):
        "DateFields can parse dates in the default format"
        f = forms.DateField()
        
        with self.assertRaises(ValidationError):
            f.clean("21/12/2010")

        
        self.assertEqual(f.clean("2010-12-21"), date(2010, 12, 21))

        
        result = f.clean("21.12.2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")

        
        result = f.clean("21.12.10")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")

    def test_localized_dateField(self):
        "Localized DateFields act as unlocalized widgets"
        f = forms.DateField(localize=True)
        
        with self.assertRaises(ValidationError):
            f.clean("21/12/2010")

        
        result = f.clean("21.12.2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")

        
        result = f.clean("21.12.10")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")

    def test_dateField_with_inputformat(self):
        "DateFields with manually specified input formats can accept those formats"
        f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"])
        
        with self.assertRaises(ValidationError):
            f.clean("2010-12-21")
        with self.assertRaises(ValidationError):
            f.clean("21/12/2010")
        with self.assertRaises(ValidationError):
            f.clean("21.12.2010")

        
        result = f.clean("12.21.2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")

        
        result = f.clean("12-21-2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")

    def test_localized_dateField_with_inputformat(self):
        
        f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"], localize=True)
        
        with self.assertRaises(ValidationError):
            f.clean("2010-12-21")
        with self.assertRaises(ValidationError):
            f.clean("21/12/2010")
        with self.assertRaises(ValidationError):
            f.clean("21.12.2010")

        
        result = f.clean("12.21.2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")

        
        result = f.clean("12-21-2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")


@translation.override(None)
@override_settings(DATE_INPUT_FORMATS=["%d.%m.%Y", "%d-%m-%Y"])
class CustomDateInputFormatsTests(SimpleTestCase):
    def test_dateField(self):
        "DateFields can parse dates in the default format"
        f = forms.DateField()
        
        with self.assertRaises(ValidationError):
            f.clean("2010-12-21")

        
        result = f.clean("21.12.2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")

        
        result = f.clean("21-12-2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")

    def test_localized_dateField(self):
        "Localized DateFields act as unlocalized widgets"
        f = forms.DateField(localize=True)
        
        with self.assertRaises(ValidationError):
            f.clean("2010-12-21")

        
        result = f.clean("21.12.2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")

        
        result = f.clean("21-12-2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")

    def test_dateField_with_inputformat(self):
        "DateFields with manually specified input formats can accept those formats"
        f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"])
        
        with self.assertRaises(ValidationError):
            f.clean("21.12.2010")
        with self.assertRaises(ValidationError):
            f.clean("2010-12-21")

        
        result = f.clean("12.21.2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")

        
        result = f.clean("12-21-2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")

    def test_localized_dateField_with_inputformat(self):
        
        f = forms.DateField(input_formats=["%m.%d.%Y", "%m-%d-%Y"], localize=True)
        
        with self.assertRaises(ValidationError):
            f.clean("21.12.2010")
        with self.assertRaises(ValidationError):
            f.clean("2010-12-21")

        
        result = f.clean("12.21.2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")

        
        result = f.clean("12-21-2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010")


class SimpleDateFormatTests(SimpleTestCase):
    def test_dateField(self):
        "DateFields can parse dates in the default format"
        f = forms.DateField()
        
        with self.assertRaises(ValidationError):
            f.clean("21.12.2010")

        
        result = f.clean("2010-12-21")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21")

        
        result = f.clean("12/21/2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21")

    def test_localized_dateField(self):
        "Localized DateFields in a non-localized environment act as unlocalized widgets"
        f = forms.DateField()
        
        with self.assertRaises(ValidationError):
            f.clean("21.12.2010")

        
        result = f.clean("2010-12-21")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21")

        
        result = f.clean("12/21/2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21")

    def test_dateField_with_inputformat(self):
        "DateFields with manually specified input formats can accept those formats"
        f = forms.DateField(input_formats=["%d.%m.%Y", "%d-%m-%Y"])
        
        with self.assertRaises(ValidationError):
            f.clean("2010-12-21")

        
        result = f.clean("21.12.2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21")

        
        result = f.clean("21-12-2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21")

    def test_localized_dateField_with_inputformat(self):
        
        f = forms.DateField(input_formats=["%d.%m.%Y", "%d-%m-%Y"], localize=True)
        
        with self.assertRaises(ValidationError):
            f.clean("2010-12-21")

        
        result = f.clean("21.12.2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21")

        
        result = f.clean("21-12-2010")
        self.assertEqual(result, date(2010, 12, 21))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21")


class LocalizedDateTimeTests(SimpleTestCase):
    def setUp(self):
        activate("de")

    def tearDown(self):
        deactivate()

    def test_dateTimeField(self):
        "DateTimeFields can parse dates in the default format"
        f = forms.DateTimeField()
        
        with self.assertRaises(ValidationError):
            f.clean("1:30:05 PM 21/12/2010")

        
        self.assertEqual(
            f.clean("2010-12-21 13:30:05"), datetime(2010, 12, 21, 13, 30, 5)
        )

        
        result = f.clean("21.12.2010 13:30:05")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010 13:30:05")

        
        result = f.clean("21.12.2010 13:30")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010 13:30:00")

    def test_localized_dateTimeField(self):
        "Localized DateTimeFields act as unlocalized widgets"
        f = forms.DateTimeField(localize=True)
        
        with self.assertRaises(ValidationError):
            f.clean("1:30:05 PM 21/12/2010")

        
        result = f.clean("21.12.2010 13:30:05")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010 13:30:05")

        
        result = f.clean("21.12.2010 13:30")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010 13:30:00")

    def test_dateTimeField_with_inputformat(self):
        "DateTimeFields with manually specified input formats can accept those formats"
        f = forms.DateTimeField(input_formats=["%H.%M.%S %m.%d.%Y", "%H.%M %m-%d-%Y"])
        
        with self.assertRaises(ValidationError):
            f.clean("2010-12-21 13:30:05 13:30:05")
        with self.assertRaises(ValidationError):
            f.clean("1:30:05 PM 21/12/2010")
        with self.assertRaises(ValidationError):
            f.clean("13:30:05 21.12.2010")

        
        result = f.clean("13.30.05 12.21.2010")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010 13:30:05")

        
        result = f.clean("13.30 12-21-2010")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010 13:30:00")

    def test_localized_dateTimeField_with_inputformat(self):
        
        f = forms.DateTimeField(
            input_formats=["%H.%M.%S %m.%d.%Y", "%H.%M %m-%d-%Y"], localize=True
        )
        
        with self.assertRaises(ValidationError):
            f.clean("2010/12/21 13:30:05")
        with self.assertRaises(ValidationError):
            f.clean("1:30:05 PM 21/12/2010")
        with self.assertRaises(ValidationError):
            f.clean("13:30:05 21.12.2010")

        
        result = f.clean("13.30.05 12.21.2010")
        self.assertEqual(datetime(2010, 12, 21, 13, 30, 5), result)
        
        self.assertEqual(
            f.clean("2010-12-21 13:30:05"),
            datetime(2010, 12, 21, 13, 30, 5),
        )
        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010 13:30:05")

        
        result = f.clean("13.30 12-21-2010")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "21.12.2010 13:30:00")


@translation.override(None)
@override_settings(DATETIME_INPUT_FORMATS=["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"])
class CustomDateTimeInputFormatsTests(SimpleTestCase):
    def test_dateTimeField(self):
        "DateTimeFields can parse dates in the default format"
        f = forms.DateTimeField()
        
        with self.assertRaises(ValidationError):
            f.clean("2010/12/21 13:30:05")

        
        result = f.clean("1:30:05 PM 21/12/2010")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:05 PM 21/12/2010")

        
        result = f.clean("1:30 PM 21-12-2010")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:00 PM 21/12/2010")

    def test_localized_dateTimeField(self):
        "Localized DateTimeFields act as unlocalized widgets"
        f = forms.DateTimeField(localize=True)
        
        with self.assertRaises(ValidationError):
            f.clean("2010/12/21 13:30:05")

        
        result = f.clean("1:30:05 PM 21/12/2010")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:05 PM 21/12/2010")

        
        result = f.clean("1:30 PM 21-12-2010")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:00 PM 21/12/2010")

    def test_dateTimeField_with_inputformat(self):
        "DateTimeFields with manually specified input formats can accept those formats"
        f = forms.DateTimeField(input_formats=["%m.%d.%Y %H:%M:%S", "%m-%d-%Y %H:%M"])
        
        with self.assertRaises(ValidationError):
            f.clean("13:30:05 21.12.2010")
        with self.assertRaises(ValidationError):
            f.clean("2010/12/21 13:30:05")

        
        result = f.clean("12.21.2010 13:30:05")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:05 PM 21/12/2010")

        
        result = f.clean("12-21-2010 13:30")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:00 PM 21/12/2010")

    def test_localized_dateTimeField_with_inputformat(self):
        
        f = forms.DateTimeField(
            input_formats=["%m.%d.%Y %H:%M:%S", "%m-%d-%Y %H:%M"], localize=True
        )
        
        with self.assertRaises(ValidationError):
            f.clean("13:30:05 21.12.2010")
        with self.assertRaises(ValidationError):
            f.clean("2010/12/21 13:30:05")

        
        result = f.clean("12.21.2010 13:30:05")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:05 PM 21/12/2010")

        
        result = f.clean("12-21-2010 13:30")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "01:30:00 PM 21/12/2010")


class SimpleDateTimeFormatTests(SimpleTestCase):
    def test_dateTimeField(self):
        "DateTimeFields can parse dates in the default format"
        f = forms.DateTimeField()
        
        with self.assertRaises(ValidationError):
            f.clean("13:30:05 21.12.2010")

        
        result = f.clean("2010-12-21 13:30:05")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21 13:30:05")

        
        result = f.clean("12/21/2010 13:30:05")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21 13:30:05")

    def test_localized_dateTimeField(self):
        
        f = forms.DateTimeField()
        
        with self.assertRaises(ValidationError):
            f.clean("13:30:05 21.12.2010")

        
        result = f.clean("2010-12-21 13:30:05")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21 13:30:05")

        
        result = f.clean("12/21/2010 13:30:05")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21 13:30:05")

    def test_dateTimeField_with_inputformat(self):
        "DateTimeFields with manually specified input formats can accept those formats"
        f = forms.DateTimeField(
            input_formats=["%I:%M:%S %p %d.%m.%Y", "%I:%M %p %d-%m-%Y"]
        )
        
        with self.assertRaises(ValidationError):
            f.clean("2010/12/21 13:30:05")

        
        result = f.clean("1:30:05 PM 21.12.2010")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21 13:30:05")

        
        result = f.clean("1:30 PM 21-12-2010")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21 13:30:00")

    def test_localized_dateTimeField_with_inputformat(self):
        
        f = forms.DateTimeField(
            input_formats=["%I:%M:%S %p %d.%m.%Y", "%I:%M %p %d-%m-%Y"], localize=True
        )
        
        with self.assertRaises(ValidationError):
            f.clean("2010/12/21 13:30:05")

        
        result = f.clean("1:30:05 PM 21.12.2010")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30, 5))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21 13:30:05")

        
        result = f.clean("1:30 PM 21-12-2010")
        self.assertEqual(result, datetime(2010, 12, 21, 13, 30))

        
        text = f.widget.format_value(result)
        self.assertEqual(text, "2010-12-21 13:30:00")