{"action":"opened","issue":{"url":"https://api.github.com/repos/odoo/odoo/issues/13440","repository_url":"https://api.github.com/repos/odoo/odoo","labels_url":"https://api.github.com/repos/odoo/odoo/issues/13440/labels{/name}","comments_url":"https://api.github.com/repos/odoo/odoo/issues/13440/comments","events_url":"https://api.github.com/repos/odoo/odoo/issues/13440/events","html_url":"https://github.com/odoo/odoo/issues/13440","id":175950944,"number":13440,"title":"context_today incorrectly calculate the date if user's preference timezone is set?","user":{"login":"niecw","id":594765,"avatar_url":"https://avatars.githubusercontent.com/u/594765?v=3","gravatar_id":"","url":"https://api.github.com/users/niecw","html_url":"https://github.com/niecw","followers_url":"https://api.github.com/users/niecw/followers","following_url":"https://api.github.com/users/niecw/following{/other_user}","gists_url":"https://api.github.com/users/niecw/gists{/gist_id}","starred_url":"https://api.github.com/users/niecw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/niecw/subscriptions","organizations_url":"https://api.github.com/users/niecw/orgs","repos_url":"https://api.github.com/users/niecw/repos","events_url":"https://api.github.com/users/niecw/events{/privacy}","received_events_url":"https://api.github.com/users/niecw/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-09-09T08:03:49Z","updated_at":"2016-09-09T08:03:49Z","closed_at":null,"body":"Impacted versions: 7.0, 8.0, 9.0, master\r\n\r\nSteps to reproduce:\r\nThe following is the code of context_today for new API (from https://github.com/odoo/odoo/blob/master/odoo/fields.py#L1423).  The old API code is very similar:\r\n\r\n```\r\n    def context_today(record, timestamp=None):\r\n        today = timestamp or datetime.now()\r\n        context_today = None\r\n        tz_name = record._context.get('tz') or record.env.user.tz\r\n        if tz_name:\r\n            try:\r\n                today_utc = pytz.timezone('UTC').localize(today, is_dst=False)  # UTC = no DST\r\n                context_today = today_utc.astimezone(pytz.timezone(tz_name))\r\n            except Exception:\r\n                _logger.debug(\"failed to compute context/client-specific today date, using UTC value for `today`\",\r\n                              exc_info=True)\r\n                return (context_today or today).strftime(DATE_FORMAT)\r\n```\r\nIMHO, if `timestamp` is not given - which is the case most of the time since `context_today` is used a lot in defaults - the code `today = timestamp or datetime.now()` will set `today` to current timestamp at the server's timezone (for example, 'Asia/Bangkok' (GMT+8)).\r\n\r\nIf the user has her/his timezone set (say to 'Asia/Bangkok' (GMT+8) - or any other timezone, I used the same timezone to simplify the example), then the code block within `if tz_name` will be executed.  The part `today_utc = pytz.timezone('UTC').localize(today, is_dst=False)` will set `today_utc` to the same timestamp as `today` but with the timezone removed and changed to 'UTC'.  Meaning that if `today` is 9 Sep 2016 17:00, then `today_utc` will be 9 Sep 2016 17:00 at UTC (although it should be 9 Sep 2016 10:00 AM UTC in actuality).\r\n\r\nThe code `context_today = today_utc.astimezone(pytz.timezone(tz_name))` will make `context_today` to be 10 Sep 2016 00:00 AM and returned as 10 Sep 2016, whereas the actual timestamp is 9 Sep 2016 17:00 or 9 Sep 2016.\r\n\r\nCurrent behavior:\r\n\r\nA user that has Timezone set in the Preference will have context_today set to the server's timezone + user's Preference Timezone instead of the user's Preference Timezone only.\r\n\r\nExpected behavior:\r\n\r\ncontext_today should be set as per user's Preference Timezone only\r\n\r\nQuestion in Odoo Forum: https://www.odoo.com/forum/help-1/question/is-something-strange-with-context-today-or-i-misunderstood-something-108024\r\n\r\nVideo/Screenshot link (optional): N/A"}}
{"actor":{"display_login":"niecw"}}