← Back to team overview

openerp-community team mailing list archive

Re: How to use datetime + timedelta on odoo filters

 

Hi Holger,

Great for relativedelta !!  Have you tried it on odoo 8?


Your patch on expression.py seems to be on the right place, but it is doing
adjustment in the wrong direction...  In this case original date should be
considered in user time zone and needs to be converted to UTC.

If user is in time zone GMT-6:00, records from one day will be stored on
database between 6:00 same day and 5:59 next day.  Your patch will try to
filter records from 18:00 same day to 17:59 next day...
We need to add the time delta, not substract it...

I cloned from original "context_timestamp" a "context_to_utc_timestamp"
that inverts the time zone conversion, so effectively providing the
expected result, and called that one in your patch instead...

    def context_to_utc_timestamp(cr, uid, timestamp, context=None):
        """Returns the given timestamp converted from the client's timezone
to UTC.
           This method is *not* meant for use as a _defaults initializer,
           because datetime fields are automatically converted upon
           display on client side. For _defaults :meth:`fields.datetime.now`
           should be used instead.

           :param datetime timestamp: naive datetime value (expressed in
user time zone)
                                      to be converted to UTC
           :param dict context: the 'tz' key in the context should give the
                                name of the User/Client timezone (otherwise
                                UTC is used)
           :rtype: datetime
           :return: timestamp converted to timezone-aware datetime in UTC
                    timezone
        """
        assert isinstance(timestamp, DT.datetime), 'Datetime instance
expected'
        #context_today = None
        utc_timestamp = None
        if context and context.get('tz'):
            tz_name = context['tz']
        else:
            registry =
openerp.modules.registry.RegistryManager.get(cr.dbname)
            user = registry['res.users'].browse(cr, SUPERUSER_ID, uid)
            tz_name = user.tz
        if tz_name:
            try:
                utc = pytz.timezone('UTC')
                context_tz = pytz.timezone(tz_name)
                localized_timestamp = context_tz.localize(timestamp,
is_dst=False) # UTC = no DST
                utc_timestamp = localized_timestamp.astimezone(utc)
            except Exception:
                _logger.debug("failed to compute UTC date using
context/client-specific today date",
                              exc_info=True)
        return utc_timestamp or timestamp


Please test it and provide feedback as to how to include this somewhere...

I also saw that these kind of patches are being mostly ignored, I don't
understand how, being them so important for correct accounting...

Regards,
-Mario

On Fri, Oct 3, 2014 at 9:03 PM, Holger Brunn <hbrunn@xxxxxxxx> wrote:

> > relativedelta in pyeval.js is only implementing year, month and day... !!
> > so adding hours will not work as expected...
>
> You might want to look into that: https://github.com/OCA/web/pull/15
>
> --
> Therp - Maatwerk in open ontwikkeling
>
> Holger Brunn - Ontwerp en implementatie
>
> mail: holger@xxxxxxxx
> web: http://therp.nl
>
> _______________________________________________
> Mailing list: https://launchpad.net/~openerp-community
> Post to     : openerp-community@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~openerp-community
> More help   : https://help.launchpad.net/ListHelp
>
>

Follow ups

References