changeset da4aa730aba2 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=da4aa730aba2
description:
        Add format_datetime to Report

        issue7202
        review45521002
diffstat:

 CHANGELOG                    |   1 +
 trytond/ir/lang.py           |   3 +++
 trytond/report/report.py     |  16 ++++++++++++++++
 trytond/tests/test_ir.py     |   6 ++++++
 trytond/tests/test_report.py |  16 ++++++++++++++++
 5 files changed, 42 insertions(+), 0 deletions(-)

diffs (111 lines):

diff -r 75402881aefc -r da4aa730aba2 CHANGELOG
--- a/CHANGELOG Sun Oct 04 00:35:23 2020 +0200
+++ b/CHANGELOG Sun Oct 04 23:04:01 2020 +0200
@@ -1,3 +1,4 @@
+* Add format_datetime to Report
 * Allow sharing view searches
 * Allow overriding digits in Lang.currency and Report.format_currency
 * Add record name in report filename
diff -r 75402881aefc -r da4aa730aba2 trytond/ir/lang.py
--- a/trytond/ir/lang.py        Sun Oct 04 00:35:23 2020 +0200
+++ b/trytond/ir/lang.py        Sun Oct 04 23:04:01 2020 +0200
@@ -520,7 +520,10 @@
         Day = pool.get('ir.calendar.day')
         if format is None:
             format = self.date
+            if isinstance(value, datetime.datetime):
+                format += ' %H:%M:%S'
         format = format.replace('%x', self.date)
+        format = format.replace('%X', '%H:%M:%S')
         if isinstance(value, datetime.date):
             for f, i, klass in (('%A', 6, Day), ('%B', 1, Month)):
                 for field, f in [('name', f), ('abbreviation', f.lower())]:
diff -r 75402881aefc -r da4aa730aba2 trytond/report/report.py
--- a/trytond/report/report.py  Sun Oct 04 00:35:23 2020 +0200
+++ b/trytond/report/report.py  Sun Oct 04 23:04:01 2020 +0200
@@ -1,6 +1,7 @@
 # This file is part of Tryton.  The COPYRIGHT file at the top level of
 # this repository contains the full copyright notices and license terms.
 import datetime
+import dateutil.tz
 import os
 import inspect
 import logging
@@ -244,6 +245,7 @@
         report_context['records'] = records
         report_context['record'] = records[0] if records else None
         report_context['format_date'] = cls.format_date
+        report_context['format_datetime'] = cls.format_datetime
         report_context['format_timedelta'] = cls.format_timedelta
         report_context['format_currency'] = cls.format_currency
         report_context['format_number'] = cls.format_number
@@ -340,6 +342,20 @@
         return lang.strftime(value, format=format)
 
     @classmethod
+    def format_datetime(cls, value, lang=None, format=None, timezone=None):
+        pool = Pool()
+        Lang = pool.get('ir.lang')
+        if lang is None:
+            lang = Lang.get()
+        if value.tzinfo is None:
+            value = value.replace(tzinfo=dateutil.tz.tzutc())
+        if timezone:
+            if isinstance(timezone, str):
+                timezone = dateutil.tz.gettz(timezone)
+            value = value.astimezone(timezone)
+        return lang.strftime(value, format)
+
+    @classmethod
     def format_timedelta(cls, value, converter=None, lang=None):
         pool = Pool()
         Lang = pool.get('ir.lang')
diff -r 75402881aefc -r da4aa730aba2 trytond/tests/test_ir.py
--- a/trytond/tests/test_ir.py  Sun Oct 04 00:35:23 2020 +0200
+++ b/trytond/tests/test_ir.py  Sun Oct 04 23:04:01 2020 +0200
@@ -95,6 +95,12 @@
             (datetime.datetime(2018, 11, 1, 14, 30), '%a %d %b %Y %I:%M %p',
                 "Thu 01 Nov 2018 02:30 PM"),
             (datetime.date(2018, 11, 1), '%x', "11/01/2018"),
+            (datetime.datetime(2018, 11, 1, 14, 30, 12),
+                '%x %X', "11/01/2018 14:30:12"),
+            (datetime.datetime(2018, 11, 1, 14, 30, 12),
+                '%H:%M:%S', "14:30:12"),
+            (datetime.datetime(2018, 11, 1, 14, 30, 12), None,
+                "11/01/2018 14:30:12"),
             ]
         for date, format_, result in test_data:
             self.assertEqual(lang.strftime(date, format_), result)
diff -r 75402881aefc -r da4aa730aba2 trytond/tests/test_report.py
--- a/trytond/tests/test_report.py      Sun Oct 04 00:35:23 2020 +0200
+++ b/trytond/tests/test_report.py      Sun Oct 04 23:04:01 2020 +0200
@@ -2,6 +2,7 @@
 # this repository contains the full copyright notices and license terms.
 import unittest
 import datetime
+
 from trytond.tests.test_tryton import activate_module, with_transaction
 from trytond.report.report import Report
 
@@ -13,6 +14,21 @@
     def setUpClass(cls):
         activate_module('tests')
 
+    @with_transaction()
+    def test_format_datetime(self):
+        "Test format datetime"
+        self.assertEqual(Report.format_datetime(
+                datetime.datetime(2020, 7, 8, 13, 30, 00)),
+            '07/08/2020 13:30:00')
+
+    @with_transaction()
+    def test_format_datetime_custom_format(self):
+        "Test format datetime custom format"
+        self.assertEqual(Report.format_datetime(
+                datetime.datetime(2020, 7, 8, 13, 30, 00),
+                format='%d %b %Y %I:%M %p'),
+            "08 Jul 2020 01:30 PM"),
+
 
 def create_test_format_timedelta(i, in_, out):
     @with_transaction()

Reply via email to