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?
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"><purchase.sch_time_tz or ''></text:placeholder>
</text:p>
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?
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.
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.