--- CBOR-XS/XS.pm 2013/12/01 14:30:52 1.33 +++ CBOR-XS/XS.pm 2013/12/02 06:37:53 1.36 @@ -66,7 +66,7 @@ use common::sense; -our $VERSION = 1.1; +our $VERSION = 1.11; our @ISA = qw(Exporter); our @EXPORT = qw(encode_cbor decode_cbor); @@ -801,6 +801,15 @@ =over 4 +=item 0, 1 (date/time string, seconds since the epoch) + +These tags are decoded into L objects. The corresponding +C method always encodes into tag 1 values currently. + +The L API is generally surprisingly bad, and fractional +seconds are only accidentally kept intact, so watch out. On the plus side, +the module comes with perl since 5.10, which has to count for something. + =item 2, 3 (positive/negative bignum) These tags are decoded into L objects. The corresponding @@ -974,8 +983,33 @@ =cut our %FILTER = ( - # 0 # rfc4287 datetime, utf-8 - # 1 # unix timestamp, any + 0 => sub { # rfc4287 datetime, utf-8 + require Time::Piece; + # Time::Piece::Strptime uses the "incredibly flexible date parsing routine" + # from FreeBSD, which can't parse ISO 8601, RFC3339, RFC4287 or much of anything + # else either. Whats incredibe over standard strptime totally escapes me. + # doesn't do fractional times, either. sigh. + # In fact, it's all a lie, it uses whatever strptime it wants, and of course, + # they are all incomptible. The openbsd one simply ignores %z (but according to the + # docs, it would be much more incredibly flexible). + scalar eval { + my $s = $_[1]; + + $s =~ s/Z$/+00:00/; + $s =~ s/(\.[0-9]+)?([+-][0-9][0-9]):([0-9][0-9])$// + or die; + + my $b = $1 - ($2 * 60 + $3) * 60; # fractional part + offset. hopefully + my $d = Time::Piece->strptime ($s, "%Y-%m-%dT%H:%M:%S"); + + Time::Piece::gmtime ($d->epoch + $b) + } || die "corrupted CBOR date/time string ($_[0])"; + }, + + 1 => sub { # seconds since the epoch, possibly fractional + require Time::Piece; + scalar Time::Piece::gmtime (pop) + }, 2 => sub { # pos bigint require Math::BigInt; @@ -1021,7 +1055,7 @@ sub URI::TO_CBOR { my $uri = $_[0]->as_string; utf8::upgrade $uri; - CBOR::XS::tag 32, $uri + tag 32, $uri } sub Math::BigInt::TO_CBOR { @@ -1030,13 +1064,17 @@ } else { my $hex = substr $_[0]->as_hex, 2; $hex = "0$hex" if 1 & length $hex; # sigh - CBOR::XS::tag $_[0] >= 0 ? 2 : 3, pack "H*", $hex + tag $_[0] >= 0 ? 2 : 3, pack "H*", $hex } } sub Math::BigFloat::TO_CBOR { my ($m, $e) = $_[0]->parts; - CBOR::XS::tag 4, [$e->numify, $m] + tag 4, [$e->numify, $m] +} + +sub Time::Piece::TO_CBOR { + tag 1, $_[0]->epoch } XSLoader::load "CBOR::XS", $VERSION;