当前位置: 首页>>代码示例>>Python>>正文


Python DateTimeType.to_native方法代码示例

本文整理汇总了Python中schematics.types.DateTimeType.to_native方法的典型用法代码示例。如果您正苦于以下问题:Python DateTimeType.to_native方法的具体用法?Python DateTimeType.to_native怎么用?Python DateTimeType.to_native使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在schematics.types.DateTimeType的用法示例。


在下文中一共展示了DateTimeType.to_native方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_parse_require_tz

# 需要导入模块: from schematics.types import DateTimeType [as 别名]
# 或者: from schematics.types.DateTimeType import to_native [as 别名]
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,代码行数:11,代码来源:test_datetime.py

示例2: test_parse_convert_drop_tzinfo

# 需要导入模块: from schematics.types import DateTimeType [as 别名]
# 或者: from schematics.types.DateTimeType import to_native [as 别名]
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,代码行数:11,代码来源:test_datetime.py

示例3: test_parse_from_timestamp

# 需要导入模块: from schematics.types import DateTimeType [as 别名]
# 或者: from schematics.types.DateTimeType import to_native [as 别名]
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,代码行数:13,代码来源:test_datetime.py

示例4: test_parse_reject_tzd

# 需要导入模块: from schematics.types import DateTimeType [as 别名]
# 或者: from schematics.types.DateTimeType import to_native [as 别名]
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,代码行数:14,代码来源:test_datetime.py

示例5: test_parse_convert

# 需要导入模块: from schematics.types import DateTimeType [as 别名]
# 或者: from schematics.types.DateTimeType import to_native [as 别名]
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,代码行数:14,代码来源:test_datetime.py

示例6: test_parse_using_formats

# 需要导入模块: from schematics.types import DateTimeType [as 别名]
# 或者: from schematics.types.DateTimeType import to_native [as 别名]
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,代码行数:15,代码来源:test_datetime.py

示例7: test_parse_with_defaults

# 需要导入模块: from schematics.types import DateTimeType [as 别名]
# 或者: from schematics.types.DateTimeType import to_native [as 别名]
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,代码行数:25,代码来源:test_datetime.py

示例8: coercion

# 需要导入模块: from schematics.types import DateTimeType [as 别名]
# 或者: from schematics.types.DateTimeType import to_native [as 别名]
def coercion():
    dt_t = DateTimeType()
    dt = dt_t.to_native('2013-08-31T02:21:21.486072')
    print(dt, '\n', dt_t.to_primitive(dt))
开发者ID:Lipvun,项目名称:schematics,代码行数:6,代码来源:Simple_test.py

示例9: test_parse_utc

# 需要导入模块: from schematics.types import DateTimeType [as 别名]
# 或者: from schematics.types.DateTimeType import to_native [as 别名]
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,代码行数:8,代码来源:test_datetime.py

示例10: test_to_native_from_datetime

# 需要导入模块: from schematics.types import DateTimeType [as 别名]
# 或者: from schematics.types.DateTimeType import to_native [as 别名]
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,代码行数:42,代码来源:test_datetime.py

示例11: test_parse_reject_invalid_type

# 需要导入模块: from schematics.types import DateTimeType [as 别名]
# 或者: from schematics.types.DateTimeType import to_native [as 别名]
def test_parse_reject_invalid_type():

    field = DateTimeType()

    with pytest.raises(ConversionError):
        field.to_native(['2017-04-30'])
开发者ID:schematics,项目名称:schematics,代码行数:8,代码来源:test_datetime.py


注:本文中的schematics.types.DateTimeType.to_native方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。