Beispiel #1
0
def parse_datetime(value):
    """
    Parses a string and return a datetime.datetime.

    This function supports time zone offsets. When the input contains one,
    the output uses a timezone with a fixed offset from UTC.

    Raises ValueError if the input is well formatted but not a valid datetime.
    Returns None if the input isn't well formatted.
    """
    match = datetime_re.match(value)
    if match:
        kw = match.groupdict()
        if kw['microsecond']:
            kw['microsecond'] = kw['microsecond'].ljust(6, '0')
        tzinfo = kw.pop('tzinfo')
        if tzinfo == 'Z':
            tzinfo = utc
        elif tzinfo is not None:
            offset_mins = int(tzinfo[-2:]) if len(tzinfo) > 3 else 0
            offset = 60 * int(tzinfo[1:3]) + offset_mins
            if tzinfo[0] == '-':
                offset = -offset
            tzinfo = get_fixed_timezone(offset)
        kw = {k: int(v) for k, v in iter(kw.items()) if v is not None}
        kw['tzinfo'] = tzinfo
        return datetime.datetime(**kw)
Beispiel #2
0
    ("2000-1-1 00:00:00.000", dt.datetime(2000, 1, 1, 0, 0, 0, 0)),
    ("2000-1-1 1:30:55.150", dt.datetime(2000, 1, 1, 1, 30, 55, 150000)),
    ("2000-1-1 0:5:0", dt.datetime(2000, 1, 1, 0, 5, 0, 0)),
    ("2000-1-1 0:0:5", dt.datetime(2000, 1, 1, 0, 0, 5, 0)),
    ("2000-1-1 0:0:0.5", dt.datetime(2000, 1, 1, 0, 0, 0, 500000)),
    ("2000-01-01T00:00:00.000", dt.datetime(2000, 1, 1)),
])
def test_parse_datetime(value, expected):
    assert dateparse.parse_datetime(value) == expected


@pytest.mark.parametrize("value, expected", [
    ("2000-01-01 00:00:00.000Z", dt.datetime(2000, 1, 1, tzinfo=utc)),
    ("2000-01-01T00:00:00.000Z", dt.datetime(2000, 1, 1, tzinfo=utc)),
    ("2000-01-01 00:00:00.000+01",
     dt.datetime(2000, 1, 1, tzinfo=get_fixed_timezone(60))),
    ("2000-01-01T00:00:00.000+01",
     dt.datetime(2000, 1, 1, tzinfo=get_fixed_timezone(60))),
])
def test_parse_datetime_with_timezone(value, expected):
    result = dateparse.parse_datetime(value)
    if result.tzinfo == utc:
        assert result.tzinfo == expected.tzinfo
    else:
        result_offset = result.tzinfo._FixedOffset__offset
        expected_offset = expected.tzinfo._FixedOffset__offset
        assert result_offset == expected_offset


@pytest.mark.parametrize("value, expected", [
    ("1 day", dt.timedelta(1)),
Beispiel #3
0
def test_get_fixed_timezone_with_timedelta(offset, timedelta, name):
    fixed_timezone = get_fixed_timezone(offset)
    assert fixed_timezone.utcoffset(None) == timedelta
    assert fixed_timezone.tzname(None) == name
Beispiel #4
0
def test_get_fixed_timezone(offset, name):
    fixed_timezone = get_fixed_timezone(offset)
    assert fixed_timezone.utcoffset(None) == dt.timedelta(minutes=offset)
    assert fixed_timezone.tzname(None) == name
Beispiel #5
0
# -*- coding: utf-8 -*-
import pytest
import datetime as dt

from aiorest_ws.conf import settings
from aiorest_ws.utils.date.timezone import UTC, ZERO, FixedOffset, \
    get_fixed_timezone, get_default_timezone, get_current_timezone, \
    get_current_timezone_name, localtime, now, is_aware, is_naive, \
    make_aware, make_naive, LocalTimezone, utc
from aiorest_ws.test.utils import override_settings

EAT = get_fixed_timezone(180)  # Africa/Nairobi
ICT = get_fixed_timezone(420)  # Asia/Bangkok


def test_utc_class():
    utc = UTC()
    assert utc.__repr__() == "<UTC>"
    assert utc.utcoffset(dt.timedelta(0)) == ZERO
    assert utc.tzname(dt.timedelta(0)) == "UTC"
    assert utc.dst(dt.timedelta(0)) == ZERO


try:
    # If pytz module has installed
    import pytz  # NOQA

    def test_get_default_timezone():
        timezone = get_default_timezone()
        assert timezone.zone == settings.TIME_ZONE