datetimefield topython supports microseconds f models datetimefield self assertequal f topython20010102 03 04 05 000006 datetime datetime2001 1 2 3 4 5 6 self assertequal f topython20010102 03 04 05 999999 datetime datetime2001 1 2 3 4 5 999999 def testtimefieldtopythonmicrosecondsself other model with different datetime the following is equivalent to utc 20140312 18 34 23 24000 the following is equivalent to utc 20140313 05 34 23 24000 in vancouver we expect both results but in utc the date only matches one of them datetimefield to_python supports microseconds timefield to_python supports microseconds other model with different datetime the following is equivalent to utc 2014 03 12 18 34 23 24000 the following is equivalent to utc 2014 03 13 05 34 23 24000 in vancouver we expect both results but in utc the __date only matches one of them
import datetime

from django.db import models
from django.test import SimpleTestCase, TestCase, override_settings, skipUnlessDBFeature
from django.test.utils import requires_tz_support
from django.utils import timezone

from .models import DateTimeModel


class DateTimeFieldTests(TestCase):
    def test_datetimefield_to_python_microseconds(self):
        
        f = models.DateTimeField()
        self.assertEqual(
            f.to_python("2001-01-02 03:04:05.000006"),
            datetime.datetime(2001, 1, 2, 3, 4, 5, 6),
        )
        self.assertEqual(
            f.to_python("2001-01-02 03:04:05.999999"),
            datetime.datetime(2001, 1, 2, 3, 4, 5, 999999),
        )

    def test_timefield_to_python_microseconds(self):
        
        f = models.TimeField()
        self.assertEqual(f.to_python("01:02:03.000004"), datetime.time(1, 2, 3, 4))
        self.assertEqual(f.to_python("01:02:03.999999"), datetime.time(1, 2, 3, 999999))

    def test_datetimes_save_completely(self):
        dat = datetime.date(2014, 3, 12)
        datetim = datetime.datetime(2014, 3, 12, 21, 22, 23, 240000)
        tim = datetime.time(21, 22, 23, 240000)
        DateTimeModel.objects.create(d=dat, dt=datetim, t=tim)
        obj = DateTimeModel.objects.first()
        self.assertTrue(obj)
        self.assertEqual(obj.d, dat)
        self.assertEqual(obj.dt, datetim)
        self.assertEqual(obj.t, tim)

    @override_settings(USE_TZ=False)
    def test_lookup_date_without_use_tz(self):
        d = datetime.date(2014, 3, 12)
        dt1 = datetime.datetime(2014, 3, 12, 21, 22, 23, 240000)
        dt2 = datetime.datetime(2014, 3, 11, 21, 22, 23, 240000)
        t = datetime.time(21, 22, 23, 240000)
        m = DateTimeModel.objects.create(d=d, dt=dt1, t=t)
        
        DateTimeModel.objects.create(d=d, dt=dt2, t=t)
        self.assertEqual(m, DateTimeModel.objects.get(dt__date=d))

    @requires_tz_support
    @skipUnlessDBFeature("has_zoneinfo_database")
    @override_settings(USE_TZ=True, TIME_ZONE="America/Vancouver")
    def test_lookup_date_with_use_tz(self):
        d = datetime.date(2014, 3, 12)
        
        dt1 = datetime.datetime(
            2014, 3, 12, 10, 22, 23, 240000, tzinfo=timezone.get_current_timezone()
        )
        
        dt2 = datetime.datetime(
            2014, 3, 12, 21, 22, 23, 240000, tzinfo=timezone.get_current_timezone()
        )
        t = datetime.time(21, 22, 23, 240000)
        m1 = DateTimeModel.objects.create(d=d, dt=dt1, t=t)
        m2 = DateTimeModel.objects.create(d=d, dt=dt2, t=t)
        
        self.assertCountEqual(
            DateTimeModel.objects.filter(dt__date=d),
            [m1, m2],
        )
        with self.settings(TIME_ZONE="UTC"):
            
            self.assertCountEqual(DateTimeModel.objects.filter(dt__date=d), [m1])


class ValidationTest(SimpleTestCase):
    def test_datefield_cleans_date(self):
        f = models.DateField()
        self.assertEqual(datetime.date(2008, 10, 10), f.clean("2008-10-10", None))