• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python types.DateTimeType类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



在下文中一共展示了DateTimeType类的18个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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


示例16: test_datetime_accepts_datetime

 def test_datetime_accepts_datetime(self):
     output = '2013.03.07 15:31'
     dt = datetime.datetime(2013, 3, 7, 15, 31)
     dt_type = DateTimeType(serialized_format='%Y.%m.%d %H:%M')
     self.assertEqual(dt_type(dt), dt)
     self.assertEqual(dt_type.to_primitive(dt), output)
开发者ID:Spencerx,项目名称:schematics,代码行数:6,代码来源:test_types.py


示例17: coercion

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,代码行数:4,代码来源:Simple_test.py


示例18: test_parse_reject_invalid_type

def test_parse_reject_invalid_type():

    field = DateTimeType()

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



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python scheme.scheme_eval函数代码示例发布时间:2022-05-27
下一篇:
Python schema_utils.normalize_against_schema函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap