本文整理汇总了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'
示例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)
示例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)
示例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)
示例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)
示例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)
示例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'
示例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)
示例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
示例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])
示例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)
示例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)
示例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())
示例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
示例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