[Openerp-community] How to use datetime + timedelta on odoo filters

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[Openerp-community] How to use datetime + timedelta on odoo filters

Mario Arias
Hi,

When a company works in a timezone different from GMT, filters by datetime get screwed up.

Example:
If user timezone is GMT-6:00, then any of current "Today" filters will filter only records from tomorrow if current local time is after 18:00  (18:00 + 6:00 = 24:00 -->> next day).

I was trying to adjust for that replacing time.strftime('%Y-%m-%d') with

(datetime.datetime.now()+datetime.timedelta(hours=6)).strftime('%Y-%m-%d  %H:%M:%S')

as all records created today after 18:00 will have a database timestamp of tomorrow...


Problem is JS complains about invalid operands for "+": datetime and timedelta...

Any idea how could this be acomplished ?

Regards,
-Mario

_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Openerp-community] How to use datetime + timedelta on odoo filters

Holger Brunn
Hi Mario,

> Example:
> If user timezone is GMT-6:00, then any of current "Today" filters will
> filter only records from tomorrow if current local time is after 18:00
>  (18:00 + 6:00 = 24:00 -->> next day).

this is caused by https://bugs.launchpad.net/ocb-server/7.0/+bug/1255612, my
patch should still work for 8.0, I think:
https://github.com/odoo/odoo/blob/8.0/openerp/osv/expression.py#L1040

> I was trying to adjust for that replacing time.strftime('%Y-%m-%d') with
>
> (datetime.datetime.now()+datetime.timedelta(hours=6)).strftime('%Y-%m-%d
>  %H:%M:%S')
> as all records created today after 18:00 will have a database timestamp of
> tomorrow...
> Problem is JS complains about invalid operands for "+": datetime and
> timedelta...

That's because the '+' operator is only defined on timedelta + timedelta:
https://github.com/odoo/odoo/blob/8.0/addons/web/static/src/js/pyeval.js#L302
 
> Any idea how could this be acomplished ?

(datetime.datetime.now()+relativedelta(hours=6)).strftime('%Y-%m-%d %H:%M:%S')

but I think you won't be completely happy with that, as what you actually want
to search for is presumingly:
[utc representation of $localdate 00:00:00] <= date_field <= [utc
representation of $localdate 23:59:59]
and that's where the above patch comes in handy.

Regards,
Holger
--
Therp - Maatwerk in open ontwikkeling

Holger Brunn - Ontwerp en implementatie

mail: [hidden email]
web: http://therp.nl
_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp

signature.asc (220 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Openerp-community] How to use datetime + timedelta on odoo filters

Holger Brunn
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

I created a PR for that: https://github.com/odoo/odoo/pull/2759

- --
Therp - Maatwerk in open ontwikkeling

Holger Brunn - Ontwerp en implementatie

mail: [hidden email]
web: http://therp.nl
phone: +31 (0)20 3093096
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iF4EAREIAAYFAlQpFQ0ACgkQAcl2D+yjrhi3qQD9FzDvmXsGv8pQx01KNmQyxbZx
1RaY5OkATXj+JH/9W7wA/iDxVGK0OBrQwMYMYXeMMQ48sS2QZt3qK4EcZAbthSxT
=C97g
-----END PGP SIGNATURE-----

_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Openerp-community] How to use datetime + timedelta on odoo filters

Nhomar Hernández

2014-09-29 3:45 GMT-04:30 Holger Brunn <[hidden email]>:

I created a PR for that: https://github.com/odoo/odoo/pull/2759

It is runbot red :-(


--
--------------------
Saludos Cordiales
 
 

_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Openerp-community] How to use datetime + timedelta on odoo filters

Holger Brunn
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

> It is runbot red :-(

Thanks Nhomar, not any more.

- --
Therp - Maatwerk in open ontwikkeling

Holger Brunn - Ontwerp en implementatie

mail: [hidden email]
web: http://therp.nl
phone: +31 (0)20 3093096
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iF4EAREIAAYFAlQpbKoACgkQAcl2D+yjrhjqigD+JK8rpY0htc5Xdau5czGqe2vA
tHjZWZKuN1LAoPx+Z5EBAK5gr+0zD73uaSKY6gV7OdG/99QBx4GI2+Tiq6qJZfEP
=ETF8
-----END PGP SIGNATURE-----

_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Openerp-community] How to use datetime + timedelta on odoo filters

Mario Arias
In reply to this post by Mario Arias
relativedelta in pyeval.js is only implementing year, month and day... !!
so adding hours will not work as expected...

I will try Holger's  patch, as anyway it seems the right place to fix this...

If S.A. experts could take a look and comment on this, it would be great !!   ;-)

Regards,
-Mario


_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Openerp-community] How to use datetime + timedelta on odoo filters

Holger Brunn
> 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: [hidden email]
web: http://therp.nl
_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp

signature.asc (220 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Openerp-community] How to use datetime + timedelta on odoo filters

Mario Arias
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 <[hidden email]> 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: [hidden email]
web: http://therp.nl

_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp



_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Openerp-community] How to use datetime + timedelta on odoo filters

