當前位置: 首頁>>代碼示例>>Python>>正文


Python types.DateTimeType類代碼示例

本文整理匯總了Python中schematics.types.DateTimeType的典型用法代碼示例。如果您正苦於以下問題:Python DateTimeType類的具體用法?Python DateTimeType怎麽用?Python DateTimeType使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了DateTimeType類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_datetime_primitive

def test_datetime_primitive():
    output = '2013.03.07 15:31'
    dt = datetime.datetime(2013, 3, 7, 15, 31)
    dt_type = DateTimeType(serialized_format='%Y.%m.%d %H:%M')
    assert dt_type(dt.isoformat()) == dt
    assert dt_type.to_primitive(dt) == output

    obj = DateTimeType(serialized_format=str)
    assert obj.to_primitive(123) == '123'
開發者ID:Afey,項目名稱:schematics,代碼行數:9,代碼來源:test_types.py

示例2: test_parse_convert_drop_tzinfo

def test_parse_convert_drop_tzinfo():

    field = DateTimeType(tzd='require', convert_tz=True, drop_tzinfo=True)

    dt = field.to_native('2015-11-08T12:34:56.0369-0730')
    assert dt == datetime(2015, 11, 8, 20, 4, 56, 36900)

    dt = field.to_native('2015-11-08 12:34:56.00Z')
    assert dt == datetime(2015, 11, 8, 12, 34, 56, 0)
開發者ID:pombredanne,項目名稱:schematics,代碼行數:9,代碼來源:test_datetime.py

示例3: test_parse_require_tz

def test_parse_require_tz():

    field = DateTimeType(tzd='require')

    with pytest.raises(ConversionError):
        dt = field.to_native('2015-11-08 12:34:56.00')

    dt = field.to_native('2015-11-08 12:34:56.00Z')
    assert dt == datetime(2015, 11, 8, 12, 34, 56, tzinfo=UTC)
開發者ID:pombredanne,項目名稱:schematics,代碼行數:9,代碼來源:test_datetime.py

示例4: test_parse_from_timestamp

def test_parse_from_timestamp():

    field = DateTimeType()

    dt = field.to_native('1446991200.7777')
    assert dt == datetime(2015, 11, 8, 14, 00, microsecond=777700, tzinfo=UTC)

    field = DateTimeType(convert_tz=True, drop_tzinfo=True)

    dt = field.to_native('1446991200.7777')
    assert dt == datetime(2015, 11, 8, 14, 00, microsecond=777700)
開發者ID:pombredanne,項目名稱:schematics,代碼行數:11,代碼來源:test_datetime.py

示例5: test_parse_convert

def test_parse_convert():

    field = DateTimeType(convert_tz=True)

    dt = field.to_native('2015-11-08T12:34')
    assert dt == datetime(2015, 11, 8, 12, 34)

    dt = field.to_native('2015-11-08T12:34:56.0369-0730')
    assert dt == datetime(2015, 11, 8, 20, 4, 56, 36900, tzinfo=UTC)

    dt = field.to_native('2015-11-08 12:34:56,00Z')
    assert dt == datetime(2015, 11, 8, 12, 34, 56, 0, tzinfo=UTC)
開發者ID:pombredanne,項目名稱:schematics,代碼行數:12,代碼來源:test_datetime.py

示例6: test_parse_using_formats

def test_parse_using_formats():

    formats = ('%Y-%m-%dT%H:%M:%S.%fZ', '%Y-%m-%dT%H:%M:%S.%f%z')
    field = DateTimeType(formats=formats)

    dt = field.to_native('2015-11-08T12:34:56.99Z')
    assert dt == datetime(2015, 11, 8, 12, 34, 56, 990000)

    if sys.version_info[0] >= 3:
        # strptime() supports the '%z' directive starting with Python 3.2.
        dt = field.to_native('2015-11-08T12:34:56.099-0700')
        assert dt.utcoffset() == timedelta(hours=-7)
        assert dt.replace(tzinfo=None) == datetime(2015, 11, 8, 12, 34, 56, 99000)
開發者ID:pombredanne,項目名稱:schematics,代碼行數:13,代碼來源:test_datetime.py

示例7: test_to_primitive

def test_to_primitive():

    dt = datetime(2015, 11, 8, 12, 34, 56, 36900, tzinfo=DateTimeType.offset_timezone(-7, -30))

    assert DateTimeType().to_primitive(dt) == '2015-11-08T12:34:56.036900-0730'
    assert DateTimeType(serialized_format='%Y-%m-%d %H:%M:%S').to_primitive(dt) \
        == '2015-11-08 12:34:56'
