Changeset 8185


Ignore:
Timestamp:
16 Apr 2012, 23:17:35 (13 years ago)
Author:
uli
Message:

Add datetime/timezone helpers urgently needed.

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  
    1818"""General helper functions for Kofa.
    1919"""
     20import datetime
    2021import os
     22import pytz
    2123import re
    2224import shutil
     
    550552            result.append((title, code))
    551553    return tuple(result)
     554
     555def 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
     564def 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  
    1919##
    2020
     21import datetime
    2122import os
     23import pytz
    2224import shutil
    2325import tempfile
     
    262264        self.assertEqual(sorted(result5), ['baz'])
    263265        return
     266
     267class 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
    264306
    265307def test_suite():
     
    275317        FileSizeTestCase,
    276318        IfaceNamesTestCase,
     319        DateTimeHelpersTestCase,
    277320        ]:
    278321        suite.addTests(
Note: See TracChangeset for help on using the changeset viewer.