Changeset 8185
- Timestamp:
- 16 Apr 2012, 23:17:35 (13 years ago)
- Location:
- main/waeup.kofa/trunk/src/waeup/kofa/utils
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.kofa/trunk/src/waeup/kofa/utils/helpers.py
r7968 r8185 18 18 """General helper functions for Kofa. 19 19 """ 20 import datetime 20 21 import os 22 import pytz 21 23 import re 22 24 import shutil … … 550 552 result.append((title, code)) 551 553 return tuple(result) 554 555 def now(tz=None): 556 """Get current datetime in timezone of `tz`. 557 558 If `tz`, a `tzinfo` instance, is None, UTC time is returned. 559 560 `tz` should be a timezone as defined in pytz. 561 """ 562 return to_timezone(datetime.datetime.utcnow(), tz=tz) 563 564 def to_timezone(dt, tz=None): 565 """Shift datetime into timezone `tz`. 566 567 If datetime `dt` contains no `tzinfo` (i.e. it is 'naive'), it is 568 assumed to be UTC. 569 570 If no `tz` is given, shift to UTC is performed. 571 """ 572 if tz is None: 573 tz = pytz.utc 574 if dt.tzinfo is None: 575 dt = pytz.utc.localize(dt) 576 return tz.normalize(dt.tzinfo.normalize(dt).astimezone(tz)) -
main/waeup.kofa/trunk/src/waeup/kofa/utils/tests/test_helpers.py
r7942 r8185 19 19 ## 20 20 21 import datetime 21 22 import os 23 import pytz 22 24 import shutil 23 25 import tempfile … … 262 264 self.assertEqual(sorted(result5), ['baz']) 263 265 return 266 267 class DateTimeHelpersTestCase(unittest.TestCase): 268 269 def test_now(self): 270 tz_berlin = pytz.timezone('Europe/Berlin') 271 result1 = helpers.now() 272 result2 = helpers.now(tz_berlin) 273 self.assertEqual(result1.tzinfo, pytz.utc) 274 self.assertFalse(result2.tzinfo == pytz.utc) 275 self.assertFalse(result2.tzinfo is None) 276 return 277 278 def test_to_timezone(self): 279 fmt = '%Y-%m-%d %H:%M:%S %Z%z' 280 tz_berlin = pytz.timezone('Europe/Berlin') 281 tz_lagos = pytz.timezone('Africa/Lagos') 282 dt1 = datetime.datetime(2012, 1, 1, 0, 0) 283 dt2 = datetime.datetime(2012, 1, 1, 0, 0, tzinfo=tz_berlin) 284 dt3 = datetime.datetime(2012, 6, 1, 0, 0, tzinfo=tz_lagos) 285 dt4 = datetime.datetime(2012, 6, 1, 0, 0, tzinfo=tz_berlin) 286 result1 = helpers.to_timezone(dt1) 287 result2 = helpers.to_timezone(dt1, pytz.utc) 288 result3 = helpers.to_timezone(dt2) 289 result4 = helpers.to_timezone(dt2, tz_lagos) 290 result5 = helpers.to_timezone(dt3, tz_berlin) 291 result6 = helpers.to_timezone(dt4, tz_lagos) 292 self.assertEqual( 293 result1.strftime(fmt), '2012-01-01 00:00:00 UTC+0000') 294 self.assertEqual( 295 result2.strftime(fmt), '2012-01-01 00:00:00 UTC+0000') 296 self.assertEqual( 297 result3.strftime(fmt), '2011-12-31 23:00:00 UTC+0000') 298 self.assertEqual( 299 result4.strftime(fmt), '2012-01-01 00:00:00 WAT+0100') 300 self.assertEqual( 301 result5.strftime(fmt), '2012-06-01 01:46:00 CEST+0200') 302 self.assertEqual( 303 result6.strftime(fmt), '2012-06-01 00:00:00 WAT+0100') 304 return 305 264 306 265 307 def test_suite(): … … 275 317 FileSizeTestCase, 276 318 IfaceNamesTestCase, 319 DateTimeHelpersTestCase, 277 320 ]: 278 321 suite.addTests(
Note: See TracChangeset for help on using the changeset viewer.