Datetime and Time Zones - OpenERP 7

2019-07-21 20:40发布

I'm keeping the current date in a model using a datetime field in which I am indicating default to take the current date as a value.

_defaults = {
        'f_inicio' : lambda *a: datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
    }

Assuming that the current date is '07/10/2013 17:24:05 ', in the view is the date '07/10/2013 12:24:05' and rectified in the database and the date is '07/10/2013 17:24:05'; gather that this subtracting five hours. The user can set the time zone 'America/Bogota', Colombia is in the region (GTM - 5:00). But do not understand how to properly show when the user since I get a totally different value that should show. Apparently this taking as 'GTM 0' the GTM Colombia. Taking the approximate date create_date field that should have given me as default is '2013-10-07 22:24:05.384'.

Anyone have any idea what may be happening, really appreciate any help on this issue that is driving me crazy.

1条回答
萌系小妹纸
2楼-- · 2019-07-21 21:15

This drived me too crazy in the past. This is a real simple issue.

The date stored in the database is UTC (GMT-0) timezone. Assume that the person is set with timezone GMT - 5:00, then while storing the value to the database, the date will be added with 5 hrs (exactly 5, not little more or little less) and thus we get the UTC time to store into the database. Now when displaying the same it will check for the users timezone and it finds that its GMT - 5:00 so the database time will be subtracted with 5 (again exactly 5, not little more or little less) and displayed the user.

This will be great for system which is used in different timezones. So the understanding is the input is taken in the user's timezone stored in UTC(GMT-0) and displayed to user's timezone (even if the user viewing is in the different timezone the time will be accurate to their timezone)

Note: if the user is not set with the timezone the browsers timezone is considered and will be used with the warning icon on the top corner

That's it. Hope this gives u better clarity!!

查看更多
登录 后发表回答