1#!/usr/bin/perl 2use strict; 3use warnings; 4 5use Time::Moment qw[]; 6use Time::HiRes qw[]; 7use CBOR::XS qw[encode_cbor]; 8 9sub filter { 10 my ($tag) = @_; 11 # http://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml 12 if ($tag == 0) { return Time::Moment->from_string($_[1]) } 13 elsif ($tag == 1) { return Time::Moment->from_epoch($_[1]) } 14 else { return &CBOR::XS::default_filter } 15} 16 17my $encoded = encode_cbor([ 18 # Tag 0 is standard date/time string; see Section 2.4.1 19 CBOR::XS::tag(0, '2013-12-24T12:30:45.123456789+01:00'), 20 # Tag 1 is epoch-based date/time; see Section 2.4.1 21 CBOR::XS::tag(1, time), 22 CBOR::XS::tag(1, Time::HiRes::time), 23 # Serializes as tag 0 24 Time::Moment->now, 25]); 26 27my $decoded = CBOR::XS->new->filter(\&filter)->decode($encoded); 28foreach my $moment (@$decoded) { 29 print $moment->to_string, "\n"; 30} 31