Mario Arias
In reply to this post by Holger Brunn
Also, using in filters
    (datetime.datetime.now()+relativedelta(hours=6)).strftime('%Y-%m-%d %H:%M:%S')    

is  not working on v8.  Would this be solved with web_relativedelta module ?
I checked and it is not in use even on 7.0...

Has that been deprecated on xml files ?



On Sun, Sep 28, 2014 at 11:48 PM, Holger Brunn <[hidden email]> wrote:
Hi Mario,

> Example:
> If user timezone is GMT-6:00, then any of current "Today" filters will
> filter only records from tomorrow if current local time is after 18:00
>  (18:00 + 6:00 = 24:00 -->> next day).

this is caused by https://bugs.launchpad.net/ocb-server/7.0/+bug/1255612, my
patch should still work for 8.0, I think:
https://github.com/odoo/odoo/blob/8.0/openerp/osv/expression.py#L1040

> I was trying to adjust for that replacing time.strftime('%Y-%m-%d') with
>
> (datetime.datetime.now()+datetime.timedelta(hours=6)).strftime('%Y-%m-%d
>  %H:%M:%S')
> as all records created today after 18:00 will have a database timestamp of
> tomorrow...
> Problem is JS complains about invalid operands for "+": datetime and
> timedelta...

That's because the '+' operator is only defined on timedelta + timedelta:
https://github.com/odoo/odoo/blob/8.0/addons/web/static/src/js/pyeval.js#L302

> Any idea how could this be acomplished ?

(datetime.datetime.now()+relativedelta(hours=6)).strftime('%Y-%m-%d %H:%M:%S')

but I think you won't be completely happy with that, as what you actually want
to search for is presumingly:
[utc representation of $localdate 00:00:00] <= date_field <= [utc
representation of $localdate 23:59:59]
and that's where the above patch comes in handy.

Regards,
Holger
--
Therp - Maatwerk in open ontwikkeling

Holger Brunn - Ontwerp en implementatie

mail: [hidden email]
web: http://therp.nl

_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp



_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Openerp-community] How to use datetime + timedelta on odoo filters

Holger Brunn
In reply to this post by Mario Arias
> Great for relativedelta !!  Have you tried it on odoo 8?

no, but it should be simple to port if there's anything to do at all.

[...]
> 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...

yes, you're right of course.

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

PR it into my branch, after another week of inactivity I'll propose this to
OCB.

On Saturday, October 04, 2014 03:13:03 you wrote:
> Also, using in filters
>     (datetime.datetime.now()+relativedelta(hours=6)).strftime('%Y-%m-%d
> %H:%M:%S')
> is  not working on v8.  Would this be solved with web_relativedelta module ?
> I checked and it is not in use even on 7.0...

This can't work with the original, as it doesn't support datetime as operands
for relativedelta. web_relativedelta is meant to solve that problem, too

--
Therp - Maatwerk in open ontwikkeling

Holger Brunn - Ontwerp en implementatie

mail: [hidden email]
web: http://therp.nl
_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp

signature.asc (220 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Openerp-community] How to use datetime + timedelta on odoo filters

Ovnicraft
In reply to this post by Nhomar Hernández


On Mon, Sep 29, 2014 at 6:50 AM, Nhomar Hernández <[hidden email]> wrote:

2014-09-29 3:45 GMT-04:30 Holger Brunn <[hidden email]>:

I created a PR for that: https://github.com/odoo/odoo/pull/2759

It is runbot red :-(


Runbot was updated and green now, why odoo dont get in touch with this script kiddie bugs ?

--
--------------------
Saludos Cordiales
 
 

_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp




--

_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Openerp-community] How to use datetime + timedelta on odoo filters

Mario Arias
I have it all working now, just by fixing original Odoo modules...

What is needed:
  • Define context_to_utc_timestamp, inverse function to context_timestamp...  PR : https://github.com/odoo/odoo/pull/2914
  • Update PR #2776 from Holger, to use "context_to_utc_timestamp" instead of "context_timestamp"
  • For filters in views, use domain tuples like ('date','&lt;=',context_today().strftime('%%Y-%%m-%%d').  This is important, as filter date should be based on user's context
  • If you want time deltas, then use something like (context_today()+relativedelta(days=-1)).strftime('%Y-%m-%d')
  • Finally, if you need replace, it is not defined for dates.  I made a PR for that too...  https://github.com/odoo/odoo/pull/2915

Regards,
-Mario

On Sat, Oct 4, 2014 at 9:55 AM, Ovnicraft <[hidden email]> wrote:


On Mon, Sep 29, 2014 at 6:50 AM, Nhomar Hernández <[hidden email]> wrote:

2014-09-29 3:45 GMT-04:30 Holger Brunn <[hidden email]>:

I created a PR for that: https://github.com/odoo/odoo/pull/2759

It is runbot red :-(


Runbot was updated and green now, why odoo dont get in touch with this script kiddie bugs ?

--
--------------------
Saludos Cordiales
 
 

_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp




--

_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp



_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : [hidden email]
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp
Loading...