開發者ID:pombredanne,項目名稱:schematics,代碼行數:7,代碼來源:test_datetime.py

示例8: test_parse_reject_tzd

def test_parse_reject_tzd():

    field = DateTimeType(tzd='reject')

    with pytest.raises(ConversionError):
        field.to_native('2015-11-08T12:34+0200')

    with pytest.raises(ConversionError):
        field.to_native('2015-11-08T12:34Z')

    dt = field.to_native('2015-11-08T12:34')
    assert dt == datetime(2015, 11, 8, 12, 34)
開發者ID:pombredanne,項目名稱:schematics,代碼行數:12,代碼來源:test_datetime.py

示例9: test_timestamp

def test_timestamp():

    field = TimestampType()
    field_no_tz = TimestampType(drop_tzinfo=True)

    ts = field.to_primitive(datetime(2015, 11, 8, 14, 00))
    assert ts == 1446991200.0
    assert type(ts) == float

    ts = field_no_tz.to_primitive(datetime(2015, 11, 8, 14, 00))
    assert ts == 1446991200.0
    assert type(ts) == float

    ts = field.to_primitive(datetime(2014, 5, 8, 22, 40, 40, tzinfo=gettz('PST8PDT')))
    assert ts == 1399614040.0

    ts = field_no_tz.to_primitive(datetime(2014, 5, 8, 22, 40, 40, tzinfo=gettz('PST8PDT')))
    assert ts == 1399614040.0

    dt_with_tz = datetime(2015, 11, 8, 16, 00, tzinfo=DateTimeType.offset_timezone(2))
    dt_no_tz = datetime(2015, 11, 8, 14, 00)
    dt_no_tz_usec = datetime(2015, 11, 8, 14, 00, microsecond=777700)

    assert field.to_primitive(dt_with_tz) == 1446991200.0
    assert field_no_tz.to_primitive(dt_with_tz) == 1446991200.0

    assert field.to_native(dt_with_tz) == dt_no_tz.replace(tzinfo=UTC)
    assert field_no_tz.to_native(dt_with_tz) == dt_no_tz

    assert field.to_native(1446991200.7777) == dt_no_tz_usec.replace(tzinfo=UTC)
    assert field_no_tz.to_native(1446991200.7777) == dt_no_tz_usec

    with pytest.raises(ConversionError):
        field.to_native(dt_no_tz)

    ts = field.to_primitive(dt_no_tz_usec)
    assert ts == 1446991200.7777
    assert type(ts) == float

    ts = field.to_primitive(datetime(2014, 5, 8, 22, 40, 40, tzinfo=UTC))
    assert ts == 1399588840.0

    assert field.to_primitive(EPOCH) == 0
    assert field.to_native(0) == EPOCH
開發者ID:pombredanne,項目名稱:schematics,代碼行數:44,代碼來源:test_datetime.py

示例10: test_validate_tz

def test_validate_tz():

    dt_naive = lambda: datetime(2015, 6, 1, 14, 00)
    dt_utc = lambda: datetime(2015, 6, 1, 14, 00, tzinfo=UTC)
    dt_plustwo = lambda: datetime(2015, 6, 1, 16, 00, tzinfo=DateTimeType.offset_timezone(2))
    dt_nyc = lambda: datetime(2015, 6, 1, 10, 00, tzinfo=NYC)

    all_values = [dt_naive, dt_utc, dt_plustwo, dt_nyc]

    def test_field(field, valid):
        for value in all_values:
            if value in valid:
                field.validate_tz(value())
            else:
                with pytest.raises(ValidationError):
                    field.validate_tz(value())

    test_field(DateTimeType(tzd='allow'), [dt_naive, dt_utc, dt_plustwo, dt_nyc])
    test_field(DateTimeType(convert_tz=True), [dt_naive, dt_utc])
    test_field(DateTimeType(tzd='utc'), [dt_utc, dt_plustwo, dt_nyc])
    test_field(DateTimeType(tzd='utc', convert_tz=True), [dt_utc])
    test_field(DateTimeType(tzd='reject'), [dt_naive])
    test_field(DateTimeType(tzd='require'), [dt_utc, dt_plustwo, dt_nyc])
    test_field(DateTimeType(drop_tzinfo=True), [dt_naive])
開發者ID:pombredanne,項目名稱:schematics,代碼行數:24,代碼來源:test_datetime.py

示例11: test_parse_with_defaults

