How to set Postgres Datetime field into Odoo Datetime field

10.1k Views Asked by At

I am trying to display set time of Postgres database time to datetime field into Odoo.

I am creating field is that to set the time.

last_modify_article = fields.Datetime("Last Modify Date")

But my DateTime :~ 2017-08-28T08:43:56+0200 is perfectly stored in Postgres database but in Odoo saw in different.

So, my question is that how can I manage the database date-time in the field.

Here is the Postgres Time

And

Here is Odoo field to set datetime in UTC

3

There are 3 best solutions below

3
On

Odoo is designed to have the date and time stored as UTC in the database and convert it to the user's timezone on the front-end.

What time zone is set for your user? You can click your name in the top right, then Preferences. Time zone should be shown on the popup form.

0
On

Actually the database stores the Datetime field according to the system timezone. In Odoo, views which will be automatically converted according to the user's timezone if it is set.

On your images, I can see the time difference id +5:30 ie, Asia/Kolakata timezone. So your custom operations on Datetime field need the proper conversion of Timezone according to the user.

Odoo views and ORM methods are treated the tz with moment.js and the pytz conversions. This is actually a good feature to manage different timezones in Odoo.

You can use astimezone on Datetime objects:

def astimezone(self, tz): # known case of datetime.datetime.astimezone
    """ tz -> convert to local time in new timezone tz """
    return datetime(1, 1, 1)

or

fields.Datetime.context_timestamp(self, datetime.strptime(value, DEFAULT_SERVER_DATETIME_FORMAT))
0
On

You can use pytz library to convert datetime based on user's timezone. See the code sample.

import pytz
import datetime

    def current_user_datetime(self):
        currenttimezone = pytz.timezone(self.env.context.get('tz'))
        user_datetime = datetime.datetime.now(currenttimezone)
        self.timefield = user_datetime