diff options
| author | Olivier Dony <odo@openerp.com> | 2012-02-13 19:48:02 +0100 |
|---|---|---|
| committer | Olivier Dony <odo@openerp.com> | 2012-02-13 19:48:02 +0100 |
| commit | 3520cc25aa93c9537f4bd2dcc336e2bc3c491d6a (patch) | |
| tree | 0c97b5f5d41b14276784e989e227182ab7a81792 | |
| parent | f87324e93e11c793b31c5e0f4d65bf7ed4751082 (diff) | |
| parent | 2b8a0616c774c75bc25f32d78fdbb69cbc4106f8 (diff) | |
[MERGE] Added fields.date.context_today method for bug 925361
lp bug: https://launchpad.net/bugs/925361 fixed
bzr revid: odo@openerp.com-20120213184802-gm6mqd8tk2kkswwn
| -rw-r--r-- | openerp/osv/fields.py | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/openerp/osv/fields.py b/openerp/osv/fields.py index 3e6f07cb08a..94b638e368a 100644 --- a/openerp/osv/fields.py +++ b/openerp/osv/fields.py @@ -35,14 +35,12 @@ import base64 import datetime as DT import logging +import pytz import re -import string -import sys import xmlrpclib from psycopg2 import Binary import openerp -import openerp.netsvc as netsvc import openerp.tools as tools from openerp.tools.translate import _ from openerp.tools import float_round, float_repr @@ -262,6 +260,7 @@ class float(_column): class date(_column): _type = 'date' + @staticmethod def today(*args): """ Returns the current date in a format fit for being a @@ -273,6 +272,28 @@ class date(_column): return DT.date.today().strftime( tools.DEFAULT_SERVER_DATE_FORMAT) + @staticmethod + def context_today(cr, uid, today=None, context=None): + """Returns the current date as seen in the client's timezone + in a format fit for date fields. + This method may be passed as value to initialize _defaults. + + :param datetime today: optional date value to use instead of + the current date""" + today = DT.datetime.now() + context_today = None + if context and context.get('tz'): + try: + utc = pytz.timezone('UTC') + context_tz = pytz.timezone(context['tz']) + utc_today = utc.localize(today, is_dst=False) + context_today = utc_today.astimezone(context_tz) + except Exception: + _logger.debug("failed to compute context/client-specific today date, " + "using the UTC value for `today`", + exc_info=True) + return (context_today or today).strftime(tools.DEFAULT_SERVER_DATE_FORMAT) + class datetime(_column): _type = 'datetime' @staticmethod |
