From 8e5f0e2ce25d846e36a5fdd657034125ded26c8a Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Sat, 12 Jan 2013 06:57:17 +1300 Subject: [PATCH] [FIX] fields.date[time]: context_today()/context_timestamp() should fallback to user TZ if context TZ is missing This takes care of cases where the context is missing, e.g. for workflow-based operations, and is similar to the way tools.translate._() falls back to the user language. lp bug: https://launchpad.net/bugs/949516 fixed bzr revid: odo@openerp.com-20130111175717-u8r1t7s8872bz2rx --- openerp/osv/fields.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index bcbdad081ad..34968a662fb 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -289,8 +289,8 @@ class date(_column): This method may be passed as value to initialize _defaults. :param Model model: model (osv) for which the date value is being - computed - technical field, currently ignored, - automatically passed when used in _defaults. + computed - automatically passed when used in + _defaults. :param datetime timestamp: optional datetime value to use instead of the current date and time (must be a datetime, regular dates can't be converted @@ -303,9 +303,13 @@ class date(_column): today = timestamp or DT.datetime.now() context_today = None if context and context.get('tz'): + tz_name = context['tz'] + else: + tz_name = model.pool.get('res.users').read(cr, SUPERUSER_ID, uid, ['tz'])['tz'] + if tz_name: try: utc = pytz.timezone('UTC') - context_tz = pytz.timezone(context['tz']) + context_tz = pytz.timezone(tz_name) utc_today = utc.localize(today, is_dst=False) # UTC = no DST context_today = utc_today.astimezone(context_tz) except Exception: @@ -346,9 +350,14 @@ class datetime(_column): """ assert isinstance(timestamp, DT.datetime), 'Datetime instance expected' if context and context.get('tz'): + tz_name = context['tz'] + else: + registry = openerp.modules.registry.RegistryManager.get(cr.dbname) + tz_name = registry.get('res.users').read(cr, SUPERUSER_ID, uid, ['tz'])['tz'] + if tz_name: try: utc = pytz.timezone('UTC') - context_tz = pytz.timezone(context['tz']) + context_tz = pytz.timezone(tz_name) utc_timestamp = utc.localize(timestamp, is_dst=False) # UTC = no DST return utc_timestamp.astimezone(context_tz) except Exception: