I
I
Igor Che2017-02-22 08:34:36
PostgreSQL
Igor Che, 2017-02-22 08:34:36

How to force DateTime field to keep correct current time?

There is a model field:
dt = models.DateTimeField(null=False, auto_now=True)
When updating an object, my local time + 7 hours UTC is written to this field. Although I expect that UTC time will be recorded.
now it is written like this:
"2017-02-22 10:58:54.025505+07"
and it should be like this:
"2017-02-22 03:58:54.025505+07"
i.e. saves my local time, but writes it as absolute time.
How to keep the correct time?
The settings are USE_TZ = False, I tried to change it to True, there is no difference.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Falseclock, 2017-03-03
@Falseclock

you are overthinking and confusing yourself.
you decide how you want to store time. Postgres offers to store time with or without a time zone.
https://www.postgresql.org/docs/9.6/static/datatyp...
You put +07 at the end of time, which means time with a time zone.
make a field without a time zone, and from the language side, decide what time you will store in the database, UTC or local time, because otherwise you will get confused and once again translate one time into another. If the field is without a time zone, then it will ignore the zone and keep the value without conversion.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question