~credativ/openobject-server/trunk-csv-import-domain

« back to all changes in this revision

Viewing changes to openerp/osv/fields.py

  • Committer: Quentin (OpenERP)
  • Date: 2014-04-14 15:36:13 UTC
  • Revision ID: qdp-launchpad@openerp.com-20140414153613-geu20zacxnf0swol
[IMP] fields.date: added date_to_datetime function

Show diffs side-by-side

added added

removed removed

Lines of Context:
344
344
                              exc_info=True)
345
345
        return (context_today or today).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
346
346
 
 
347
    def date_to_datetime(self, cr, uid, userdate, context=None):
 
348
        """ Convert date values expressed in user's timezone to
 
349
        server-side UTC timestamp, assuming a default arbitrary
 
350
        time of 12:00 AM - because a time is needed.
 
351
    
 
352
        :param str userdate: date string in in user time zone
 
353
        :return: UTC datetime string for server-side use
 
354
        """
 
355
        user_date = datetime.strptime(userdate, DEFAULT_SERVER_DATE_FORMAT)
 
356
        if context and context.get('tz'):
 
357
            tz_name = context['tz']
 
358
        else:
 
359
            tz_name = self.pool.get('res.users').read(cr, SUPERUSER_ID, uid, ['tz'])['tz']
 
360
        if tz_name:
 
361
            utc = pytz.timezone('UTC')
 
362
            context_tz = pytz.timezone(tz_name)
 
363
            user_datetime = user_date + timedelta(hours=12.0)
 
364
            local_timestamp = context_tz.localize(user_datetime, is_dst=False)
 
365
            user_datetime = local_timestamp.astimezone(utc)
 
366
            return user_datetime.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
 
367
        return user_date.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
 
368
 
 
369
 
347
370
class datetime(_column):
348
371
    _type = 'datetime'
349
372