Edgewall Software

Ticket #8240: 8240-tzinfo-localize-r8816.patch

File 8240-tzinfo-localize-r8816.patch, 2.8 KB (added by rblank, 3 years ago)

Use tzinfo.localize() when constructing datetime objects.

  • trac/util/datefmt.py

    diff --git a/trac/util/datefmt.py b/trac/util/datefmt.py
    a b  
    4141 
    4242    Any other input will trigger a `TypeError`. 
    4343    """ 
     44    tzinfo = tzinfo or localtz 
    4445    if t is None: 
    45         return datetime.now(tzinfo or localtz) 
     46        return tzinfo.localize(datetime.now()) 
    4647    elif isinstance(t, datetime): 
    4748        return t 
    4849    elif isinstance(t, date): 
    49         return datetime(t.year, t.month, t.day, tzinfo=tzinfo or localtz) 
     50        return tzinfo.localize(datetime(t.year, t.month, t.day)) 
    5051    elif isinstance(t, (int,long,float)): 
    51         return datetime.fromtimestamp(t, tzinfo or localtz) 
     52        return tzinfo.localize(datetime.fromtimestamp(t)) 
    5253    raise TypeError('expecting datetime, int, long, float, or None; got %s' % 
    5354                    type(t)) 
    5455 
     
    232233        raise TracError('"%s" is an invalid date, or the date format ' 
    233234                        'is not known. Try "%s" instead.' % (text, hint), 
    234235                        'Invalid Date') 
    235     dt = datetime(*(tm[0:6] + (0, tzinfo))) 
     236    dt = tzinfo.localize(datetime(*tm[0:6])) 
    236237    # Make sure we can convert it to a timestamp and back - fromtimestamp() 
    237238    # may raise ValueError if larger than platform C localtime() or gmtime() 
    238239    try: 
     
    268269    def dst(self, dt): 
    269270        return _zero 
    270271 
     272    def localize(self, dt, is_dst=False): 
     273        return dt.replace(tzinfo=self) 
     274 
    271275 
    272276STDOFFSET = timedelta(seconds=-time.timezone) 
    273277if time.daylight: 
     
    307311        except OverflowError: 
    308312            return False 
    309313 
     314    def localize(self, dt, is_dst=False): 
     315        return dt.replace(tzinfo=self) 
     316 
    310317 
    311318utc = FixedOffset(0, 'UTC') 
    312319utcmin = datetime.min.replace(tzinfo=utc) 
  • trac/util/tests/datefmt.py

    diff --git a/trac/util/tests/datefmt.py b/trac/util/tests/datefmt.py
    a b  
    4242                             tz.utcoffset(None)) 
    4343            self.assertEqual('GMT +4:00', tz.zone) 
    4444 
     45        def test_parse_date(self): 
     46            tz = datefmt.get_timezone('Europe/Zurich') 
     47            t = datefmt.parse_date('2009-12-01T12:00:00', tz) 
     48            t_utc = datetime.datetime(2009, 12, 1, 11, 0, 0, 0, datefmt.utc) 
     49            self.assertEqual(t_utc, t) 
     50 
     51        def test_parse_date_dst(self): 
     52            tz = datefmt.get_timezone('Europe/Zurich') 
     53            t = datefmt.parse_date('2009-08-01T12:00:00', tz) 
     54            t_utc = datetime.datetime(2009, 8, 1, 10, 0, 0, 0, datefmt.utc) 
     55            self.assertEqual(t_utc, t) 
     56 
     57 
    4558class DateFormatTestCase(unittest.TestCase): 
    4659 
    4760    def test_to_datetime(self): 
     
    93106        self.assertEqual('2009-08-20',  
    94107                         datefmt.format_date(a_date, format='%Y-%m-%d')) 
    95108 
     109 
    96110def suite(): 
    97111    suite = unittest.TestSuite() 
    98112    if PytzTestCase: