AnyEvent-XMPP
view release on metacpan or search on metacpan
lib/AnyEvent/XMPP/Util.pm view on Meta::CPAN
The meanings of C<$sec>, ..., C<$year> are explained in the perldoc
of Perl's C<localtime> builtin and have the same value ranges.
C<$tz> has to be either C<"Z"> (for UTC) or of the form C<[+-]hh:mm> (offset
from UTC), if it is undefined "Z" will be used.
C<$secfrac> are optional and can be the fractions of the second.
See also XEP-0082.
=cut
sub to_xmpp_datetime {
my ($sec, $min, $hour, $mday, $mon, $year, $tz, $secfrac) = @_;
my $time = to_xmpp_time ($sec, $min, $hour, (defined $tz ? $tz : 'Z'), $secfrac);
sprintf "%04d-%02d-%02dT%s", $year + 1900, $mon + 1, $mday, $time;
}
=item B<from_xmpp_datetime ($string)>
This function transforms the C<$string> which is either a time or datetime in XMPP
format. If the string was not in the right format an empty list is returned.
Otherwise this is returned:
my ($sec, $min, $hour, $mday, $mon, $year, $tz, $secfrac)
= from_xmpp_datetime ($string);
For the value ranges and semantics of C<$sec>, ..., C<$srcfrac> please look at the
documentation for C<to_xmpp_datetime>.
C<$tz> and C<$secfrac> might be undefined.
If C<$tz> is undefined the timezone is to be assumed to be UTC.
If C<$string> contained just a time C<$mday>, C<$mon> and C<$year> will be undefined.
See also XEP-0082.
=cut
sub from_xmpp_datetime {
my ($string) = @_;
if ($string !~
/^(?:(\d{4})-?(\d{2})-?(\d{2})T)?(\d{2}):(\d{2}):(\d{2})(\.\d{3})?(Z|[+-]\d{2}:\d{2})?/)
{
return ()
}
($6, $5, $4,
($3 ne '' ? $3 : undef),
($2 ne '' ? $2 - 1 : undef),
($1 ne '' ? $1 - 1900 : undef),
($8 ne '' ? $8 : undef),
($7 ne '' ? $7 : undef))
}
=item B<xmpp_datetime_as_timestamp ($string)>
This function takes the same arguments as C<from_xmpp_datetime>, but returns a
unix timestamp, like C<time ()> would.
This function requires the L<POSIX> module.
=cut
sub xmpp_datetime_as_timestamp {
my ($string) = @_;
my ($s, $m, $h, $md, $mon, $year, $tz) = from_xmpp_datetime ($string);
return 0 unless defined $h;
my $ts = timegm ($s, $m, $h, $md, $mon, $year);
if ($tz =~ /^([+-])(\d{2}):(\d{2})$/) {
$ts += ($1 eq '-' ? -1 : 1) * ($2 * 3600 + $3 * 60)
}
$ts
}
sub dump_twig_xml {
my $data = shift;
require XML::Twig;
my $t = XML::Twig->new;
if ($t->safe_parse ("<deb>$data</deb>")) {
$t->set_pretty_print ('indented');
return ($t->sprint . "\n");
} else {
return "$data\n";
}
}
sub install_default_debug_dump {
my ($con) = @_;
$con->reg_cb (
debug_recv => sub {
my ($con, $data) = @_;
printf "recv>> %s:%d\n%s", $con->{host}, $con->{port}, dump_twig_xml ($data)
},
debug_send => sub {
my ($con, $data) = @_;
printf "send<< %s:%d\n%s", $con->{host}, $con->{port}, dump_twig_xml ($data)
},
)
}
=back
=head1 AUTHOR
Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >>
=head1 COPYRIGHT & LICENSE
Copyright 2007, 2008 Robin Redeker, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
( run in 0.755 second using v1.01-cache-2.11-cpan-39bf76dae61 )