From 39ea946edda7508dcc379ec112a0fea18ef99bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 31 Oct 2018 18:15:57 +0100 Subject: [PATCH] Remove custom strftime for year before 1900 The bug has been fixed in Python 3.3 issue7814 review48671002 --- trytond/ir/lang.py | 5 ++- trytond/ir/sequence.py | 7 ++-- trytond/tests/test_tools.py | 18 ++-------- trytond/tools/__init__.py | 1 - trytond/tools/datetime_strftime.py | 54 ------------------------------ 5 files changed, 7 insertions(+), 78 deletions(-) delete mode 100644 trytond/tools/datetime_strftime.py diff --git a/trytond/ir/lang.py b/trytond/ir/lang.py index fbf78b2be5..eb1e2d1ab0 100644 --- a/trytond/ir/lang.py +++ b/trytond/ir/lang.py @@ -12,7 +12,6 @@ from sql import Table from ..model import ModelView, ModelSQL, DeactivableMixin, fields, Check from ..cache import Cache -from ..tools import datetime_strftime from ..transaction import Transaction from ..pool import Pool from .time_locale import TIME_LOCALE @@ -212,7 +211,7 @@ class Lang(DeactivableMixin, ModelSQL, ModelView): for lang in langs: date = lang.date try: - datetime_strftime(datetime.datetime.now(), date) + datetime.datetime.now().strftime(date) except Exception: cls.raise_user_error('invalid_date', { 'format': lang.date, @@ -478,7 +477,7 @@ class Lang(DeactivableMixin, ModelSQL, ModelView): format = format.replace('%p', TIME_LOCALE[code]['%p'][datetime.timetuple()[3] < 12 and 0 or 1]) - return datetime_strftime(datetime, format) + return datetime.strftime(format) def get_parent_language(code): diff --git a/trytond/ir/sequence.py b/trytond/ir/sequence.py index 4003eac6ee..641765b64d 100644 --- a/trytond/ir/sequence.py +++ b/trytond/ir/sequence.py @@ -6,7 +6,6 @@ import time from sql import Literal, For from ..model import ModelView, ModelSQL, DeactivableMixin, fields, Check -from ..tools import datetime_strftime from ..pyson import Eval, And from ..transaction import Transaction from ..pool import Pool @@ -283,9 +282,9 @@ class Sequence(DeactivableMixin, ModelSQL, ModelView): if not date: date = Date.today() return { - 'year': datetime_strftime(date, '%Y'), - 'month': datetime_strftime(date, '%m'), - 'day': datetime_strftime(date, '%d'), + 'year': date.strftime('%Y'), + 'month': date.strftime('%m'), + 'day': date.strftime('%d'), } @staticmethod diff --git a/trytond/tests/test_tools.py b/trytond/tests/test_tools.py index 59b55a77fa..6ff5feaae5 100644 --- a/trytond/tests/test_tools.py +++ b/trytond/tests/test_tools.py @@ -4,12 +4,11 @@ import unittest import doctest -import datetime import sql import sql.operators -from trytond.tools import reduce_ids, datetime_strftime, \ - reduce_domain, decimal_, is_instance_method, file_open +from trytond.tools import ( + reduce_ids, reduce_domain, decimal_, is_instance_method, file_open) class ToolsTestCase(unittest.TestCase): @@ -59,19 +58,6 @@ class ToolsTestCase(unittest.TestCase): | (self.table.id.in_([15.0, 18.0, 19.0, 21.0])))) self.assertRaises(AssertionError, reduce_ids, self.table.id, [1.1]) - def test_datetime_strftime(self): - 'Test datetime_strftime' - self.assertTrue(datetime_strftime(datetime.date(2005, 3, 2), - '%Y-%m-%d'), '2005-03-02') - self.assertTrue(datetime_strftime(datetime.date(1805, 3, 2), - '%Y-%m-%d'), '1805-03-02') - self.assertTrue(datetime_strftime(datetime.datetime(2005, 3, 2, 0, 0, 0), - '%Y-%m-%d'), '2005-03-02') - with self.assertRaises(TypeError): - datetime_strftime(None, '%Y-%m-%d') - with self.assertRaises(TypeError): - datetime_strftime(2, '%Y-%m-%d') - def test_reduce_domain(self): 'Test reduce_domain' clause = ('x', '=', 'x') diff --git a/trytond/tools/__init__.py b/trytond/tools/__init__.py index 96f012e929..227e90d542 100644 --- a/trytond/tools/__init__.py +++ b/trytond/tools/__init__.py @@ -3,7 +3,6 @@ from .misc import * -from .datetime_strftime import * from .decimal_ import * diff --git a/trytond/tools/datetime_strftime.py b/trytond/tools/datetime_strftime.py deleted file mode 100644 index 4d34774236..0000000000 --- a/trytond/tools/datetime_strftime.py +++ /dev/null @@ -1,54 +0,0 @@ -# This file is part of Tryton. The COPYRIGHT file at the top level of -# this repository contains the full copyright notices and license terms. -# Copyright (c) 2002-2007 John D. Hunter; All Rights Reserved -import datetime -import time - - -def datetime_strftime(date, fmt): - ''' - Allow datetime strftime formatting for years before 1900. - See http://bugs.python.org/issue1777412 - ''' - if not isinstance(date, datetime.date): - raise TypeError('datetime_strftime requires a ''datetime.date'' object' - 'but received a ''%s''' % type(date)) - if date.year > 1900: - return date.strftime(fmt) - - def _findall(text, substr): - # Also finds overlaps - sites = [] - i = 0 - while True: - j = text.find(substr, i) - if j == -1: - break - sites.append(j) - i = j + 1 - return sites - - year = date.year - # For every non-leap year century, advance by - # 6 years to get into the 28-year repeat cycle - delta = 2000 - year - off = 6 * (delta // 100 + delta // 400) - year = year + off - # Move to around the year 2000 - year = year + ((2000 - year) // 28) * 28 - timetuple = date.timetuple() - string1 = time.strftime(fmt, (year,) + timetuple[1:]) - sites1 = _findall(string1, str(year)) - - string2 = time.strftime(fmt, (year + 28,) + timetuple[1:]) - sites2 = _findall(string2, str(year + 28)) - - sites = [] - for site in sites1: - if site in sites2: - sites.append(site) - - syear = "%4d" % (date.year,) - for site in sites: - string1 = string1[:site] + syear + string1[site + 4:] - return string1