diff options
| author | Quentin (OpenERP) <qdp-launchpad@openerp.com> | 2014-04-14 17:36:13 +0200 |
|---|---|---|
| committer | Quentin (OpenERP) <qdp-launchpad@openerp.com> | 2014-04-14 17:36:13 +0200 |
| commit | 43b8d2aec1f1867a1b4c01db8d9c2f5cd5b6904f (patch) | |
| tree | e334d9af6556cec46cd29d08eae3f473d89f5390 | |
| parent | 14be89970611e344026883e9f1e6d0a918f62876 (diff) | |
[IMP] fields.date: added date_to_datetime function
bzr revid: qdp-launchpad@openerp.com-20140414153613-geu20zacxnf0swol
| -rw-r--r-- | openerp/osv/fields.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index f60e83726c4..8273fe09830 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -344,6 +344,29 @@ class date(_column): exc_info=True) return (context_today or today).strftime(tools.DEFAULT_SERVER_DATE_FORMAT) + def date_to_datetime(self, cr, uid, userdate, context=None): + """ Convert date values expressed in user's timezone to + server-side UTC timestamp, assuming a default arbitrary + time of 12:00 AM - because a time is needed. + + :param str userdate: date string in in user time zone + :return: UTC datetime string for server-side use + """ + user_date = datetime.strptime(userdate, DEFAULT_SERVER_DATE_FORMAT) + if context and context.get('tz'): + tz_name = context['tz'] + else: + tz_name = self.pool.get('res.users').read(cr, SUPERUSER_ID, uid, ['tz'])['tz'] + if tz_name: + utc = pytz.timezone('UTC') + context_tz = pytz.timezone(tz_name) + user_datetime = user_date + timedelta(hours=12.0) + local_timestamp = context_tz.localize(user_datetime, is_dst=False) + user_datetime = local_timestamp.astimezone(utc) + return user_datetime.strftime(DEFAULT_SERVER_DATETIME_FORMAT) + return user_date.strftime(DEFAULT_SERVER_DATETIME_FORMAT) + + class datetime(_column): _type = 'datetime' |
