14

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

Harshit Trivedi
  • 764
  • 9
  • 33

3 Answers3

9

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))
travisw
  • 2,052
  • 1
  • 14
  • 27
Hilar AK
  • 1,655
  • 13
  • 25
4

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.

travisw
  • 2,052
  • 1
  • 14
  • 27
  • yes you right to Odoo is set to UTC but my time is correct in the database so how I can print same time in the field. And I am working with a different country so I can not assign timezone into my Preferences. and If I assign timezone its mismatch with the browser. so that way I print same as database value in my field value – Harshit Trivedi Aug 29 '17 at 04:12
  • @HarshitTrivedi Are you manually setting the time in the database (or in Python) or is it set from the Odoo frontend? – travisw Aug 29 '17 at 04:16
  • actually I working with API so I can get the API and set the DateTime field – Harshit Trivedi Aug 29 '17 at 04:18
0

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
Bishal Ghimire
  • 647
  • 7
  • 15