def test_parse_with_defaults():

    field = DateTimeType()

    dt = field.to_native('2015-11-08T12:34')
    assert dt == datetime(2015, 11, 8, 12, 34)

    dt = field.to_native('2015-11-08T12:34:00.1')
    assert dt == datetime(2015, 11, 8, 12, 34, 0, 100000)

    dt = field.to_native('2015-11-08T12:34:56,0369-0730')
    assert dt.utcoffset() == timedelta(hours=-7, minutes=-30)
    assert dt.replace(tzinfo=None) == datetime(2015, 11, 8, 12, 34, 56, 36900)

    assert dt == field.to_native(u'2015-11-08T12:34:56,0369−0730') # minus U+2212

    dt = field.to_native('2015-11-08 12:34:56.00200+02:00')
    assert dt.utcoffset() == timedelta(hours=2)
    assert dt.replace(tzinfo=None) == datetime(2015, 11, 8, 12, 34, 56, 2000)

    dt = field.to_native('2015-11-08 12:34:56.00Z')
    assert dt.utcoffset() == timedelta(0)
    assert dt.replace(tzinfo=None) == datetime(2015, 11, 8, 12, 34, 56, 0)
開發者ID:pombredanne,項目名稱:schematics,代碼行數:23,代碼來源:test_datetime.py

示例12: test_parse_utc

def test_parse_utc():

    field = DateTimeType(tzd='utc')

    dt = field.to_native('2015-11-08 12:34:56.00')
    assert dt == datetime(2015, 11, 8, 12, 34, 56, tzinfo=UTC)
開發者ID:pombredanne,項目名稱:schematics,代碼行數:6,代碼來源:test_datetime.py

示例13: test_mock

def test_mock(tzd, convert_tz, drop_tzinfo):

    field = DateTimeType(tzd=tzd, convert_tz=convert_tz, drop_tzinfo=drop_tzinfo)
    for _ in range(20):
        field.validate(field._mock())
開發者ID:pombredanne,項目名稱:schematics,代碼行數:5,代碼來源:test_datetime.py

示例14: test_to_native_from_datetime

def test_to_native_from_datetime():

    dt_naive_utc = datetime(2015, 6, 1, 14, 00)
    dt_utc = datetime(2015, 6, 1, 14, 00, tzinfo=UTC)
    dt_plustwo = datetime(2015, 6, 1, 16, 00, tzinfo=DateTimeType.offset_timezone(2))
    dt_nyc = datetime(2015, 6, 1, 10, 00, tzinfo=NYC)

    field = DateTimeType(tzd='allow')
    assert field.to_native(dt_naive_utc) == dt_naive_utc
    assert field.to_native(dt_utc) == dt_utc
    assert field.to_native(dt_plustwo) == dt_plustwo
    assert field.to_native(dt_nyc) == dt_nyc

    field = DateTimeType(tzd='utc')
    assert field.to_native(dt_naive_utc) == dt_utc

    field = DateTimeType(tzd='reject')
    assert field.to_native(dt_naive_utc) == dt_naive_utc
    with pytest.raises(ConversionError):
        field.to_native(dt_utc)

    field = DateTimeType(tzd='require')
    assert field.to_native(dt_utc) == dt_utc
    with pytest.raises(ConversionError):
        field.to_native(dt_naive_utc)

    field = DateTimeType(tzd='require', convert_tz=True, drop_tzinfo=True)
    assert field.to_native(dt_naive_utc) == dt_naive_utc

    field = DateTimeType(convert_tz=True)
    assert field.to_native(dt_naive_utc) == dt_naive_utc
    assert field.to_native(dt_utc) == dt_utc
    assert field.to_native(dt_plustwo) == dt_utc
    assert field.to_native(dt_nyc) == dt_utc

    field = DateTimeType(convert_tz=True, drop_tzinfo=True)
    assert field.to_native(dt_naive_utc) == dt_naive_utc
    assert field.to_native(dt_utc) == dt_naive_utc
    assert field.to_native(dt_plustwo) == dt_naive_utc
    assert field.to_native(dt_nyc) == dt_naive_utc
開發者ID:pombredanne,項目名稱:schematics,代碼行數:40,代碼來源:test_datetime.py

示例15: test_datetime_accepts_datetime

def test_datetime_accepts_datetime():
    output = '2013.03.07 15:31'
    dt = datetime.datetime(2013, 3, 7, 15, 31)
    dt_type = DateTimeType(serialized_format='%Y.%m.%d %H:%M')
    assert dt_type(dt) == dt
    assert dt_type.to_primitive(dt) == output
開發者ID:Afey,項目名稱:schematics,代碼行數:6,代碼來源:test_types.py


注:本文中的schematics.types.DateTimeType類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。