[Fixed]-Django datetimefield and postgres

1πŸ‘

βœ…

The result from the database is rounded to seconds and therefor 15:49.21.93894+1.00 becomes 15:49:21+02:00

It’s totally wrong. You have dropped microseconds part during converting datetime object to string now.strftime('%m.%d.%Y %H:%M:%S').
And also, +02:00 is a timezone utc offset of datetime object. It’s not a rounded seconds or whatever.

And the real problem was in your’s strftime string format. Today, your timezone probably has Daylight Saving Time. But, when you have passed wrong formatted datetime string to form, django form parsed it as 2017-10-02(02 October 2017) that is standart time in your timezone. So, that was the reason why you have got different utc offsets: +01:00 and +02:00.

0πŸ‘

The result from the database is rounded to seconds and therefor 15:49.21.93894+1.00 becomes 15:49:21+02:00

Beside the fact that the strftime format string should be :(β€˜%d.%m.%Y %H:%M:%S’) instead of (β€˜%m.%d.%Y %H:%M:%S’)

My Test is now like this :

class TestTravelShareForm(TestCase):

def setUp(self):
    self.creator = ShrUser.objects.create_user(username="foo", email="hallo.ich@localhost.de", password="bla")
    self.source = Point(x=48.0, y=11.0, srid=4326)
    self.destination = Point(x=48.0001, y=11.0001, srid=4326)
    self.now = timezone.now()
    self.now_string = self.now.strftime('%d.%m.%Y %H:%M:%S')

def test_TravelShareCreate(self):

    form_data = {

        'source': self.source,
        'destination': self.destination,
        'departure': self.now_string,

    }

    form = TravelShareForm (form_data)
    self.assertTrue(form.is_valid())

    form.instance.creator = self.creator
    result = form.save()

    self.assertEqual(result.creator_id, self.creator.id)
    self.assertEqual(result.source, self.source)
    self.assertEqual(result.destination, self.destination)

    result_dep = result.departure.strftime('%d.%m.%Y %H:%M:%S')

    self.assertEqual(result_dep, self.now_string)
πŸ‘€mbieren

Leave a comment