How to use tzlocal - 10 common examples

To help you get started, we’ve selected a few tzlocal examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github eoyilmaz / stalker / tests / models / test_time_log.py View on Github external
from stalker.db.session import DBSession
        DBSession.add(time_log1)
        DBSession.commit()

        # time_log2
        kwargs["start"] = \
            datetime.datetime(2013, 3, 22, 4, 0, tzinfo=pytz.utc)
        kwargs["duration"] = datetime.timedelta(15)

        from stalker.exceptions import OverBookedError
        with pytest.raises(OverBookedError) as cm:
            TimeLog(**kwargs)

        import tzlocal
        local_tz = tzlocal.get_localzone()
        assert str(cm.value) == \
            'The resource has another TimeLog between %s and %s' % (
                datetime.datetime(2013, 3, 17, 4, 0, tzinfo=pytz.utc)
                    .astimezone(local_tz),
                datetime.datetime(2013, 4, 1, 4, 0, tzinfo=pytz.utc)
                    .astimezone(local_tz),
            )
github ilius / starcal / tzlocal / tests.py View on Github external
def test_only_localtime(self):
        local_path = os.path.split(__file__)[0]
        tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'localtime'))
        self.assertEqual(tz.zone, 'local')
        dt = datetime(2012, 1, 1, 5)
        self.assertEqual(pytz.timezone('Africa/Harare').localize(dt), tz.localize(dt))
github ilius / starcal / tzlocal / tests.py View on Github external
def test_timezone(self):
        # Most versions of Ubuntu
        local_path = os.path.split(__file__)[0]
        tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'timezone'))
        self.assertEqual(tz.zone, 'Africa/Harare')
github ilius / starcal / tzlocal / tests.py View on Github external
# Some Unices allow this as well, so we must allow it:
        tz_harare = tzlocal.unix._tz_from_env('Africa/Harare')
        self.assertEqual(tz_harare.zone, 'Africa/Harare')

        local_path = os.path.split(__file__)[0]
        tz_local = tzlocal.unix._tz_from_env(':' + os.path.join(local_path, 'test_data', 'Harare'))
        self.assertEqual(tz_local.zone, 'local')
        # Make sure the local timezone is the same as the Harare one above.
        # We test this with a past date, so that we don't run into future changes
        # of the Harare timezone.
        dt = datetime(2012, 1, 1, 5)
        self.assertEqual(tz_harare.localize(dt), tz_local.localize(dt))

        # Non-zoneinfo timezones are not supported in the TZ environment.
        self.assertRaises(pytz.UnknownTimeZoneError, tzlocal.unix._tz_from_env, 'GMT+03:00')
github ilius / starcal / tzlocal / tests.py View on Github external
def test_env(self):
        tz_harare = tzlocal.unix._tz_from_env(':Africa/Harare')
        self.assertEqual(tz_harare.zone, 'Africa/Harare')

        # Some Unices allow this as well, so we must allow it:
        tz_harare = tzlocal.unix._tz_from_env('Africa/Harare')
        self.assertEqual(tz_harare.zone, 'Africa/Harare')

        local_path = os.path.split(__file__)[0]
        tz_local = tzlocal.unix._tz_from_env(':' + os.path.join(local_path, 'test_data', 'Harare'))
        self.assertEqual(tz_local.zone, 'local')
        # Make sure the local timezone is the same as the Harare one above.
        # We test this with a past date, so that we don't run into future changes
        # of the Harare timezone.
        dt = datetime(2012, 1, 1, 5)
        self.assertEqual(tz_harare.localize(dt), tz_local.localize(dt))

        # Non-zoneinfo timezones are not supported in the TZ environment.
github ilius / starcal / tzlocal / tests.py View on Github external
def test_timezone_setting(self):
        # A ZONE setting in /etc/conf.d/clock, f ex Gentoo
        local_path = os.path.split(__file__)[0]
        tz = tzlocal.unix._get_localzone(_root=os.path.join(local_path, 'test_data', 'timezone_setting'))
        self.assertEqual(tz.zone, 'Africa/Harare')
github mozilla / MozDef / tests / mq / test_esworker_eventtask.py View on Github external
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# Copyright (c) 2017 Mozilla Corporation

import pytz
import tzlocal
import datetime


def utc_timezone():
    return pytz.timezone('UTC')


tzlocal.get_localzone = utc_timezone


import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), "../../mq"))
from mq import esworker_eventtask


class MockOptions():
    @property
    def mozdefhostname(self):
        return 'sample'


class TestKeyMapping():
    def setup(self):
github regebro / tzlocal / tests / tests.py View on Github external
def test_vardbzoneinfo_setting(self):
        # A ZONE setting in /etc/conf.d/clock, f ex Gentoo

        tz = tzlocal.unix._get_localzone(_root=os.path.join(self.path, 'test_data', 'vardbzoneinfo'))
        self.assertEqual(tz.zone, 'Africa/Harare')
github pymedusa / Medusa / ext / tzlocal / tests.py View on Github external
def test_zone_setting(self):
        # A ZONE setting in /etc/sysconfig/clock, f ex CentOS

        tz = tzlocal.unix._get_localzone(_root=os.path.join(self.path, 'test_data', 'zone_setting'))
        self.assertEqual(tz.zone, 'Africa/Harare')
github regebro / tzlocal / tests / tests.py View on Github external
def test_fail(self):
        out = StringIO()
        stderr = sys.stderr
        try:
            sys.stderr = out
            tz = tzlocal.unix._get_localzone(
                _root=os.path.join(self.path, 'test_data'))
        finally:
            sys.stderr = stderr
        self.assertEqual(tz, pytz.utc)
        self.assertIn('Can not find any timezone configuration',
            out.getvalue())