Python dateutil.tz.localize() Examples

The following are 15 code examples of dateutil.tz.localize(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module dateutil.tz , or try the search function .
Example #1
Source File: test_timezones.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_infer_tz(eastern, localize):
    utc = pytz.utc

    start_naive = datetime(2001, 1, 1)
    end_naive = datetime(2009, 1, 1)

    start = localize(eastern, start_naive)
    end = localize(eastern, end_naive)

    assert (timezones.infer_tzinfo(start, end) is
            tslib._localize_pydatetime(start_naive, eastern).tzinfo)
    assert (timezones.infer_tzinfo(start, None) is
            tslib._localize_pydatetime(start_naive, eastern).tzinfo)
    assert (timezones.infer_tzinfo(None, end) is
            tslib._localize_pydatetime(end_naive, eastern).tzinfo)

    start = utc.localize(start_naive)
    end = utc.localize(end_naive)
    assert timezones.infer_tzinfo(start, end) is utc

    end = tslib._localize_pydatetime(end_naive, eastern)
    with pytest.raises(Exception):
        timezones.infer_tzinfo(start, end)
    with pytest.raises(Exception):
        timezones.infer_tzinfo(end, start) 
Example #2
Source File: test_dates.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_yearlocator_pytz():
    import pytz

    tz = pytz.timezone('America/New_York')
    x = [tz.localize(datetime.datetime(2010, 1, 1))
            + datetime.timedelta(i) for i in range(2000)]
    locator = mdates.AutoDateLocator(interval_multiples=True, tz=tz)
    locator.create_dummy_axis()
    locator.set_view_interval(mdates.date2num(x[0])-1.0,
                              mdates.date2num(x[-1])+1.0)

    np.testing.assert_allclose([733408.208333, 733773.208333, 734138.208333,
                                734503.208333, 734869.208333,
                                735234.208333, 735599.208333], locator())
    expected = ['2009-01-01 00:00:00-05:00',
                '2010-01-01 00:00:00-05:00', '2011-01-01 00:00:00-05:00',
                '2012-01-01 00:00:00-05:00', '2013-01-01 00:00:00-05:00',
                '2014-01-01 00:00:00-05:00', '2015-01-01 00:00:00-05:00']
    st = list(map(str, mdates.num2date(locator(), tz=tz)))
    assert st == expected 
Example #3
Source File: test_dates.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def test_yearlocator_pytz():
    import pytz

    tz = pytz.timezone('America/New_York')
    x = [tz.localize(datetime.datetime(2010, 1, 1))
            + datetime.timedelta(i) for i in range(2000)]
    locator = mdates.AutoDateLocator(interval_multiples=True, tz=tz)
    locator.create_dummy_axis()
    locator.set_view_interval(mdates.date2num(x[0])-1.0,
                              mdates.date2num(x[-1])+1.0)

    np.testing.assert_allclose([733408.208333, 733773.208333, 734138.208333,
                                734503.208333, 734869.208333,
                                735234.208333, 735599.208333], locator())
    expected = ['2009-01-01 00:00:00-05:00',
                '2010-01-01 00:00:00-05:00', '2011-01-01 00:00:00-05:00',
                '2012-01-01 00:00:00-05:00', '2013-01-01 00:00:00-05:00',
                '2014-01-01 00:00:00-05:00', '2015-01-01 00:00:00-05:00']
    st = list(map(str, mdates.num2date(locator(), tz=tz)))
    assert st == expected 
Example #4
Source File: test_timezones.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_infer_tz(eastern, localize):
    utc = pytz.utc

    start_naive = datetime(2001, 1, 1)
    end_naive = datetime(2009, 1, 1)

    start = localize(eastern, start_naive)
    end = localize(eastern, end_naive)

    assert (timezones.infer_tzinfo(start, end) is
            tslib._localize_pydatetime(start_naive, eastern).tzinfo)
    assert (timezones.infer_tzinfo(start, None) is
            tslib._localize_pydatetime(start_naive, eastern).tzinfo)
    assert (timezones.infer_tzinfo(None, end) is
            tslib._localize_pydatetime(end_naive, eastern).tzinfo)

    start = utc.localize(start_naive)
    end = utc.localize(end_naive)
    assert timezones.infer_tzinfo(start, end) is utc

    end = tslib._localize_pydatetime(end_naive, eastern)
    with pytest.raises(Exception):
        timezones.infer_tzinfo(start, end)
    with pytest.raises(Exception):
        timezones.infer_tzinfo(end, start) 
Example #5
Source File: test_timezones.py    From recruit with Apache License 2.0 5 votes vote down vote up
def infer_setup(request):
    eastern, localize = request.param

    start_naive = datetime(2001, 1, 1)
    end_naive = datetime(2009, 1, 1)

    start = localize(eastern, start_naive)
    end = localize(eastern, end_naive)

    return eastern, localize, start, end, start_naive, end_naive 
Example #6
Source File: test_timezones.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_infer_tz_utc_localize(infer_setup):
    _, _, start, end, start_naive, end_naive = infer_setup
    utc = pytz.utc

    start = utc.localize(start_naive)
    end = utc.localize(end_naive)

    assert timezones.infer_tzinfo(start, end) is utc 
Example #7
Source File: test_timezones.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_infer_tz_mismatch(infer_setup, ordered):
    eastern, _, _, _, start_naive, end_naive = infer_setup
    msg = "Inputs must both have the same timezone"

    utc = pytz.utc
    start = utc.localize(start_naive)
    end = conversion.localize_pydatetime(end_naive, eastern)

    args = (start, end) if ordered else (end, start)

    with pytest.raises(AssertionError, match=msg):
        timezones.infer_tzinfo(*args) 
Example #8
Source File: test_dates.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_rrulewrapper_pytz():
    # Test to make sure pytz zones are supported in rrules
    import pytz

    def attach_tz(dt, zi):
        return zi.localize(dt)

    _test_rrulewrapper(attach_tz, pytz.timezone) 
Example #9
Source File: test_timezones.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def infer_setup(request):
    eastern, localize = request.param

    start_naive = datetime(2001, 1, 1)
    end_naive = datetime(2009, 1, 1)

    start = localize(eastern, start_naive)
    end = localize(eastern, end_naive)

    return eastern, localize, start, end, start_naive, end_naive 
Example #10
Source File: test_timezones.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_infer_tz_utc_localize(infer_setup):
    _, _, start, end, start_naive, end_naive = infer_setup
    utc = pytz.utc

    start = utc.localize(start_naive)
    end = utc.localize(end_naive)

    assert timezones.infer_tzinfo(start, end) is utc 
Example #11
Source File: test_timezones.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_infer_tz_mismatch(infer_setup, ordered):
    eastern, _, _, _, start_naive, end_naive = infer_setup
    msg = "Inputs must both have the same timezone"

    utc = pytz.utc
    start = utc.localize(start_naive)
    end = conversion.localize_pydatetime(end_naive, eastern)

    args = (start, end) if ordered else (end, start)

    with pytest.raises(AssertionError, match=msg):
        timezones.infer_tzinfo(*args) 
Example #12
Source File: test_dates.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_rrulewrapper_pytz():
    # Test to make sure pytz zones are supported in rrules
    import pytz

    def attach_tz(dt, zi):
        return zi.localize(dt)

    _test_rrulewrapper(attach_tz, pytz.timezone) 
Example #13
Source File: test_timezones.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def infer_setup(request):
    eastern, localize = request.param

    start_naive = datetime(2001, 1, 1)
    end_naive = datetime(2009, 1, 1)

    start = localize(eastern, start_naive)
    end = localize(eastern, end_naive)

    return eastern, localize, start, end, start_naive, end_naive 
Example #14
Source File: test_timezones.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_infer_tz_utc_localize(infer_setup):
    _, _, start, end, start_naive, end_naive = infer_setup
    utc = pytz.utc

    start = utc.localize(start_naive)
    end = utc.localize(end_naive)

    assert timezones.infer_tzinfo(start, end) is utc 
Example #15
Source File: test_timezones.py    From coffeegrindsize with MIT License 5 votes vote down vote up
def test_infer_tz_mismatch(infer_setup, ordered):
    eastern, _, _, _, start_naive, end_naive = infer_setup
    msg = "Inputs must both have the same timezone"

    utc = pytz.utc
    start = utc.localize(start_naive)
    end = conversion.localize_pydatetime(end_naive, eastern)

    args = (start, end) if ordered else (end, start)

    with pytest.raises(AssertionError, match=msg):
        timezones.infer_tzinfo(*args)