diff --git a/trac/util/datefmt.py b/trac/util/datefmt.py
|
a
|
b
|
|
| 41 | 41 | |
| 42 | 42 | Any other input will trigger a `TypeError`. |
| 43 | 43 | """ |
| | 44 | tzinfo = tzinfo or localtz |
| 44 | 45 | if t is None: |
| 45 | | return datetime.now(tzinfo or localtz) |
| | 46 | return tzinfo.localize(datetime.now()) |
| 46 | 47 | elif isinstance(t, datetime): |
| 47 | 48 | return t |
| 48 | 49 | 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)) |
| 50 | 51 | elif isinstance(t, (int,long,float)): |
| 51 | | return datetime.fromtimestamp(t, tzinfo or localtz) |
| | 52 | return tzinfo.localize(datetime.fromtimestamp(t)) |
| 52 | 53 | raise TypeError('expecting datetime, int, long, float, or None; got %s' % |
| 53 | 54 | type(t)) |
| 54 | 55 | |
| … |
… |
|
| 232 | 233 | raise TracError('"%s" is an invalid date, or the date format ' |
| 233 | 234 | 'is not known. Try "%s" instead.' % (text, hint), |
| 234 | 235 | 'Invalid Date') |
| 235 | | dt = datetime(*(tm[0:6] + (0, tzinfo))) |
| | 236 | dt = tzinfo.localize(datetime(*tm[0:6])) |
| 236 | 237 | # Make sure we can convert it to a timestamp and back - fromtimestamp() |
| 237 | 238 | # may raise ValueError if larger than platform C localtime() or gmtime() |
| 238 | 239 | try: |
| … |
… |
|
| 268 | 269 | def dst(self, dt): |
| 269 | 270 | return _zero |
| 270 | 271 | |
| | 272 | def localize(self, dt, is_dst=False): |
| | 273 | return dt.replace(tzinfo=self) |
| | 274 | |
| 271 | 275 | |
| 272 | 276 | STDOFFSET = timedelta(seconds=-time.timezone) |
| 273 | 277 | if time.daylight: |
| … |
… |
|
| 307 | 311 | except OverflowError: |
| 308 | 312 | return False |
| 309 | 313 | |
| | 314 | def localize(self, dt, is_dst=False): |
| | 315 | return dt.replace(tzinfo=self) |
| | 316 | |
| 310 | 317 | |
| 311 | 318 | utc = FixedOffset(0, 'UTC') |
| 312 | 319 | utcmin = datetime.min.replace(tzinfo=utc) |
diff --git a/trac/util/tests/datefmt.py b/trac/util/tests/datefmt.py
|
a
|
b
|
|
| 42 | 42 | tz.utcoffset(None)) |
| 43 | 43 | self.assertEqual('GMT +4:00', tz.zone) |
| 44 | 44 | |
| | 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 | |
| 45 | 58 | class DateFormatTestCase(unittest.TestCase): |
| 46 | 59 | |
| 47 | 60 | def test_to_datetime(self): |
| … |
… |
|
| 93 | 106 | self.assertEqual('2009-08-20', |
| 94 | 107 | datefmt.format_date(a_date, format='%Y-%m-%d')) |
| 95 | 108 | |
| | 109 | |
| 96 | 110 | def suite(): |
| 97 | 111 | suite = unittest.TestSuite() |
| 98 | 112 | if PytzTestCase: |