← Back to team overview

openerp-india team mailing list archive

[Bug 979550] Re: Needing standard/good functions for date/time output in user's timezone for account reports

 

The OpenERP framework does have methods to compute/output datetime values in the timezone of the user.
You should never need to use pytz manually to accomplish this, that's one of the reasons the current report engine does not provide pytz in the report context - you don't need it.

API-wise, as of OpenERP 6.1 the fields.date and fields.datetime classes have relevant static methods: http://bazaar.launchpad.net/~openerp/openobject-server/6.1/view/4334/openerp/osv/fields.py#L261
 - fields.date.today() returns the current date in default value format for a ``date`` field (meant for _defaults)
 - fields.date.context_today(model, timestamp) returns the current date or specified timestamp as seen in the contextual timezone, in a format fit for date fields (meant only for special use, e.g. reports)
 - fields.datetime.now() returns the current datetime in default value format for a ``datetime`` field (meant for _defaults)
 - fields.datetime.context_timestamp(timestamp) returns the given timestamp converted to the contextual timezone (meant only for special use, e.g. reports)

However you should never need to call context_timestamp directly in report, as formatLang() will do it for you, based on the current user's timezone. I'm not sure why you call it superfluous or brittle.
formatLang() does what is necessary 99% of the time: take a datetime value from a database record, and return a string that can be printed in a report for a specific user, in the appropriate format. Customizing the output format is not necessary in most cases, the date/time format is specified on the language itself, so using it everywhere is better for consistency. Using another timezone than that of the report context is also very uncommon.

So we mainly need to cover the case where you want to output the report printing date/time in the current user's timezone - that could indeed be done with extra now() and today() methods in the report context, doing what formatLang() does but with the current date/time.
We can of course add pytz and datetime to the report context, but using them should really be an exception.

Feel free to submit a merge proposal for this :-)

BTW, if you notice cases where reports are outputting incorrect datetime
values, those should be separate bugs (missing formatLang calls), and
are easily corrected.

Thanks!

-- 
You received this bug notification because you are a member of OpenERP
Indian Team, which is subscribed to OpenERP Server.
https://bugs.launchpad.net/bugs/979550

Title:
  Needing standard/good functions for date/time output in user's
  timezone for account reports

Status in OpenERP Server:
  Confirmed

Bug description:
  I've run into a problem of not having correct timezones applied when
  generating reports. This is partly the fault of the server's timezone
  being forced to UTC, but also due to the lack of availability of easy-
  to-use functions in the templates used to generate these reports.

  What I have done as a proof of concept is to add "import pytz" into
  openobject-server/openerp/report/report_sxw.py, then add pytz and
  datetime into the localcontext member variable in rmlparse.

  ---- DIFF ----

  index 27bd5fa..ad2c341 100644
  --- a/openerp/report/report_sxw.py
  +++ b/openerp/report/report_sxw.py
  @@ -26,6 +26,7 @@ from datetime import datetime
   import os
   import re
   import time
  +import pytz
   from interface import report_rml
   import preprocess
   import logging
  @@ -168,6 +169,8 @@ class rml_parse(object):
               'setHtmlImage' : self.set_html_image,
               'strip_name' : self._strip_name,
               'time' : time,
  +            'pytz': pytz,
  +            'datetime': datetime,
               'display_address': self.display_address,
               # more context members are setup in setCompany() below:
               #  - company_id
  ---- DIFF ----

  This change allows me to call pytz and datetime from within the report
  templates (most specifically I've been looking at the internal
  header/footer templates for companies). I find this to be preferable
  over formatLang and time.strftime calls, which do not really allow for
  timezone changes. formatLang is something of an exception here - it is
  meant to take a date or datetime string of a particular format and
  correct the timezone for it, however this breaks easily, and seems
  superfluous.

  What I use currently for correct date and time in the templates are:
  Date: [[ datetime.now(python.timezone(tz)).strftime('%Y-%m-%d') ]]
  Time: [[ datetime.now(python.timezone(tz)).strftime('%H:%M') ]]

  What I think would be much better is easy access to standard objects/functions that will output dates, times and datetimes in the user's timezone. So that we might call them with something like:
  Date: [[ utime.today() ]]
  Time: [[ utime.localtime() ]]
  Datetime: [[ utime.now() ]]

  I would also like the flexibility to provide my own strftime string.
  I have no attachment to these names for functions/namespaces/objects.

To manage notifications about this bug go to:
https://bugs.launchpad.net/openobject-server/+bug/979550/+subscriptions


References