def decode_datetime_string(decoder, value, shareable_index=None): # Semantic tag 0 match = timestamp_re.match(value) if match: year, month, day, hour, minute, second, micro, offset_h, offset_m = match.groups() if offset_h: tz = timezone(timedelta(hours=int(offset_h), minutes=int(offset_m))) else: tz = timezone.utc return datetime(int(year), int(month), int(day), int(hour), int(minute), int(second), int(micro or 0), tz) else: raise CBORDecodeError('invalid datetime string: {}'.format(value))
decoded = loads(unhexlify(payload)) assert decoded == expected # # Tests for extension tags # @pytest.mark.parametrize('payload, expected', [ ('c074323031332d30332d32315432303a30343a30305a', datetime(2013, 3, 21, 20, 4, 0, tzinfo=timezone.utc)), ('c0781b323031332d30332d32315432303a30343a30302e3338303834315a', datetime(2013, 3, 21, 20, 4, 0, 380841, tzinfo=timezone.utc)), ('c07819323031332d30332d32315432323a30343a30302b30323a3030', datetime(2013, 3, 21, 22, 4, 0, tzinfo=timezone(timedelta(hours=2)))), ('c11a514b67b0', datetime(2013, 3, 21, 20, 4, 0, tzinfo=timezone.utc)), ('c11a514b67b0', datetime(2013, 3, 21, 22, 4, 0, tzinfo=timezone(timedelta(hours=2)))) ], ids=['datetime/utc', 'datetime+micro/utc', 'datetime/eet', 'timestamp/utc', 'timestamp/eet']) def test_datetime(payload, expected): decoded = loads(unhexlify(payload)) assert decoded == expected def test_bad_datetime(): exc = pytest.raises(CBORDecodeError, loads, unhexlify('c06b303030302d3132332d3031')) assert str(exc.value).endswith('invalid datetime string: 0000-123-01') def test_fraction(): decoded = loads(unhexlify('c48221196ab3'))
assert decoded == expected # # Tests for extension tags # @pytest.mark.parametrize( 'payload, expected', [('c074323031332d30332d32315432303a30343a30305a', datetime(2013, 3, 21, 20, 4, 0, tzinfo=timezone.utc)), ('c0781b323031332d30332d32315432303a30343a30302e3338303834315a', datetime(2013, 3, 21, 20, 4, 0, 380841, tzinfo=timezone.utc)), ('c07819323031332d30332d32315432323a30343a30302b30323a3030', datetime(2013, 3, 21, 22, 4, 0, tzinfo=timezone(timedelta(hours=2)))), ('c11a514b67b0', datetime(2013, 3, 21, 20, 4, 0, tzinfo=timezone.utc)), ('c11a514b67b0', datetime(2013, 3, 21, 22, 4, 0, tzinfo=timezone(timedelta(hours=2))))], ids=[ 'datetime/utc', 'datetime+micro/utc', 'datetime/eet', 'timestamp/utc', 'timestamp/eet' ]) def test_datetime(payload, expected): decoded = loads(unhexlify(payload)) assert decoded == expected def test_bad_datetime(): exc = pytest.raises(CBORDecodeError, loads, unhexlify('c06b303030302d3132332d3031'))
]) def test_simple_value(value, expected): expected = unhexlify(expected) assert dumps(value) == expected # # Tests for extension tags # @pytest.mark.parametrize('value, as_timestamp, expected', [ (datetime(2013, 3, 21, 20, 4, 0, tzinfo=timezone.utc), False, 'c074323031332d30332d32315432303a30343a30305a'), (datetime(2013, 3, 21, 20, 4, 0, 380841, tzinfo=timezone.utc), False, 'c0781b323031332d30332d32315432303a30343a30302e3338303834315a'), (datetime(2013, 3, 21, 22, 4, 0, tzinfo=timezone(timedelta(hours=2))), False, 'c07819323031332d30332d32315432323a30343a30302b30323a3030'), (datetime(2013, 3, 21, 20, 4, 0), False, 'c074323031332d30332d32315432303a30343a30305a'), (datetime(2013, 3, 21, 20, 4, 0, tzinfo=timezone.utc), True, 'c11a514b67b0'), (datetime(2013, 3, 21, 22, 4, 0, tzinfo=timezone(timedelta(hours=2))), True, 'c11a514b67b0') ], ids=['datetime/utc', 'datetime+micro/utc', 'datetime/eet', 'naive', 'timestamp/utc', 'timestamp/eet']) def test_datetime(value, as_timestamp, expected): expected = unhexlify(expected) assert dumps(value, datetime_as_timestamp=as_timestamp, timezone=timezone.utc) == expected def test_date(): expected = unhexlify('c074323031332d30332d32315430303a30303a30305a') assert dumps(date(2013, 3, 21), timezone=timezone.utc) == expected