Datetime format in .fodt report

I want to replace a time zone AND an output format to something like ‘September 17, 2020 4:25 pm’ in printed report that is parsed XML (the template is sale.fodt). I don’t understand — where is coded this conversion (I mean a field data into output view)?
I am ok to modify it manually by hardcoding that, but where? What python file is doing that job?

Constantine.

Solution:
I created a function field that converts from datetime field to char in the format and a timezone I needed:
NOTE: self.sch_time — a dateime field that keeps scheduled date and time


import pytz

        sch_time_tz = fields.Function(fields.Char('Scheduled time with TZ'), 'get_scheduled_time_with_tz')`

        ## Converts timezone and format for printed report
        def utc_to_local(self,utc_dt,timezone):
            tz_utc = pytz.timezone('UTC')
            return utc_dt.replace(tzinfo=pytz.utc).astimezone(tz=timezone)         

        def get_scheduled_time_with_tz(self, name):
            try:
                tz = pytz.timezone('US/Central')
                return "{0:%b %d, %Y %I:%M %p  %Z}".format(self.utc_to_local(self.sch_time,tz))
            except Exception as e:
                return 'Not set.';

In a XXXXX.fodt report I added sch_time_tz for time indication:

     <text:p text:style-name="P24">
     <text:placeholder text:placeholder-type="text">&lt;purchase.sch_time_tz or &apos;&apos;&gt;</text:placeholder>
     </text:p>

Hi

You can use the following syntax to convert the date to the desired format directly in the fodt document:

<format_date(datetime.datetime.now(),user.language, '%B %d ,%y %H:%M%p')>
1 Like

There is a proposal to add a native format_datetime function to reports. It will be great if you can test it.

1 Like

Cool,

I looked for that way but didn’t succeed in the search, thank you!
with that, using a fields.Function() gives me greater flexibility.

Constantine.

I just inserted the code (I didn’t use patch utility, I prefer to do that manually), as suggested here:
https://codereview.tryton.org/45521002/patch/315651002/309891004
to /trytond/report/report.py
and it works fine, as expected.

purchase.fodt:

<(format_datetime(purchase.sch_time, timezone='US/Central', format='%b %d ,%y %I:%M%p %Z'))>

————————
The following is a screenshot of the report. The upper — my text field (
‘purchase.sch_time_tz’), the lower — the proposed function’s result:
image

————————
So, when it will be available in the release?

Constantine.

It’s missing some more love on the code to be included on the next release. I will try to work on it but I can not guatanteee anything.

Also, I think it worth having a field in configuration to “nail down” a timezone for print reports and other things like that.
——————
What can I do for the community?
Say, I created (for myself) a check-list for XXX2Many fields for modules overriding/connecting: that is easy to forget to update tryton.cfg or _init.py or a model view.
It was poorly documented on to how to be sure everything is fine.
How and where I can share this list, for example?

Constantine.

Constantine.

We have such field defined on the company module and most of the transactional models are linked to a company. So you just have to pass the company timezone to the format_datetime function of your reports.

1 Like

I like it!
I left the hard-coded timezone for the client, although, I applied the proposed way in my sandbox and I like how it works. Will use that further.

C.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.