← Index
NYTProf Performance Profile   « block view • line view • sub view »
For mentat.storage.mongo.pl
  Run on Tue Jun 24 09:58:41 2014
Reported on Tue Jun 24 09:59:27 2014

Filename/usr/local/lib/site_perl/Value/Convertor.pm
StatementsExecuted 157 statements in 17.6ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11128.8ms205msValue::Convertor::::BEGIN@61Value::Convertor::BEGIN@61
11110.7ms12.0msValue::Convertor::::BEGIN@55Value::Convertor::BEGIN@55
1116.84ms7.40msValue::Convertor::::BEGIN@52Value::Convertor::BEGIN@52
1116.31ms6.59msValue::Convertor::::BEGIN@56Value::Convertor::BEGIN@56
1113.96ms61.2msValue::Convertor::::BEGIN@364Value::Convertor::BEGIN@364
1111.93ms13.4msValue::Convertor::::BEGIN@60Value::Convertor::BEGIN@60
1111.32ms1.91msValue::Convertor::::BEGIN@53Value::Convertor::BEGIN@53
111158µs122msValue::Convertor::::BEGIN@153Value::Convertor::BEGIN@153
11149µs115µsValue::Convertor::::BEGIN@103Value::Convertor::BEGIN@103
193139µs39µsValue::Convertor::::CORE:qrValue::Convertor::CORE:qr (opcode)
11127µs34µsValue::Convertor::::BEGIN@2Value::Convertor::BEGIN@2
11124µs38µsValue::Convertor::::ipv4str_to_intValue::Convertor::ipv4str_to_int
11122µs34µsValue::Convertor::::BEGIN@3Value::Convertor::BEGIN@3
11120µs770µsValue::Convertor::::BEGIN@389Value::Convertor::BEGIN@389
11119µs794µsValue::Convertor::::BEGIN@1230Value::Convertor::BEGIN@1230
11117µs78µsValue::Convertor::::BEGIN@137Value::Convertor::BEGIN@137
11115µs74µsValue::Convertor::::BEGIN@88Value::Convertor::BEGIN@88
11115µs222µsValue::Convertor::::BEGIN@154Value::Convertor::BEGIN@154
11114µs658µsValue::Convertor::::BEGIN@1721Value::Convertor::BEGIN@1721
11114µs85µsValue::Convertor::::BEGIN@73Value::Convertor::BEGIN@73
11114µs73µsValue::Convertor::::BEGIN@84Value::Convertor::BEGIN@84
11114µs74µsValue::Convertor::::BEGIN@74Value::Convertor::BEGIN@74
11114µs71µsValue::Convertor::::BEGIN@78Value::Convertor::BEGIN@78
11113µs13µsValue::Convertor::::BEGIN@62Value::Convertor::BEGIN@62
21113µs16µsValue::Convertor::::bin_to_hexstrValue::Convertor::bin_to_hexstr
11113µs71µsValue::Convertor::::BEGIN@76Value::Convertor::BEGIN@76
11113µs74µsValue::Convertor::::ipv4cidr_to_binsValue::Convertor::ipv4cidr_to_bins
11113µs68µsValue::Convertor::::BEGIN@96Value::Convertor::BEGIN@96
11113µs70µsValue::Convertor::::BEGIN@82Value::Convertor::BEGIN@82
11113µs69µsValue::Convertor::::BEGIN@95Value::Convertor::BEGIN@95
11113µs71µsValue::Convertor::::BEGIN@87Value::Convertor::BEGIN@87
11113µs67µsValue::Convertor::::BEGIN@92Value::Convertor::BEGIN@92
11113µs68µsValue::Convertor::::BEGIN@100Value::Convertor::BEGIN@100
11113µs69µsValue::Convertor::::BEGIN@79Value::Convertor::BEGIN@79
11113µs68µsValue::Convertor::::BEGIN@86Value::Convertor::BEGIN@86
11113µs68µsValue::Convertor::::BEGIN@83Value::Convertor::BEGIN@83
11113µs68µsValue::Convertor::::BEGIN@80Value::Convertor::BEGIN@80
11113µs68µsValue::Convertor::::BEGIN@91Value::Convertor::BEGIN@91
11113µs68µsValue::Convertor::::BEGIN@101Value::Convertor::BEGIN@101
11113µs68µsValue::Convertor::::BEGIN@90Value::Convertor::BEGIN@90
11113µs70µsValue::Convertor::::BEGIN@98Value::Convertor::BEGIN@98
11112µs67µsValue::Convertor::::BEGIN@94Value::Convertor::BEGIN@94
11112µs67µsValue::Convertor::::BEGIN@99Value::Convertor::BEGIN@99
11111µs49µsValue::Convertor::::ipv4cidr_to_intsValue::Convertor::ipv4cidr_to_ints
32110µs10µsValue::Convertor::::CORE:packValue::Convertor::CORE:pack (opcode)
2119µs12µsValue::Convertor::::ipv4int_to_binValue::Convertor::ipv4int_to_bin
1118µs312µsValue::Convertor::::BEGIN@2047Value::Convertor::BEGIN@2047
1117µs317µsValue::Convertor::::BEGIN@1746Value::Convertor::BEGIN@1746
1117µs360µsValue::Convertor::::BEGIN@1801Value::Convertor::BEGIN@1801
1117µs321µsValue::Convertor::::BEGIN@1774Value::Convertor::BEGIN@1774
1117µs324µsValue::Convertor::::BEGIN@2068Value::Convertor::BEGIN@2068
3215µs5µsValue::Convertor::::CORE:unpackValue::Convertor::CORE:unpack (opcode)
1114µs4µsValue::Convertor::::CORE:matchValue::Convertor::CORE:match (opcode)
1112µs2µsValue::Convertor::::ENDValue::Convertor::END
1111µs1µsValue::Convertor::::CORE:regcompValue::Convertor::CORE:regcomp (opcode)
0000s0sValue::Convertor::::_initValue::Convertor::_init
0000s0sValue::Convertor::::_zero_padValue::Convertor::_zero_pad
0000s0sValue::Convertor::::_zero_pad_nValue::Convertor::_zero_pad_n
0000s0sValue::Convertor::::anything_to_intValue::Convertor::anything_to_int
0000s0sValue::Convertor::::anything_to_samethingValue::Convertor::anything_to_samething
0000s0sValue::Convertor::::anything_to_stringValue::Convertor::anything_to_string
0000s0sValue::Convertor::::array_to_listValue::Convertor::array_to_list
0000s0sValue::Convertor::::bigint_to_binValue::Convertor::bigint_to_bin
0000s0sValue::Convertor::::bigint_to_hexstrValue::Convertor::bigint_to_hexstr
0000s0sValue::Convertor::::bigint_to_ipv6strValue::Convertor::bigint_to_ipv6str
0000s0sValue::Convertor::::bigint_to_macstrValue::Convertor::bigint_to_macstr
0000s0sValue::Convertor::::bin_to_bigintValue::Convertor::bin_to_bigint
0000s0sValue::Convertor::::bin_to_binstrValue::Convertor::bin_to_binstr
0000s0sValue::Convertor::::bin_to_ipv4hexValue::Convertor::bin_to_ipv4hex
0000s0sValue::Convertor::::bin_to_ipv4intValue::Convertor::bin_to_ipv4int
0000s0sValue::Convertor::::bin_to_ipv4strValue::Convertor::bin_to_ipv4str
0000s0sValue::Convertor::::bin_to_ipv6strValue::Convertor::bin_to_ipv6str
0000s0sValue::Convertor::::bin_to_macstrValue::Convertor::bin_to_macstr
0000s0sValue::Convertor::::binstr_to_binValue::Convertor::binstr_to_bin
0000s0sValue::Convertor::::binstr_to_hexstrValue::Convertor::binstr_to_hexstr
0000s0sValue::Convertor::::datestr_to_nbiValue::Convertor::datestr_to_nbi
0000s0sValue::Convertor::::datestr_to_ubiValue::Convertor::datestr_to_ubi
0000s0sValue::Convertor::::datestr_to_unixsValue::Convertor::datestr_to_unixs
0000s0sValue::Convertor::::datetime_to_nbiValue::Convertor::datetime_to_nbi
0000s0sValue::Convertor::::detect_datetimeValue::Convertor::detect_datetime
0000s0sValue::Convertor::::detect_ipValue::Convertor::detect_ip
0000s0sValue::Convertor::::detect_ipv4Value::Convertor::detect_ipv4
0000s0sValue::Convertor::::detect_ipv6Value::Convertor::detect_ipv6
0000s0sValue::Convertor::::duration_to_stringValue::Convertor::duration_to_string
0000s0sValue::Convertor::::hexstr_to_bigintValue::Convertor::hexstr_to_bigint
0000s0sValue::Convertor::::hexstr_to_binValue::Convertor::hexstr_to_bin
0000s0sValue::Convertor::::hexstr_to_binstrValue::Convertor::hexstr_to_binstr
0000s0sValue::Convertor::::hexstr_to_ipv6strValue::Convertor::hexstr_to_ipv6str
0000s0sValue::Convertor::::hexstr_to_macstrValue::Convertor::hexstr_to_macstr
0000s0sValue::Convertor::::int_to_ipv4hexValue::Convertor::int_to_ipv4hex
0000s0sValue::Convertor::::int_to_ipv4strValue::Convertor::int_to_ipv4str
0000s0sValue::Convertor::::ints_to_ipv4cidrValue::Convertor::ints_to_ipv4cidr
0000s0sValue::Convertor::::ints_to_ipv4netmValue::Convertor::ints_to_ipv4netm
0000s0sValue::Convertor::::ints_to_ipv4rngstrValue::Convertor::ints_to_ipv4rngstr
0000s0sValue::Convertor::::ints_to_ntpsValue::Convertor::ints_to_ntps
0000s0sValue::Convertor::::ints_to_unixsValue::Convertor::ints_to_unixs
0000s0sValue::Convertor::::ip_to_binValue::Convertor::ip_to_bin
0000s0sValue::Convertor::::ip_to_intValue::Convertor::ip_to_int
0000s0sValue::Convertor::::ipv4_anonymizeValue::Convertor::ipv4_anonymize
0000s0sValue::Convertor::::ipv4_to_binValue::Convertor::ipv4_to_bin
0000s0sValue::Convertor::::ipv4_to_intValue::Convertor::ipv4_to_int
0000s0sValue::Convertor::::ipv4hex_to_binValue::Convertor::ipv4hex_to_bin
0000s0sValue::Convertor::::ipv4hex_to_intValue::Convertor::ipv4hex_to_int
0000s0sValue::Convertor::::ipv4netm_to_binsValue::Convertor::ipv4netm_to_bins
0000s0sValue::Convertor::::ipv4netm_to_intsValue::Convertor::ipv4netm_to_ints
0000s0sValue::Convertor::::ipv4rngstr_to_binsValue::Convertor::ipv4rngstr_to_bins
0000s0sValue::Convertor::::ipv4rngstr_to_intsValue::Convertor::ipv4rngstr_to_ints
0000s0sValue::Convertor::::ipv4str_to_binValue::Convertor::ipv4str_to_bin
0000s0sValue::Convertor::::ipv6_collapseValue::Convertor::ipv6_collapse
0000s0sValue::Convertor::::ipv6_expandValue::Convertor::ipv6_expand
0000s0sValue::Convertor::::ipv6_to_bigintValue::Convertor::ipv6_to_bigint
0000s0sValue::Convertor::::ipv6_to_binValue::Convertor::ipv6_to_bin
0000s0sValue::Convertor::::ipv6cidr_to_bigintsValue::Convertor::ipv6cidr_to_bigints
0000s0sValue::Convertor::::ipv6cidr_to_binsValue::Convertor::ipv6cidr_to_bins
0000s0sValue::Convertor::::ipv6str_to_bigintValue::Convertor::ipv6str_to_bigint
0000s0sValue::Convertor::::ipv6str_to_binValue::Convertor::ipv6str_to_bin
0000s0sValue::Convertor::::ipv6str_to_hexstrValue::Convertor::ipv6str_to_hexstr
0000s0sValue::Convertor::::list_to_arrayValue::Convertor::list_to_array
0000s0sValue::Convertor::::local_to_utcValue::Convertor::local_to_utc
0000s0sValue::Convertor::::macstr_to_bigintValue::Convertor::macstr_to_bigint
0000s0sValue::Convertor::::macstr_to_binValue::Convertor::macstr_to_bin
0000s0sValue::Convertor::::macstr_to_hexstrValue::Convertor::macstr_to_hexstr
0000s0sValue::Convertor::::nbi_to_datestrValue::Convertor::nbi_to_datestr
0000s0sValue::Convertor::::nbi_to_ntpsValue::Convertor::nbi_to_ntps
0000s0sValue::Convertor::::nbi_to_unixsValue::Convertor::nbi_to_unixs
0000s0sValue::Convertor::::nbis_to_periodstrValue::Convertor::nbis_to_periodstr
0000s0sValue::Convertor::::newValue::Convertor::new
0000s0sValue::Convertor::::ntpe_to_unixeValue::Convertor::ntpe_to_unixe
0000s0sValue::Convertor::::ntpe_to_unixe_bValue::Convertor::ntpe_to_unixe_b
0000s0sValue::Convertor::::ntps_to_datestrValue::Convertor::ntps_to_datestr
0000s0sValue::Convertor::::ntps_to_intsValue::Convertor::ntps_to_ints
0000s0sValue::Convertor::::ntps_to_ldatestrValue::Convertor::ntps_to_ldatestr
0000s0sValue::Convertor::::ntps_to_nbiValue::Convertor::ntps_to_nbi
0000s0sValue::Convertor::::periodstr_to_nbisValue::Convertor::periodstr_to_nbis
0000s0sValue::Convertor::::string_to_durationValue::Convertor::string_to_duration
0000s0sValue::Convertor::::ubi_to_datestrValue::Convertor::ubi_to_datestr
0000s0sValue::Convertor::::ubi_to_unixsValue::Convertor::ubi_to_unixs
0000s0sValue::Convertor::::unixe_to_ntpeValue::Convertor::unixe_to_ntpe
0000s0sValue::Convertor::::unixe_to_ntpe_bValue::Convertor::unixe_to_ntpe_b
0000s0sValue::Convertor::::unixs_to_datestrValue::Convertor::unixs_to_datestr
0000s0sValue::Convertor::::unixs_to_datetimeValue::Convertor::unixs_to_datetime
0000s0sValue::Convertor::::unixs_to_intsValue::Convertor::unixs_to_ints
0000s0sValue::Convertor::::unixs_to_ldatestrValue::Convertor::unixs_to_ldatestr
0000s0sValue::Convertor::::unixs_to_nbiValue::Convertor::unixs_to_nbi
0000s0sValue::Convertor::::unixs_to_ubiValue::Convertor::unixs_to_ubi
0000s0sValue::Convertor::::utc_to_localValue::Convertor::utc_to_local
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Value::Convertor;
2270µs242µs
# spent 34µs (27+7) within Value::Convertor::BEGIN@2 which was called: # once (27µs+7µs) by main::BEGIN@7 at line 2
use strict;
# spent 34µs making 1 call to Value::Convertor::BEGIN@2 # spent 7µs making 1 call to strict::import
32103µs246µs
# spent 34µs (22+12) within Value::Convertor::BEGIN@3 which was called: # once (22µs+12µs) by main::BEGIN@7 at line 3
use warnings;
# spent 34µs making 1 call to Value::Convertor::BEGIN@3 # spent 12µs making 1 call to warnings::import
4
5#*******************************************************************************
6# DOCUMENTATION SECTION
7#*******************************************************************************
8
9=head1 NAME
10
11Value::Convertor - Short module description
12
13=head1 SYNOPSIS
14
15 use Value::Convertor;
16
17=head1 DESCRIPTION
18
19This is the library class for various conversion functions. All conversions can
20be called either as class, or instance methods.
21
22=head1 AUTHOR
23
24Jan Mach
25Cesnet, z.s.p.o
26jan.mach@cesnet.cz
27http://www.cesnet.cz
28
29=head1 COPYRIGHT
30
31This program is free software; you can redistribute
32it and/or modify it under the same terms as Perl itself.
33
34The full text of the license can be found in the
35LICENSE file included with this module.
36
37=head1 SEE ALSO
38
39perl(1).
40
41=head1 FUNCTION REFERENCE
42
43=over 4
44
45=cut
46
47#*******************************************************************************
48# LIBRARY LOADING SECTION
49#*******************************************************************************
50
51#-- Perl core modules ---------------------------------------------------------#
522203µs27.50ms
# spent 7.40ms (6.84+555µs) within Value::Convertor::BEGIN@52 which was called: # once (6.84ms+555µs) by main::BEGIN@7 at line 52
use Carp;
# spent 7.40ms making 1 call to Value::Convertor::BEGIN@52 # spent 107µs making 1 call to Exporter::import
532215µs22.03ms
# spent 1.91ms (1.32+588µs) within Value::Convertor::BEGIN@53 which was called: # once (1.32ms+588µs) by main::BEGIN@7 at line 53
use Scalar::Util 'blessed';
# spent 1.91ms making 1 call to Value::Convertor::BEGIN@53 # spent 122µs making 1 call to Exporter::import
54
552272µs212.1ms
# spent 12.0ms (10.7+1.29) within Value::Convertor::BEGIN@55 which was called: # once (10.7ms+1.29ms) by main::BEGIN@7 at line 55
use Data::Dumper; #-+-> DEVEL ONLY <-+-#
# spent 12.0ms making 1 call to Value::Convertor::BEGIN@55 # spent 62µs making 1 call to Exporter::import
562243µs16.59ms
# spent 6.59ms (6.31+282µs) within Value::Convertor::BEGIN@56 which was called: # once (6.31ms+282µs) by main::BEGIN@7 at line 56
use Net::CIDR::Lite; # libnet-cidr-lite-perl
# spent 6.59ms making 1 call to Value::Convertor::BEGIN@56
57#use Smart::Comments; #-+-> DEVEL ONLY <-+-#
58
59#-- Perl CPAN modules ---------------------------------------------------------#
602236µs213.6ms
# spent 13.4ms (1.93+11.4) within Value::Convertor::BEGIN@60 which was called: # once (1.93ms+11.4ms) by main::BEGIN@7 at line 60
use Date::Parse;
# spent 13.4ms making 1 call to Value::Convertor::BEGIN@60 # spent 232µs making 1 call to Exporter::import
612324µs1205ms
# spent 205ms (28.8+177) within Value::Convertor::BEGIN@61 which was called: # once (28.8ms+177ms) by main::BEGIN@7 at line 61
use DateTime;
# spent 205ms making 1 call to Value::Convertor::BEGIN@61
62270µs113µs
# spent 13µs within Value::Convertor::BEGIN@62 which was called: # once (13µs+0s) by main::BEGIN@7 at line 62
use DateTime::TimeZone;
# spent 13µs making 1 call to Value::Convertor::BEGIN@62
63
64#-- Custom application modules ------------------------------------------------#
65
66#*******************************************************************************
67# CONSTANTS AND GLOBAL VARIABLES DEFINITION SECTION
68#*******************************************************************************
69
70#-- Constants -----------------------------------------------------------------#
71
72# Number of seconds between 1.1.1900 and 1.1.1970 (for NTP to unix timestamp conversions)
73270µs2156µs
# spent 85µs (14+71) within Value::Convertor::BEGIN@73 which was called: # once (14µs+71µs) by main::BEGIN@7 at line 73
use constant NTP_TO_EPOCH_DELTA => 2208988800;
# spent 85µs making 1 call to Value::Convertor::BEGIN@73 # spent 71µs making 1 call to constant::import
74259µs2135µs
# spent 74µs (14+61) within Value::Convertor::BEGIN@74 which was called: # once (14µs+61µs) by main::BEGIN@7 at line 74
use constant MAX_IP_MASK => hex('0xFFFFFFFF');
# spent 74µs making 1 call to Value::Convertor::BEGIN@74 # spent 61µs making 1 call to constant::import
75
76257µs2128µs
# spent 71µs (13+58) within Value::Convertor::BEGIN@76 which was called: # once (13µs+58µs) by main::BEGIN@7 at line 76
use constant UNKNOWN_TYPE => 'UNKONWN';
# spent 71µs making 1 call to Value::Convertor::BEGIN@76 # spent 58µs making 1 call to constant::import
77
78254µs2129µs
# spent 71µs (14+58) within Value::Convertor::BEGIN@78 which was called: # once (14µs+58µs) by main::BEGIN@7 at line 78
use constant IPV4ADDR_STRING => 'IPV4ADDR_STRING';
# spent 71µs making 1 call to Value::Convertor::BEGIN@78 # spent 58µs making 1 call to constant::import
79254µs2126µs
# spent 69µs (13+57) within Value::Convertor::BEGIN@79 which was called: # once (13µs+57µs) by main::BEGIN@7 at line 79
use constant IPV4ADDR_HEXSTR => 'IPV4ADDR_HEXSTR';
# spent 69µs making 1 call to Value::Convertor::BEGIN@79 # spent 57µs making 1 call to constant::import
80253µs2123µs
# spent 68µs (13+55) within Value::Convertor::BEGIN@80 which was called: # once (13µs+55µs) by main::BEGIN@7 at line 80
use constant IPV4ADDR_INTEGER => 'IPV4ADDR_INTEGER';
# spent 68µs making 1 call to Value::Convertor::BEGIN@80 # spent 55µs making 1 call to constant::import
81
82253µs2128µs
# spent 70µs (13+58) within Value::Convertor::BEGIN@82 which was called: # once (13µs+58µs) by main::BEGIN@7 at line 82
use constant IPV6ADDR_STRING => 'IPV6ADDR_STRING';
# spent 70µs making 1 call to Value::Convertor::BEGIN@82 # spent 58µs making 1 call to constant::import
83270µs2124µs
# spent 68µs (13+56) within Value::Convertor::BEGIN@83 which was called: # once (13µs+56µs) by main::BEGIN@7 at line 83
use constant IPV6ADDR_HEXSTR => 'IPV6ADDR_HEXSTR';
# spent 68µs making 1 call to Value::Convertor::BEGIN@83 # spent 56µs making 1 call to constant::import
84256µs2132µs
# spent 73µs (14+59) within Value::Convertor::BEGIN@84 which was called: # once (14µs+59µs) by main::BEGIN@7 at line 84
use constant IPV6ADDR_INTEGER => 'IPV6ADDR_INTEGER';
# spent 73µs making 1 call to Value::Convertor::BEGIN@84 # spent 59µs making 1 call to constant::import
85
86252µs2123µs
# spent 68µs (13+55) within Value::Convertor::BEGIN@86 which was called: # once (13µs+55µs) by main::BEGIN@7 at line 86
use constant IPV4CIDR_STRING => 'IPV4CIDR_STRING';
# spent 68µs making 1 call to Value::Convertor::BEGIN@86 # spent 55µs making 1 call to constant::import
87253µs2130µs
# spent 71µs (13+58) within Value::Convertor::BEGIN@87 which was called: # once (13µs+58µs) by main::BEGIN@7 at line 87
use constant IPV4NETM_STRING => 'IPV4NETM_STRING';
# spent 71µs making 1 call to Value::Convertor::BEGIN@87 # spent 58µs making 1 call to constant::import
88254µs2133µs
# spent 74µs (15+59) within Value::Convertor::BEGIN@88 which was called: # once (15µs+59µs) by main::BEGIN@7 at line 88
use constant IPV6CIDR_STRING => 'IPV6CIDR_STRING';
# spent 74µs making 1 call to Value::Convertor::BEGIN@88 # spent 59µs making 1 call to constant::import
89
90253µs2124µs
# spent 68µs (13+55) within Value::Convertor::BEGIN@90 which was called: # once (13µs+55µs) by main::BEGIN@7 at line 90
use constant IPV4RNG_STRING => 'IPV4RNG_STRING';
# spent 68µs making 1 call to Value::Convertor::BEGIN@90 # spent 55µs making 1 call to constant::import
91253µs2123µs
# spent 68µs (13+55) within Value::Convertor::BEGIN@91 which was called: # once (13µs+55µs) by main::BEGIN@7 at line 91
use constant IPV4RNG_HEXSTR => 'IPV4RNG_HEXSTR';
# spent 68µs making 1 call to Value::Convertor::BEGIN@91 # spent 55µs making 1 call to constant::import
92253µs2122µs
# spent 67µs (13+55) within Value::Convertor::BEGIN@92 which was called: # once (13µs+55µs) by main::BEGIN@7 at line 92
use constant IPV4RNG_INTEGER => 'IPV4RNG_INTEGER';
# spent 67µs making 1 call to Value::Convertor::BEGIN@92 # spent 54µs making 1 call to constant::import
93
94253µs2122µs
# spent 67µs (12+55) within Value::Convertor::BEGIN@94 which was called: # once (12µs+55µs) by main::BEGIN@7 at line 94
use constant IPV6RNG_STRING => 'IPV6RNG_STRING';
# spent 67µs making 1 call to Value::Convertor::BEGIN@94 # spent 55µs making 1 call to constant::import
95252µs2124µs
# spent 69µs (13+56) within Value::Convertor::BEGIN@95 which was called: # once (13µs+56µs) by main::BEGIN@7 at line 95
use constant IPV6RNG_HEXSTR => 'IPV6RNG_HEXSTR';
# spent 69µs making 1 call to Value::Convertor::BEGIN@95 # spent 56µs making 1 call to constant::import
96253µs2124µs
# spent 68µs (13+55) within Value::Convertor::BEGIN@96 which was called: # once (13µs+55µs) by main::BEGIN@7 at line 96
use constant IPV6RNG_INTEGER => 'IPV6RNG_INTEGER';
# spent 68µs making 1 call to Value::Convertor::BEGIN@96 # spent 55µs making 1 call to constant::import
97
98252µs2128µs
# spent 70µs (13+58) within Value::Convertor::BEGIN@98 which was called: # once (13µs+58µs) by main::BEGIN@7 at line 98
use constant TS_UNIXSTAMP => 'TS_UNIXSTAMP';
# spent 70µs making 1 call to Value::Convertor::BEGIN@98 # spent 58µs making 1 call to constant::import
99252µs2122µs
# spent 67µs (12+55) within Value::Convertor::BEGIN@99 which was called: # once (12µs+55µs) by main::BEGIN@7 at line 99
use constant TS_NTPSTAMP => 'TS_NTPSTAMP';
# spent 67µs making 1 call to Value::Convertor::BEGIN@99 # spent 55µs making 1 call to constant::import
100253µs2122µs
# spent 68µs (13+55) within Value::Convertor::BEGIN@100 which was called: # once (13µs+55µs) by main::BEGIN@7 at line 100
use constant TS_DATETIME => 'TS_DATETIME';
# spent 68µs making 1 call to Value::Convertor::BEGIN@100 # spent 55µs making 1 call to constant::import
1012329µs2123µs
# spent 68µs (13+55) within Value::Convertor::BEGIN@101 which was called: # once (13µs+55µs) by main::BEGIN@7 at line 101
use constant TS_PERIOD => 'TS_PERIOD';
# spent 68µs making 1 call to Value::Convertor::BEGIN@101 # spent 55µs making 1 call to constant::import
102
103149µs166µs
# spent 115µs (49+66) within Value::Convertor::BEGIN@103 which was called: # once (49µs+66µs) by main::BEGIN@7 at line 135
use constant IP_CONVERSIONS => {
# spent 66µs making 1 call to constant::import
104 IPV4ADDR_STRING() =>
105 {'INT' => 'ipv4str_to_int', 'BIN' => 'ipv4str_to_bin'},
106 IPV4ADDR_HEXSTR() =>
107 {'INT' => 'ipv4hex_to_int', 'BIN' => 'ipv4hex_to_bin'},
108 IPV4ADDR_INTEGER() =>
109 {'INT' => 'anything_to_samething', 'BIN' => 'ipv4int_to_bin'},
110 IPV4CIDR_STRING() =>
111 {'INT' => 'ipv4cidr_to_ints', 'BIN' => 'ipv4cidr_to_bins'},
112 IPV4NETM_STRING() =>
113 {'INT' => 'ipv4netm_to_ints', 'BIN' => 'ipv4netm_to_bins'},
114 IPV4RNG_STRING() =>
115 {'INT' => 'ipv4rngstr_to_ints', 'BIN' => 'ipv4rngstr_to_bins'},
116 IPV4RNG_HEXSTR() =>
117 {},
118 IPV4RNG_INTEGER() =>
119 {},
120
121 IPV6ADDR_STRING() =>
122 {'INT' => 'ipv6str_to_bigint', 'BIN' => 'ipv6str_to_bin'},
123 IPV6ADDR_HEXSTR() =>
124 {'INT' => 'hexstr_to_bigint', 'BIN' => 'hexstr_to_bin'},
125 IPV6ADDR_INTEGER() =>
126 {'INT' => 'anything_to_samething', 'BIN' => 'bigint_to_bin'},
127 IPV6CIDR_STRING() =>
128 {'INT' => 'ipv6cidr_to_bigints', 'BIN' => 'ipv6cidr_to_bins'},
129 IPV6RNG_STRING() =>
130 {},
131 IPV6RNG_HEXSTR() =>
132 {},
133 IPV6RNG_INTEGER() =>
134 {},
1351139µs1115µs };
# spent 115µs making 1 call to Value::Convertor::BEGIN@103
136
137112µs160µs
# spent 78µs (17+60) within Value::Convertor::BEGIN@137 which was called: # once (17µs+60µs) by main::BEGIN@7 at line 142
use constant TS_CONVERSIONS => {
# spent 60µs making 1 call to constant::import
138 TS_UNIXSTAMP() => 'unixs_to_nbi',
139 TS_NTPSTAMP() => 'ntps_to_nbi',
140 TS_DATETIME() => 'datestr_to_nbi',
141 TS_PERIOD() => 'periodstr_to_nbis',
142178µs178µs };
# spent 78µs making 1 call to Value::Convertor::BEGIN@137
143
144#-- Static public class variables (our) ---------------------------------------#
145
146#-- Static protected class variables (my) -------------------------------------#
147
148#*******************************************************************************
149# INITIALIZATION AND CLEANUP SECTION
150#*******************************************************************************
151
152#-- Module initializations ----------------------------------------------------#
153
# spent 122ms (158µs+122) within Value::Convertor::BEGIN@153 which was called: # once (158µs+122ms) by main::BEGIN@7 at line 192
BEGIN {
15421.17ms2428µs
# spent 222µs (15+207) within Value::Convertor::BEGIN@154 which was called: # once (15µs+207µs) by main::BEGIN@7 at line 154
use vars qw($VERSION $DEVEL $TIME_ZONE_OFFSET $TIME_ZONE $QR_IPV4 $QR_IPV6 $QR_TS %IP_QRS %TS_QRS);
# spent 222µs making 1 call to Value::Convertor::BEGIN@154 # spent 207µs making 1 call to vars::import
1551900ns $VERSION = '0.1';
1561400ns $DEVEL = 0;
157
158 # Calculate the timezone offset for local timezone
15915µs1121ms $TIME_ZONE = DateTime::TimeZone->new(name => 'local');
# spent 121ms making 1 call to DateTime::TimeZone::new
160112µs21.08ms $TIME_ZONE_OFFSET = DateTime->now(time_zone => $TIME_ZONE)->offset();
# spent 1.01ms making 1 call to DateTime::now # spent 72µs making 1 call to DateTime::offset
161
162149µs816µs $QR_IPV4 = [
# spent 16µs making 8 calls to Value::Convertor::CORE:qr, avg 2µs/call
163 [IPV4ADDR_STRING(), qr/^([\d]+\.[\d]+\.[\d]+\.[\d]+)$/],
164 [IPV4ADDR_HEXSTR(), qr/^(?:0x)?([a-zA-Z0-9]{8})$/],
165 [IPV4ADDR_INTEGER(), qr/^(0|[1-9][0-9]{0,10})$/],
166 [IPV4CIDR_STRING(), qr/^([\d]+\.[\d]+\.[\d]+\.[\d]+)\/([\d]+)$/],
167 [IPV4NETM_STRING(), qr/^([\d]+\.[\d]+\.[\d]+\.[\d]+)\/([\d]+\.[\d]+\.[\d]+\.[\d]+)$/],
168 [IPV4RNG_STRING(), qr/^([\d]+\.[\d]+\.[\d]+\.[\d]+) ?(?:-|\.\.) ?([\d]+\.[\d]+\.[\d]+\.[\d]+)$/],
169 [IPV4RNG_HEXSTR(), qr/^(?:0x)?([a-zA-Z0-9]{8}) ?(?:-|\.\.) ?(?:0x)?([a-zA-Z0-9]{8})$/],
170 [IPV4RNG_INTEGER(), qr/^([0-9]{1,10}) ?(?:-|\.\.) ?([0-9]{1,10})$/],
171 ];
172
173145µs717µs $QR_IPV6 = [
# spent 17µs making 7 calls to Value::Convertor::CORE:qr, avg 2µs/call
174 [IPV6ADDR_STRING(), qr/^([:a-zA-Z0-9]+:[:a-zA-Z0-9]*)$/],
175 [IPV6ADDR_HEXSTR(), qr/^(?:0x)?([a-zA-Z0-9]{32})$/],
176 [IPV6ADDR_INTEGER(), qr/^(0|[1-9][0-9]*)$/],
177 [IPV6CIDR_STRING(), qr/^([:a-zA-Z0-9]+:[:a-zA-Z0-9]+)\/([\d]+)$/],
178 [IPV6RNG_STRING(), qr/^([:a-zA-Z0-9]+:[:a-zA-Z0-9]+) ?(?:-|\.\.) ?([:a-zA-Z0-9]+:[:a-zA-Z0-9]+)$/],
179 [IPV6RNG_HEXSTR(), qr/^(?:0x)?([a-zA-Z0-9]{32}) ?(?:-|\.\.) ?(?:0x)?([a-zA-Z0-9]{32})$/],
180 [IPV6RNG_INTEGER(), qr/^([0-9]+) ?(?:-|\.\.) ?([0-9]+)$/],
181 ];
182
183122µs47µs $QR_TS = [
# spent 7µs making 4 calls to Value::Convertor::CORE:qr, avg 2µs/call
184 [TS_NTPSTAMP(), qr/^(0x[a-zA-Z0-9]+\.0x[a-zA-Z0-9]+)$/],
185 [TS_UNIXSTAMP(), qr/^([0-9]+(?:\.[0-9]+)?)$/],
186 [TS_DATETIME(), qr/^(\d{4}-\d{2}-\d{2}(?:T| )\d{2}:\d{2}(?::\d{2})?(?:Z|\+\d{2}:?\d{2}|-\d{2}:?\d{2})?)$/],
187 [TS_PERIOD(), qr/^(\d{4}-\d{2}-\d{2}(?:T| )\d{2}:\d{2}(?::\d{2})?(?:Z|\+\d{2}:?\d{2}|-\d{2}:?\d{2})?) ?(?:-|\.\.) ?(\d{4}-\d{2}-\d{2}(?:T| )\d{2}:\d{2}(?::\d{2})?(?:Z|\+\d{2}:?\d{2}|-\d{2}:?\d{2})?)$/],
188 ];
189
1901627µs map { $IP_QRS{$_->[0]} = $_->[1] } (@$QR_IPV4, @$QR_IPV6);
191514µs map { $TS_QRS{$_->[0]} = $_->[1] } @$QR_TS;
1921987µs1122ms}
# spent 122ms making 1 call to Value::Convertor::BEGIN@153
193
194
195#-- Module clean-up code (global destructor) ----------------------------------#
19612µs
# spent 2µs within Value::Convertor::END which was called: # once (2µs+0s) by main::RUNTIME at line 0 of mentat.storage.mongo.pl
END {
197
198}
199
200#*******************************************************************************
201# IMPLEMENTATION SECTION
202#*******************************************************************************
203
204=item new() [PUBLIC,STATIC]
205
206 Usage : my $instance = Value::Convertor->new()
207 Purpose : Create and return reference to the new instance
208 Arguments : Same as the the _init() method
209 Returns : Value::Convertor reference on success, croaks on failure
210 Throws : Dies, if invoked on object
211 Comments : Internally uses _init() method, subclasses should never overload this method.
212 See Also : _init() method
213
214=cut
215
216sub new
217{
218 my $class = shift;
219 confess "Class method not invoked on class" if blessed($class);
220 my $self = bless ({}, $class);
221 return $self->_init(@_);
222}
223
224#-------------------------------------------------------------------------------
225# General conversion methods
226#-------------------------------------------------------------------------------
227
228=item anything_to_samething($) [PUBLIC,HYBRID]
229
230 Usage : my $result = Value::Convertor->anything_to_samething($value);
231 my $result = $convertor->anything_to_samething($value);
232 Purpose : Converts anything to same thing (dummy conversion)
233 Arguments : MIXED $anything - Value to be converted [MANDATORY]
234 Returns : MIXED $anything
235 Throws : Dies, if not given any conversion argument
236
237=cut
238
239sub anything_to_samething($)
240{
241 my $invocant = shift;
242 my ($anything,) = @_;
243 confess "Expected conversion argument not given" unless defined $anything;
244
245 return $anything;
246}
247
248=item anything_to_int($) [PUBLIC,HYBRID]
249
250 Usage : my $result = Value::Convertor->anything_to_int($value);
251 my $result = $convertor->anything_to_int($value);
252 Purpose : Converts anything to integer
253 Arguments : MIXED $anything - Value to be converted [MANDATORY]
254 Returns : INTEGER $anything
255 Throws : Dies, if not given any conversion argument
256
257=cut
258
259sub anything_to_int($)
260{
261 my $invocant = shift;
262 my ($anything,) = @_;
263 confess "Expected conversion argument not given" unless defined $anything;
264
265 return int($anything);
266}
267
268=item anything_to_string($) [PUBLIC,HYBRID]
269
270 Usage : my $result = Value::Convertor->anything_to_string($value);
271 my $result = $convertor->anything_to_string($value);
272 Purpose : Converts anything to string
273 Arguments : MIXED $anything - Value to be converted [MANDATORY]
274 Returns : STRING $anything
275 Throws : Dies, if not given any conversion argument
276
277=cut
278
279sub anything_to_string($)
280{
281 my $invocant = shift;
282 my ($anything,) = @_;
283 confess "Expected conversion argument not given" unless defined $anything;
284
285 return "$anything";
286}
287
288#-------------------------------------------------------------------------------
289
290=item hexstr_to_binstr($;$) [PUBLIC,HYBRID]
291
292 Usage : my $result = Value::Convertor->hexstr_to_binstr($value, $width);
293 my $result = $convertor->hexstr_to_binstr($value, $width);
294 Purpose : Converts 'hexadecimal string' to 'binary string'
295 Arguments : STRING $hex - Value to be converted [MANDATORY]
296 INTEGER $width - Width of the string, pad zeros from left [OPTIONAL, DEFAULT = 0]
297 Returns : STRING $bin
298 Throws : Dies, if not given any conversion argument
299
300=cut
301
302sub hexstr_to_binstr($;$)
303{
304 my $invocant = shift;
305 my ($hexstr, $width) = @_;
306 confess "Expected conversion argument not given" unless defined $hexstr;
307
308 # Strip out the leading '0x', if present
309 $hexstr =~ s/^0x//;
310
311 my $binstr = unpack("B*", pack("H*", $invocant->_zero_pad_n($hexstr,2)));
312 $binstr = $invocant->_zero_pad($binstr, $width) if $width;
313
314 return $binstr;
315}
316
317=item binstr_to_hexstr($;$$) [PUBLIC,HYBRID]
318
319 Usage : my $result = Value::Convertor->binstr_to_hexstr($value, $width, $prefix);
320 my $result = $convertor->binstr_to_hexstr($value, $width, $prefix);
321 Purpose : Converts 'hexadecimal string' to 'binary string'
322 Arguments : STRING $binstr - Value to be converted [MANDATORY]
323 INTEGER $width - Width of the string, pad zeros from left [OPTIONAL, DEFAULT = 0]
324 BOOL $prefix - Prepend the '0x' prefix to the result? [OPTIONAL, DEFAULT = 0]
325 Returns : STRING $hexstr
326 Throws : Dies, if not given any conversion argument
327
328=cut
329
330sub binstr_to_hexstr($;$$)
331{
332 my $invocant = shift;
333 my ($binstr, $width, $prefix) = @_;
334 confess "Expected conversion argument not given" unless defined $binstr;
335
336 my $hexstr = unpack("H*", pack("B*", $invocant->_zero_pad_n($binstr,8)));
337 $hexstr = $invocant->_zero_pad($hexstr, $width) if $width;
338
339 return (($prefix)?'0x':'') . uc($hexstr);
340}
341
342#-------------------------------------------------------------------------------
343
344=item hexstr_to_bigint($) [PUBLIC,HYBRID]
345
346 Usage : my $result = Value::Convertor->hexstr_to_bigint($value);
347 my $result = $convertor->hexstr_to_bigint($value);
348 Purpose : Converts 'hexadecimal string' to 'big integer'
349 Arguments : STRING $hexstr - Value to be converted [MANDATORY]
350 Returns : BIGINT $bigint
351 Throws : Dies, if not given any conversion argument
352
353=cut
354
355sub hexstr_to_bigint($)
356{
357 my $invocant = shift;
358 my ($hexstr,) = @_;
359 confess "Expected conversion argument not given" unless defined $hexstr;
360
361 # Strip out the leading '0x', if present
362 $hexstr =~ s/^0x//;
363
3642391µs2118ms
# spent 61.2ms (3.96+57.2) within Value::Convertor::BEGIN@364 which was called: # once (3.96ms+57.2ms) by main::BEGIN@7 at line 364
use bigint;
# spent 61.2ms making 1 call to Value::Convertor::BEGIN@364 # spent 56.8ms making 1 call to bigint::import
365 my $bigint = hex("0x$hexstr");
366
367 return $bigint;
368}
369
370=item bigint_to_hexstr($;$$) [PUBLIC,HYBRID]
371
372 Usage : my $result = Value::Convertor->bigint_to_hexstr($value, $width, $prefix);
373 my $result = $convertor->bigint_to_hexstr($value, $width,$prefix);
374 Purpose : Converts 'big integer' to 'hexadecimal string'
375 Arguments : BIGINT $bigint - Value to be converted [MANDATORY]
376 INTEGER $width - Width of the string, pad zeros from left [OPTIONAL, DEFAULT = 0]
377 BOOL $prefix - Prepend the '0x' prefix to the result? [OPTIONAL, DEFAULT = 0]
378 Returns : STRING $hexstr
379 Throws : Dies, if not given any conversion argument
380
381=cut
382
383sub bigint_to_hexstr($;$$)
384{
385 my $invocant = shift;
386 my ($bigint, $width, $prefix) = @_;
387 confess "Expected conversion argument not given" unless defined $bigint;
388
38925.02ms21.52ms
# spent 770µs (20+750) within Value::Convertor::BEGIN@389 which was called: # once (20µs+750µs) by main::BEGIN@7 at line 389
use bigint;
# spent 770µs making 1 call to Value::Convertor::BEGIN@389 # spent 750µs making 1 call to bigint::import
390 my $hexstr = $bigint->as_hex();
391 $hexstr =~ s/^0x//;
392 $hexstr = $invocant->_zero_pad($hexstr, $width) if $width;
393
394 return (($prefix)?'0x':'') . uc($hexstr);
395}
396
397#-------------------------------------------------------------------------------
398
399=item hexstr_to_bin($) [PUBLIC,HYBRID]
400
401 Usage : my $result = Value::Convertor->hexstr_to_bin($value);
402 my $result = $convertor->hexstr_to_bin($value);
403 Purpose : Converts 'hexadecimal string' to 'binary'
404 Arguments : STRING $hexstr - Value to be converted [MANDATORY]
405 Returns : BINARY $data
406 Throws : Dies, if not given any conversion argument
407
408=cut
409
410sub hexstr_to_bin($)
411{
412 my $invocant = shift;
413 my ($hexstr,) = @_;
414 confess "Expected conversion argument not given" unless defined $hexstr;
415
416 # Strip out the leading '0x', if present
417 $hexstr =~ s/^0x//;
418
419 return pack("H*", $invocant->_zero_pad_n($hexstr,2));
420}
421
422=item bin_to_hexstr($;$$) [PUBLIC,HYBRID]
423
424 Usage : my $result = Value::Convertor->bin_to_hexstr($value, $width, $prefix);
425 my $result = $convertor->bin_to_hexstr($value, $width, $prefix);
426 Purpose : Converts 'binary' to 'hexadecimal string'
427 Arguments : BINARY $data - Value to be converted [MANDATORY]
428 INTEGER $width - Width of the string, pad zeros from left [OPTIONAL, DEFAULT = 0]
429 BOOL $prefix - Prepend the '0x' prefix to the result? [OPTIONAL, DEFAULT = 0]
430 Returns : STRING $hexstr
431 Throws : Dies, if not given any conversion argument
432
433=cut
434
435sub bin_to_hexstr($;$$)
436
# spent 16µs (13+3) within Value::Convertor::bin_to_hexstr which was called 2 times, avg 8µs/call: # 2 times (13µs+3µs) by Mentat::Storage::Mongo::_unbless at line 917 of Mentat/Storage/Mongo.pm, avg 8µs/call
{
43721µs my $invocant = shift;
43822µs my ($data, $width, $prefix) = @_;
4392600ns confess "Expected conversion argument not given" unless defined $data;
440
44127µs23µs my $hexstr = unpack("H*", $data);
# spent 3µs making 2 calls to Value::Convertor::CORE:unpack, avg 1µs/call
4422400ns $hexstr = $invocant->_zero_pad($hexstr, $width) if $width;
443
44427µs return (($prefix)?'0x':'') . uc($hexstr);
445}
446
447#-------------------------------------------------------------------------------
448
449=item binstr_to_bin($) [PUBLIC,HYBRID]
450
451 Usage : my $result = Value::Convertor->binstr_to_bin($value);
452 my $result = $convertor->binstr_to_bin($value);
453 Purpose : Converts 'binary string' to 'binary'
454 Arguments : STRING $binstr - Value to be converted [MANDATORY]
455 Returns : BINARY $bin
456 Throws : Dies, if not given any conversion argument
457
458=cut
459
460sub binstr_to_bin($)
461{
462 my $invocant = shift;
463 my ($binstr,) = @_;
464 confess "Expected conversion argument not given" unless defined $binstr;
465
466 return pack("B*", $invocant->_zero_pad_n($binstr,8));
467}
468
469=item bin_to_binstr($;$) [PUBLIC,HYBRID]
470
471 Usage : my $result = Value::Convertor->bin_to_binstr($value, $width);
472 my $result = $convertor->bin_to_binstr($value, $width);
473 Purpose : Converts 'binary' to 'binary string'
474 Arguments : BINARY $data - Value to be converted [MANDATORY]
475 INTEGER $width - Width of the string, pad zeros from left [OPTIONAL, DEFAULT = 0]
476 Returns : STRING $binstr
477 Throws : Dies, if not given any conversion argument
478
479=cut
480
481sub bin_to_binstr($;$)
482{
483 my $invocant = shift;
484 my ($data, $width) = @_;
485 confess "Expected conversion argument not given" unless defined $data;
486
487 my $binstr = unpack("B*", $data);
488 $binstr = $invocant->_zero_pad($binstr, $width) if $width;
489
490 return $binstr;
491}
492
493#-------------------------------------------------------------------------------
494
495=item bin_to_bigint($) [PUBLIC,HYBRID]
496
497 Usage : my $result = Value::Convertor->bin_to_bigint($value);
498 my $result = $convertor->bin_to_bigint($value);
499 Purpose : Converts 'binary' to 'big integer'
500 Arguments : BINARY $bin - Value to be converted [MANDATORY]
501 Returns : BIGINT $bigint
502 Throws : Dies, if not given any conversion argument
503
504=cut
505
506sub bin_to_bigint($)
507{
508 my $invocant = shift;
509 my ($bin,) = @_;
510 confess "Expected conversion argument not given" unless defined $bin;
511
512 return $invocant->hexstr_to_bigint($invocant->bin_to_hexstr($bin));
513}
514
515=item bigint_to_bin($;$$) [PUBLIC,HYBRID]
516
517 Usage : my $result = Value::Convertor->bigint_to_bin($value);
518 my $result = $convertor->bigint_to_bin($value);
519 Purpose : Converts 'big integer' to 'binary'
520 Arguments : BIGINT $bigint - Value to be converted [MANDATORY]
521 Returns : BINARY $bin
522 Throws : Dies, if not given any conversion argument
523
524=cut
525
526sub bigint_to_bin($;$$)
527{
528 my $invocant = shift;
529 my ($bigint,) = @_;
530 confess "Expected conversion argument not given" unless defined $bigint;
531
532 return $invocant->hexstr_to_bin($invocant->bigint_to_hexstr($bigint));
533}
534
535#-------------------------------------------------------------------------------
536# IPv4 conversion methods
537#-------------------------------------------------------------------------------
538
539=item ipv4str_to_int($) [PUBLIC,HYBRID]
540
541 Usage : my $result = Value::Convertor->ipv4str_to_int($value);
542 my $result = $convertor->ipv4str_to_int($value);
543 Purpose : Converts 'ipv4str' to 'integer'
544 Arguments : STRING $ipv4str - Value to be converted [MANDATORY]
545 Returns : INTEGER $ipv4int
546 Throws : Dies, if not given any conversion argument
547
548=cut
549
550sub ipv4str_to_int($)
551
# spent 38µs (24+14) within Value::Convertor::ipv4str_to_int which was called: # once (24µs+14µs) by Value::Convertor::ipv4cidr_to_ints at line 782
{
5521500ns my $invocant = shift;
5531700ns my ($ipv4str,) = @_;
5541300ns confess "Expected conversion argument not given" unless defined $ipv4str;
555
55611µs my $ptrn = $IP_QRS{IPV4ADDR_STRING()};
557116µs25µs return undef unless $ipv4str =~ /$ptrn/;
# spent 4µs making 1 call to Value::Convertor::CORE:match # spent 1µs making 1 call to Value::Convertor::CORE:regcomp
558
559121µs29µs return unpack('N', pack('C4', split(/\./, $ipv4str)));
# spent 7µs making 1 call to Value::Convertor::CORE:pack # spent 2µs making 1 call to Value::Convertor::CORE:unpack
560}
561
562=item int_to_ipv4str($) [PUBLIC,HYBRID]
563
564 Usage : my $result = Value::Convertor->int_to_ipv4str($value);
565 my $result = $convertor->int_to_ipv4str($value);
566 Purpose : Converts 'integer' to 'ipv4str'
567 Arguments : INTEGER $ipv4int - Value to be converted [MANDATORY]
568 Returns : STRING $ipv4str
569 Throws : Dies, if not given any conversion argument
570
571=cut
572
573sub int_to_ipv4str($)
574{
575 my $invocant = shift;
576 my ($int,) = @_;
577 confess "Expected conversion argument not given" unless defined $int;
578
579 return join('.', unpack('C4', pack('N', $int)));
580}
581
582#-------------------------------------------------------------------------------
583
584=item ipv4hex_to_int($) [PUBLIC,HYBRID]
585
586 Usage : my $result = Value::Convertor->ipv4hex_to_int($value);
587 my $result = $convertor->ipv4hex_to_int($value);
588 Purpose : Converts 'ipv4hex' to 'integer'
589 Arguments : STRING $ipv4hex - Value to be converted [MANDATORY]
590 Returns : INTEGER $ipv4int
591 Throws : Dies, if not given any conversion argument
592
593=cut
594
595sub ipv4hex_to_int($)
596{
597 my $invocant = shift;
598 my ($ipv4hex,) = @_;
599 confess "Expected conversion argument not given" unless defined $ipv4hex;
600
601 $ipv4hex =~ s/^0x//;
602 return unpack("N", pack("H*", $ipv4hex));
603}
604
605=item int_to_ipv4hex($) [PUBLIC,HYBRID]
606
607 Usage : my $result = Value::Convertor->int_to_ipv4hex($value);
608 my $result = $convertor->int_to_ipv4hex($value);
609 Purpose : Converts 'integer' to 'ipv4hex'
610 Arguments : INTEGER $ipv4int - Value to be converted [MANDATORY]
611 Returns : STRING $ipv4hex
612 Throws : Dies, if not given any conversion argument
613
614=cut
615
616sub int_to_ipv4hex($)
617{
618 my $invocant = shift;
619 my ($ipv4int,) = @_;
620 confess "Expected conversion argument not given" unless defined $ipv4int;
621
622 return uc(unpack("H*", pack("N", $ipv4int)));
623}
624
625#-------------------------------------------------------------------------------
626
627=item ipv4str_to_bin($) [PUBLIC,HYBRID]
628
629 Usage : my $result = Value::Convertor->ipv4str_to_bin($value);
630 my $result = $convertor->ipv4str_to_bin($value);
631 Purpose : Converts 'ipv4str' to 'binary'
632 Arguments : STRING $ipv4str - Value to be converted [MANDATORY]
633 Returns : BINARY $ipv4data
634 Throws : Dies, if not given any conversion argument
635
636=cut
637
638sub ipv4str_to_bin($)
639{
640 my $invocant = shift;
641 my ($ipv4str,) = @_;
642 confess "Expected conversion argument not given" unless defined $ipv4str;
643
644 return pack('C4', split(/\./, $ipv4str));
645}
646
647=item bin_to_ipv4str($) [PUBLIC,HYBRID]
648
649 Usage : my $result = Value::Convertor->bin_to_ipv4str($value);
650 my $result = $convertor->bin_to_ipv4str($value);
651 Purpose : Converts 'binary' to 'ipv4str'
652 Arguments : BINARY $ipv4data - Value to be converted [MANDATORY]
653 Returns : STRING $ipv4str
654 Throws : Dies, if not given any conversion argument
655
656=cut
657
658sub bin_to_ipv4str($)
659{
660 my $invocant = shift;
661 my ($ipv4data,) = @_;
662 confess "Expected conversion argument not given" unless defined $ipv4data;
663
664 return join('.', unpack('C4', $ipv4data));
665}
666
667#-------------------------------------------------------------------------------
668
669=item ipv4hex_to_bin($) [PUBLIC,HYBRID]
670
671 Usage : my $result = Value::Convertor->ipv4hex_to_bin($value);
672 my $result = $convertor->ipv4hex_to_bin($value);
673 Purpose : Converts 'ipv4hex' to 'binary'
674 Arguments : STRING $ipv4hex - Value to be converted [MANDATORY]
675 Returns : BINARY $ipv4data
676 Throws : Dies, if not given any conversion argument
677
678=cut
679
680sub ipv4hex_to_bin($)
681{
682 my $invocant = shift;
683 my ($ipv4hex,) = @_;
684 confess "Expected conversion argument not given" unless defined $ipv4hex;
685
686 return $invocant->hexstr_to_bin($ipv4hex);
687}
688
689=item bin_to_ipv4hex($) [PUBLIC,HYBRID]
690
691 Usage : my $result = Value::Convertor->bin_to_ipv4hex($value);
692 my $result = $convertor->bin_to_ipv4hex($value);
693 Purpose : Converts 'binary' to 'ipv4hex'
694 Arguments : BINARY $ipv4data - Value to be converted [MANDATORY]
695 Returns : STRING $ipv4hex
696 Throws : Dies, if not given any conversion argument
697
698=cut
699
700sub bin_to_ipv4hex($)
701{
702 my $invocant = shift;
703 my ($ipv4bin,) = @_;
704 confess "Expected conversion argument not given" unless defined $ipv4bin;
705
706 return $invocant->bin_to_hexstr($ipv4bin);
707}
708
709#-------------------------------------------------------------------------------
710
711=item ipv4int_to_bin($) [PUBLIC,HYBRID]
712
713 Usage : my $result = Value::Convertor->ipv4int_to_bin($value);
714 my $result = $convertor->ipv4int_to_bin($value);
715 Purpose : Converts 'ipv4int' to 'binary'
716 Arguments : INTEGER $ipv4int - Value to be converted [MANDATORY]
717 Returns : BINARY $ipv4data
718 Throws : Dies, if not given any conversion argument
719
720=cut
721
722sub ipv4int_to_bin($)
723
# spent 12µs (9+3) within Value::Convertor::ipv4int_to_bin which was called 2 times, avg 6µs/call: # 2 times (9µs+3µs) by Value::Convertor::ipv4cidr_to_bins at line 812, avg 6µs/call
{
7242800ns my $invocant = shift;
72521µs my ($ipv4int,) = @_;
7262500ns confess "Expected conversion argument not given" unless defined $ipv4int;
727
728211µs23µs return pack("N", $ipv4int);
# spent 3µs making 2 calls to Value::Convertor::CORE:pack, avg 1µs/call
729}
730
731=item bin_to_ipv4int($) [PUBLIC,HYBRID]
732
733 Usage : my $result = Value::Convertor->bin_to_ipv4int($value);
734 my $result = $convertor->bin_to_ipv4int($value);
735 Purpose : Converts 'binary' to 'ipv4int'
736 Arguments : BINARY $ipv4data - Value to be converted [MANDATORY]
737 Returns : INTEGER $ipv4int
738 Throws : Dies, if not given any conversion argument
739
740=cut
741
742sub bin_to_ipv4int($)
743{
744 my $invocant = shift;
745 my ($ipv4bin,) = @_;
746 confess "Expected conversion argument not given" unless defined $ipv4bin;
747
748 return unpack('N', $ipv4bin);
749}
750
751#-------------------------------------------------------------------------------
752
753=item ipv4cidr_to_ints($;$) [PUBLIC,HYBRID]
754
755 Usage : my $result = Value::Convertor->ipv4cidr_to_ints($value, $length);
756 my $result = $convertor->ipv4cidr_to_ints($value, $lenght);
757 Purpose : Converts 'ipv4cidr string' to 'integers'
758 Arguments : STRING $ipv4cidr - Value to be converted [MANDATORY]
759 INTEGER $length - Length of the network, can be parsed out from previous argument [OPTIONAL]
760 Returns : INTEGER $min
761 INTEGER $max
762 Throws : Dies, if not given any conversion argument
763
764=cut
765
766sub ipv4cidr_to_ints($;$)
767
# spent 49µs (11+38) within Value::Convertor::ipv4cidr_to_ints which was called: # once (11µs+38µs) by Value::Convertor::ipv4cidr_to_bins at line 811
{
7681500ns my $invocant = shift;
76911µs my ($ipv4cidr,$length) = @_;
7701500ns confess "Expected conversion argument not given" unless defined $ipv4cidr;
771
7721200ns unless ($length)
773 {
774 my $ptrn = $IP_QRS{IPV4CIDR_STRING()};
775 my @matches = ($ipv4cidr =~ /$ptrn/);
776 ($ipv4cidr,$length) = @matches if @matches;
777 }
7781300ns confess "Invalid network address given as argument" unless defined $ipv4cidr;
7791200ns confess "Invalid network length given as argument" unless defined $length;
780
781 # Convert the IP address to integer
78213µs138µs my $ipv4int = $invocant->ipv4str_to_int($ipv4cidr);
# spent 38µs making 1 call to Value::Convertor::ipv4str_to_int
783
784 # Masks must be applied, otherwise this algorithm would fail on 64bit platforms !!!
7851900ns my $netmask_int = (MAX_IP_MASK() << (32 - $length)) & MAX_IP_MASK();
7861600ns my $wildmask_int = (~ $netmask_int) & MAX_IP_MASK();
787
788 # And now calculate the ranges
7891400ns my $min = ($ipv4int & $netmask_int);
7901300ns my $max = ($ipv4int & $netmask_int) + $wildmask_int;
791
79213µs return ($min, $max);
793}
794
795=item ipv4cidr_to_bins($;$) [PUBLIC,HYBRID]
796
797 Usage : my $result = Value::Convertor->ipv4cidr_to_bins($value);
798 my $result = $convertor->ipv4cidr_to_bins($value);
799 Purpose : Converts 'ipv4cidr string' to 'binaries'
800 Arguments : Same as ipv4cidr_to_ints() method
801 Returns : BINARY $min
802 BINARY $max
803 Throws : Dies, if not given any conversion argument
804
805=cut
806
807sub ipv4cidr_to_bins($;$)
808
# spent 74µs (13+61) within Value::Convertor::ipv4cidr_to_bins which was called: # once (13µs+61µs) by main::RUNTIME at line 10 of mentat.storage.mongo.pl
{
8091900ns my $invocant = shift;
810
81113µs149µs my ($min, $max) = $invocant->ipv4cidr_to_ints(@_);
# spent 49µs making 1 call to Value::Convertor::ipv4cidr_to_ints
81217µs212µs return ($invocant->ipv4int_to_bin($min), $invocant->ipv4int_to_bin($max));
# spent 12µs making 2 calls to Value::Convertor::ipv4int_to_bin, avg 6µs/call
813}
814
815#-------------------------------------------------------------------------------
816
817=item ipv4rngstr_to_ints($;$) [PUBLIC,HYBRID]
818
819 Usage : my $result = Value::Convertor->ipv4rngstr_to_ints($value, $length);
820 my $result = $convertor->ipv4rngstr_to_ints($value, $lenght);
821 Purpose : Converts 'ipv4rngstr string' to 'integers'
822 Arguments : STRING $ipv4rngstr - Value to be converted [MANDATORY]
823 INTEGER $second - Length of the network, can be parsed out from previous argument [OPTIONAL]
824 Returns : INTEGER $min
825 INTEGER $max
826 Throws : Dies, if not given any conversion argument
827
828=cut
829
830sub ipv4rngstr_to_ints($;$)
831{
832 my $invocant = shift;
833 my ($ipv4rngstr,$second) = @_;
834 confess "Expected conversion argument not given" unless defined $ipv4rngstr;
835
836 unless ($second)
837 {
838 my $ptrn = $IP_QRS{IPV4RNG_STRING()};
839 my @matches = ($ipv4rngstr =~ /$ptrn/);
840 ($ipv4rngstr,$second) = @matches if @matches;
841 }
842 confess "Invalid network range beginning address given as argument" unless defined $ipv4rngstr;
843 confess "Invalid network range end address given as argument" unless defined $second;
844
845 # Convert the IP address to integer
846 my $min = $invocant->ipv4str_to_int($ipv4rngstr);
847 my $max = $invocant->ipv4str_to_int($second);
848
849 return ($min, $max);
850}
851
852=item ipv4rngstr_to_bins($;$) [PUBLIC,HYBRID]
853
854 Usage : my $result = Value::Convertor->ipv4rngstr_to_bins($value);
855 my $result = $convertor->ipv4rngstr_to_bins($value);
856 Purpose : Converts 'ipv4rngstr string' to 'binaries'
857 Arguments : Same as ipv4rngstr_to_ints() method
858 Returns : BINARY $min
859 BINARY $max
860 Throws : Dies, if not given any conversion argument
861
862=cut
863
864sub ipv4rngstr_to_bins($;$)
865{
866 my $invocant = shift;
867
868 my ($min, $max) = $invocant->ipv4rngstr_to_ints(@_);
869 return ($invocant->ipv4int_to_bin($min), $invocant->ipv4int_to_bin($max));
870}
871
872#-------------------------------------------------------------------------------
873
874=item ipv4netm_to_ints($;$) [PUBLIC,HYBRID]
875
876 Usage : my $result = Value::Convertor->ipv4netm_to_ints($network, $netmask);
877 my $result = $convertor->ipv4netm_to_ints($value, $lenght);
878 Purpose : Converts 'ipv4rngstr string' to 'integers'
879 Arguments : STRING $ipv4rngstr - Value to be converted [MANDATORY]
880 INTEGER $second - Length of the network, can be parsed out from previous argument [OPTIONAL]
881 Returns : INTEGER $min
882 INTEGER $max
883 Throws : Dies, if not given any conversion argument
884
885=cut
886
887sub ipv4netm_to_ints($;$)
888{
889 my $invocant = shift;
890 my ($ipv4netm, $netmask) = @_;
891 confess "Expected conversion argument not given" unless defined $ipv4netm;
892
893 unless ($netmask)
894 {
895 my $ptrn = $IP_QRS{IPV4NETM_STRING()};
896 my @matches = ($ipv4netm =~ /$ptrn/);
897 ($ipv4netm, $netmask) = @matches if @matches;
898 }
899 confess "Invalid network address given as argument" unless defined $ipv4netm;
900 confess "Invalid network mask given as argument" unless defined $netmask;
901
902 # Convert the IP address to integer
903 my $ipv4int = $invocant->ipv4str_to_int($ipv4netm);
904 my $netmask_int = $invocant->ipv4str_to_int($netmask);
905
906 # This algorithm would be perfect on 32bit platforms, sadly, it fails on 64bit !!!
907 my $wildmask_int = (~ $netmask_int) & MAX_IP_MASK();
908
909 # And now calculate the ranges
910 my $min = ($ipv4int & $netmask_int);
911 my $max = ($ipv4int & $netmask_int) + $wildmask_int;
912
913 return ($min, $max);
914}
915
916=item ipv4netm_to_bins($;$) [PUBLIC,HYBRID]
917
918 Usage : my $result = Value::Convertor->ipv4netm_to_bins($value);
919 my $result = $convertor->ipv4netm_to_bins($value);
920 Purpose : Converts 'ipv4rngstr string' to 'binaries'
921 Arguments : Same as ipv4rngstr_to_ints() method
922 Returns : BINARY $min
923 BINARY $max
924 Throws : Dies, if not given any conversion argument
925
926=cut
927
928sub ipv4netm_to_bins($;$)
929{
930 my $invocant = shift;
931
932 my ($min, $max) = $invocant->ipv4netm_to_ints(@_);
933 return ($invocant->ipv4int_to_bin($min), $invocant->ipv4int_to_bin($max));
934}
935
936#-------------------------------------------------------------------------------
937
938=item ints_to_ipv4cidr($$) [PUBLIC,HYBRID]
939
940 Usage : my $result = Value::Convertor->ints_to_ipv4cidr($min, $max);
941 my $result = $convertor->ints_to_ipv4cidr($min, $max);
942 Purpose : Converts 'integers' to 'ipv4cidr string'
943 Arguments : INTEGER $min - minimal IP [MANDATORY]
944 INTEGER $max - maximal IP [MANDATORY]
945 Returns : STRING $ipv4cidr
946 Throws : Dies, if not given any conversion argument
947
948=cut
949
950sub ints_to_ipv4cidr($;$)
951{
952 my $invocant = shift;
953 my ($min, $max) = @_;
954 confess "Expected conversion argument not given" unless defined $min and defined $max;
955
956 my $network = ($min & $max);
957 my $wildmask = ($min ^ $max);
958
959 my ($size, $i) = (32, 1);
960 while ($i < $wildmask)
961 {
962 $size--;
963 $i = $i << 1;
964 }
965
966 return $invocant->int_to_ipv4str($network) . "/$size";
967}
968
969=item ints_to_ipv4netm($$) [PUBLIC,HYBRID]
970
971 Usage : my $result = Value::Convertor->ints_to_ipv4netm($min, $max);
972 my $result = $convertor->ints_to_ipv4netm($min, $max);
973 Purpose : Converts 'integers' to 'ipv4netm string'
974 Arguments : INTEGER $min - minimal IP [MANDATORY]
975 INTEGER $max - maximal IP [MANDATORY]
976 Returns : STRING $ipv4cidr
977 Throws : Dies, if not given any conversion argument
978
979=cut
980
981sub ints_to_ipv4netm($;$)
982{
983 my $invocant = shift;
984 my ($min,$max) = @_;
985 confess "Expected conversion argument not given" unless defined $min and defined $max;
986
987 my $network = ($min & $max);
988 my $wildmask = ($min ^ $max);
989 my $netmask = (MAX_IP_MASK() ^ $wildmask) & MAX_IP_MASK();
990
991 return $invocant->int_to_ipv4str($network) . "/" . $invocant->int_to_ipv4str($netmask);
992}
993
994=item ints_to_ipv4rngstr($$) [PUBLIC,HYBRID]
995
996 Usage : my $result = Value::Convertor->ints_to_ipv4rngstr($min, $max);
997 my $result = $convertor->ints_to_ipv4rngstr($min, $max);
998 Purpose : Converts 'integers' to 'ipv4rngstr string'
999 Arguments : INTEGER $min - minimal IP [MANDATORY]
1000 INTEGER $max - maximal IP [MANDATORY]
1001 Returns : STRING $ipv4cidr
1002 Throws : Dies, if not given any conversion argument
1003
1004=cut
1005
1006sub ints_to_ipv4rngstr($;$)
1007{
1008 my $invocant = shift;
1009 my ($min,$max) = @_;
1010 confess "Expected conversion argument not given" unless defined $min and defined $max;
1011
1012 return $invocant->int_to_ipv4str($min) . '..' . $invocant->int_to_ipv4str($max);
1013}
1014
1015#-------------------------------------------------------------------------------
1016# IPv6 conversion methods
1017#-------------------------------------------------------------------------------
1018
1019=item ipv6_expand($) [PUBLIC,HYBRID]
1020
1021 Usage : my $result = Value::Convertor->ipv6_expand($value);
1022 my $result = $convertor->ipv6_expand($value);
1023 Purpose : Expand the shortened IPv6 address string
1024 Arguments : STRING $ipv6str - Value to be converted [MANDATORY]
1025 Returns : STRING $ipv6str
1026 Throws : Dies, if not given any conversion argument
1027
1028=cut
1029
1030sub ipv6_expand($)
1031{
1032 my $invocant = shift;
1033 my ($ipv6str,) = @_;
1034 confess "Expected conversion argument not given" unless defined $ipv6str;
1035
1036 # Calculate number of IPv6 address sections
1037 my $tmp = $ipv6str;
1038 $tmp =~ s/^:+|:+$//g;
1039 my @sects = split(/:+/, $tmp);
1040
1041 # If there are too few sections, IPv6 address was in compressed format
1042 if (scalar(@sects) != 8)
1043 {
1044 my @zeros = ();
1045 push(@zeros, 0) while ((scalar(@zeros) + scalar(@sects)) < 8);
1046 my $zeros = join(':', @zeros);
1047 $ipv6str =~ s/::/:$zeros:/;
1048 $ipv6str =~ s/^:|:$//g;
1049 @sects = split(/:+/, $ipv6str);
1050 }
1051
1052 # Pad with zeroes from the left according to RFC 3513, section 2.3
1053 # (Internet Protocol Version 6 (IPv6) Addressing Architecture)
1054 my @result = map { $invocant->_zero_pad($_, 4) } @sects;
1055
1056 return join(':', @result);
1057}
1058
1059=item ipv6_collapse($) [PUBLIC,HYBRID]
1060
1061 Usage : my $result = Value::Convertor->ipv6_collapse($value);
1062 my $result = $convertor->ipv6_collapse($value);
1063 Purpose : Collapse the expanded IPv6 address string
1064 Arguments : STRING $ipv6str - Value to be converted [MANDATORY]
1065 Returns : STRING $ipv6str
1066 Throws : Dies, if not given any conversion argument
1067
1068=cut
1069
1070sub ipv6_collapse($)
1071{
1072 my $invocant = shift;
1073 my ($ipv6str,) = @_;
1074 confess "Expected conversion argument not given" unless defined $ipv6str;
1075
1076 # Split IPv6 address to sections
1077 my @sects = split(/:+/, $ipv6str);
1078
1079 # Remove the leading zeros
1080 my @tmp = map { $_ =~ s/^0*(\d+)$/$1/; $_; } @sects;
1081 $ipv6str = join(':', @tmp);
1082
1083 # Find the maximal string of zeros
1084 my $max = "";
1085 while ($ipv6str =~ /(0(:0)+)/gs) {
1086 $max = $1 if length($1) > length($max);
1087 }
1088 if ($max)
1089 {
1090 # Replace it with double colon
1091 $ipv6str =~ s/$max/::/;
1092 # Cleanup after last operation
1093 $ipv6str =~ s/:::+/::/;
1094 }
1095 return $ipv6str;
1096}
1097
1098#-------------------------------------------------------------------------------
1099
1100=item ipv6str_to_hexstr($) [PUBLIC,HYBRID]
1101
1102 Usage : my $result = Value::Convertor->ipv6str_to_hexstr($value);
1103 my $result = $convertor->ipv6str_to_hexstr($value);
1104 Purpose : Converts 'ipv6str' to 'hexadecimal string'
1105 Arguments : STRING $ipv6str - Value to be converted [MANDATORY]
1106 Returns : STRING $hexstr
1107 Throws : Dies, if not given any conversion argument
1108
1109=cut
1110
1111sub ipv6str_to_hexstr($)
1112{
1113 my $invocant = shift;
1114 my ($ipv6str,) = @_;
1115 confess "Expected conversion argument not given" unless defined $ipv6str;
1116
1117 $ipv6str = $invocant->ipv6_expand($ipv6str);
1118 $ipv6str =~ s/://g;
1119
1120 return uc($ipv6str);
1121}
1122
1123=item hexstr_to_ipv6str($) [PUBLIC,HYBRID]
1124
1125 Usage : my $result = Value::Convertor->hexstr_to_ipv6st($value);
1126 my $result = $convertor->hexstr_to_ipv6st($value);
1127 Purpose : Converts 'hexadecimal string' to 'ipv6str'
1128 Arguments : STRING $hexstr - Value to be converted [MANDATORY]
1129 Returns : STRING $ipv6str
1130 Throws : Dies, if not given any conversion argument
1131
1132=cut
1133
1134sub hexstr_to_ipv6str($)
1135{
1136 my $invocant = shift;
1137 my ($ipv6hex,) = @_;
1138 confess "Expected conversion argument not given" unless defined $ipv6hex;
1139
1140 return $invocant->ipv6_collapse(join(':', unpack('(A4)*', $ipv6hex)));
1141}
1142
1143#-------------------------------------------------------------------------------
1144
1145=item ipv6str_to_bin($) [PUBLIC,HYBRID]
1146
1147 Usage : my $result = Value::Convertor->ipv6str_to_bin($value);
1148 my $result = $convertor->ipv6str_to_bin($value);
1149 Purpose : Converts 'ipv6str' to 'binary'
1150 Arguments : STRING $ipv6str - Value to be converted [MANDATORY]
1151 Returns : BINARY $ipv6bin
1152 Throws : Dies, if not given any conversion argument
1153
1154=cut
1155
1156sub ipv6str_to_bin($)
1157{
1158 my $invocant = shift;
1159 my ($ipv6str,) = @_;
1160 confess "Expected conversion argument not given" unless defined $ipv6str;
1161
1162 $ipv6str = $invocant->ipv6_expand($ipv6str);
1163 $ipv6str =~ s/://g;
1164
1165 return $invocant->hexstr_to_bin($ipv6str);
1166}
1167
1168=item bin_to_ipv6str($) [PUBLIC,HYBRID]
1169
1170 Usage : my $result = Value::Convertor->bin_to_ipv6str($value);
1171 my $result = $convertor->bin_to_ipv6str($value);
1172 Purpose : Converts 'binary' to 'ipv6str'
1173 Arguments : BINARY $ipv6bin - Value to be converted [MANDATORY]
1174 Returns : STRING $ipv6str
1175 Throws : Dies, if not given any conversion argument
1176
1177=cut
1178
1179sub bin_to_ipv6str($)
1180{
1181 my $invocant = shift;
1182 my ($ipv6bin,) = @_;
1183 confess "Expected conversion argument not given" unless defined $ipv6bin;
1184
1185 return $invocant->ipv6_collapse(join(':', unpack('(A4)*', $invocant->bin_to_hexstr($ipv6bin))));
1186}
1187
1188#-------------------------------------------------------------------------------
1189
1190=item ipv6str_to_bigint($) [PUBLIC,HYBRID]
1191
1192 Usage : my $result = Value::Convertor->ipv6str_to_bigint($value);
1193 my $result = $convertor->ipv6str_to_bigint($value);
1194 Purpose : Converts 'ipv6str' to 'bigint'
1195 Arguments : STRING $ipv6str - Value to be converted [MANDATORY]
1196 Returns : BIGINT $ipv6bigint
1197 Throws : Dies, if not given any conversion argument
1198
1199=cut
1200
1201sub ipv6str_to_bigint($)
1202{
1203 my $invocant = shift;
1204 my ($ipv6str,) = @_;
1205 confess "Expected conversion argument not given" unless defined $ipv6str;
1206
1207 $ipv6str = $invocant->ipv6_expand($ipv6str);
1208 $ipv6str =~ s/://g;
1209
1210 return $invocant->hexstr_to_bigint($ipv6str);
1211}
1212
1213=item bigint_to_ipv6str($) [PUBLIC,HYBRID]
1214
1215 Usage : my $result = Value::Convertor->bigint_to_ipv6str($value);
1216 my $result = $convertor->bigint_to_ipv6str($value);
1217 Purpose : Converts 'bigint' to 'ipv6str'
1218 Arguments : BIGINT $ipv6bigint - Value to be converted [MANDATORY]
1219 Returns : STRING $ipv6str
1220 Throws : Dies, if not given any conversion argument
1221
1222=cut
1223
1224sub bigint_to_ipv6str($)
1225{
1226 my $invocant = shift;
1227 my ($ipv6bigint,) = @_;
1228 confess "Expected conversion argument not given" unless defined $ipv6bigint;
1229
1230249µs21.57ms
# spent 794µs (19+776) within Value::Convertor::BEGIN@1230 which was called: # once (19µs+776µs) by main::BEGIN@7 at line 1230
use bigint;
# spent 794µs making 1 call to Value::Convertor::BEGIN@1230 # spent 776µs making 1 call to bigint::import
123113.37ms1142µs return $invocant->ipv6_collapse(join(':', unpack('(A4)*', $invocant->bigint_to_hexstr($ipv6bigint,32))));
# spent 142µs making 1 call to Math::BigInt::__ANON__[Math/BigInt.pm:2702]
1232}
1233
1234#-------------------------------------------------------------------------------
1235# IP address detection
1236#-------------------------------------------------------------------------------
1237
1238=item detect_ipv4($) [PUBLIC,HYBRID]
1239
1240 Usage : my $result = Value::Convertor->detect_ipv4($value);
1241 my $result = $convertor->detect_ipv4($value);
1242 Purpose : Detect type of IPv4 adress/network/range
1243 Arguments : STRING $address - Value to be converted [MANDATORY]
1244 Returns : STRING $type
1245 Throws : Dies, if not given any conversion argument
1246
1247=cut
1248
1249sub detect_ipv4($)
1250{
1251 my $invocant = shift;
1252 my ($address,) = @_;
1253 confess "Expected conversion argument not given" unless defined $address;
1254
1255 my ($type, $ptrn, @matches);
1256 foreach my $t (@$QR_IPV4)
1257 {
1258 ($type, $ptrn) = @$t;
1259 @matches = ($address =~ /$ptrn/);
1260 return ($type, @matches) if @matches;
1261 }
1262 return undef;
1263}
1264
1265=item detect_ipv6($) [PUBLIC,HYBRID]
1266
1267 Usage : my $result = Value::Convertor->detect_ipv6($value);
1268 my $result = $convertor->detect_ipv6($value);
1269 Purpose : Detect type of IPv6 adress/network/range
1270 Arguments : STRING $address - Value to be converted [MANDATORY]
1271 Returns : STRING $type
1272 Throws : Dies, if not given any conversion argument
1273
1274=cut
1275
1276sub detect_ipv6($)
1277{
1278 my $invocant = shift;
1279 my ($address,) = @_;
1280 confess "Expected conversion argument not given" unless defined $address;
1281
1282 my ($type, $ptrn, @matches);
1283 foreach my $t (@$QR_IPV6)
1284 {
1285 ($type, $ptrn) = @$t;
1286 @matches = ($address =~ /$ptrn/);
1287 return ($type, @matches) if @matches;
1288 }
1289 return undef;
1290}
1291
1292=item detect_ip($) [PUBLIC,HYBRID]
1293
1294 Usage : my $result = Value::Convertor->detect_ip($value);
1295 my $result = $convertor->detect_ip($value);
1296 Purpose : Detect type of IPv4/IPv6 adress/network/range
1297 Arguments : STRING $address - Value to be converted [MANDATORY]
1298 Returns : STRING $type
1299 Throws : Dies, if not given any conversion argument
1300
1301=cut
1302
1303sub detect_ip($)
1304{
1305 my $invocant = shift;
1306 my ($address,) = @_;
1307 confess "Expected conversion argument not given" unless defined $address;
1308
1309 my ($type, @matches);
1310 ($type, @matches) = $invocant->detect_ipv4($address);
1311 return ($type, @matches) if $type;
1312 ($type, @matches) = $invocant->detect_ipv6($address);
1313 return ($type, @matches);
1314}
1315
1316#-------------------------------------------------------------------------------
1317
1318=item ipv4_to_int($;$) [PUBLIC,HYBRID]
1319
1320 Usage : my $result = Value::Convertor->ipv4_to_int($value);
1321 my $result = $convertor->ipv4_to_int($value);
1322 Purpose : Converts 'ipv4' adress in any representation to 'integer'
1323 Arguments : INTEGER $ipv4 - Value to be converted [MANDATORY]
1324 STRING $type - Type of the argument (bypass autodetection) [OPTIONAL]
1325 Returns : INTEGER $ipv4int
1326 Throws : Dies, if not given any conversion argument
1327
1328=cut
1329
1330sub ipv4_to_int($)
1331{
1332 my $invocant = shift;
1333 my ($ipv4, $type, @matches) = @_;
1334 confess "Expected conversion argument not given" unless defined $ipv4;
1335
1336 ($type, @matches) = $invocant->detect_ipv4($ipv4) unless $type;
1337 confess "Invalid argument '$ipv4' given" unless $type;
1338 confess "Do not know how to convert $type to integer" unless exists IP_CONVERSIONS()->{$type}->{INT};
1339
1340 my $conversion = IP_CONVERSIONS()->{$type}->{INT};
1341 return ($type, $invocant->$conversion(@matches)) if $conversion;
1342 return (UNKNOWN_TYPE(), $ipv4);
1343}
1344
1345=item ipv4_to_bin($) [PUBLIC,HYBRID]
1346
1347 Usage : my $result = Value::Convertor->ipv4_to_bin($value);
1348 my $result = $convertor->ipv4_to_bin($value);
1349 Purpose : Converts 'ipv4' adress in any representation to 'binary'
1350 Arguments : Same as ipv4_to_int() method
1351 Returns : BINARY $binary
1352 Throws : Dies, if not given any conversion argument
1353 See also : ipv4_to_int() method
1354
1355=cut
1356
1357sub ipv4_to_bin($)
1358{
1359 my $invocant = shift;
1360 my ($ipv4, $type, @matches) = @_;
1361 confess "Expected conversion argument not given" unless defined $ipv4;
1362
1363 ($type, @matches) = $invocant->detect_ipv4($ipv4) unless $type;
1364 confess "Invalid argument '$ipv4' given" unless $type;
1365 confess "Do not know how to convert $type to binary" unless exists IP_CONVERSIONS()->{$type}->{BIN};
1366
1367 my $conversion = IP_CONVERSIONS()->{$type}->{BIN};
1368 return ($type, $invocant->$conversion(@matches)) if $conversion;
1369 return (UNKNOWN_TYPE(), $ipv4);
1370}
1371
1372#-------------------------------------------------------------------------------
1373
1374=item ipv6_to_bigint($) [PUBLIC,HYBRID]
1375
1376 Usage : my $result = Value::Convertor->ipv6_to_bigint($value);
1377 my $result = $convertor->ipv6_to_bigint($value);
1378 Purpose : Converts 'ipv6' adress in any representation to 'integer'
1379 Arguments : INTEGER $ipv6 - Value to be converted [MANDATORY]
1380 STRING $type - Type of the argument (bypass autodetection) [OPTIONAL]
1381 Returns : INTEGER $ipv6int
1382 Throws : Dies, if not given any conversion argument
1383
1384=cut
1385
1386sub ipv6_to_bigint($;$)
1387{
1388 my $invocant = shift;
1389 my ($ipv6, $type, @matches) = @_;
1390 confess "Expected conversion argument not given" unless defined $ipv6;
1391
1392 ($type, @matches) = $invocant->detect_ipv6($ipv6) unless $type;
1393 confess "Invalid argument '$ipv6' given" unless $type;
1394 confess "Do not know how to convert $type" unless exists IP_CONVERSIONS()->{$type}->{INT};
1395
1396 my $conversion = IP_CONVERSIONS()->{$type}->{INT};
1397 return ($type, $invocant->$conversion($ipv6)) if $conversion;
1398 return (UNKNOWN_TYPE(), $ipv6);
1399}
1400
1401=item ipv6_to_bin($) [PUBLIC,HYBRID]
1402
1403 Usage : my $result = Value::Convertor->ipv6_to_bin($value);
1404 my $result = $convertor->ipv6_to_bin($value);
1405 Purpose : Converts 'ipv6' adress in any representation to 'binary'
1406 Arguments : Same as ipv6_to_bigint() method
1407 Returns : BINARY $binary
1408 Throws : Dies, if not given any conversion argument
1409 See also : ipv6_to_bigint() method
1410
1411=cut
1412
1413sub ipv6_to_bin($;$)
1414{
1415 my $invocant = shift;
1416 my ($ipv6, $type, @matches) = @_;
1417 confess "Expected conversion argument not given" unless defined $ipv6;
1418
1419 ($type, @matches) = $invocant->detect_ipv6($ipv6) unless $type;
1420 confess "Invalid argument '$ipv6' given" unless $type;
1421 confess "Do not know how to convert $type" unless exists IP_CONVERSIONS()->{$type}->{BIN};
1422
1423 my $conversion = IP_CONVERSIONS()->{$type}->{BIN};
1424 return ($type, $invocant->$conversion($ipv6)) if $conversion;
1425 return (UNKNOWN_TYPE(), $ipv6);
1426}
1427
1428#-------------------------------------------------------------------------------
1429
1430=item ipv6cidr_to_bigints($;$) [PUBLIC,HYBRID]
1431
1432 Usage : my $result = Value::Convertor->ipv6cidr_to_bigints($value, $length);
1433 my $result = $convertor->ipv6cidr_to_bigints($value, $length);
1434 Purpose : Converts 'ipv6cidr string' to 'bigintegers'
1435 Arguments : STRING $ipv6cidr - Value to be converted [MANDATORY]
1436 Returns : BIGINT $min
1437 BIGINT $max
1438 Throws : Dies, if not given any conversion argument
1439
1440=cut
1441
1442sub ipv6cidr_to_bigints($)
1443{
1444 my $invocant = shift;
1445 my ($ipv6cidr) = @_;
1446 confess "Expected conversion argument not given" unless defined $ipv6cidr;
1447
1448 my $cidr = Net::CIDR::Lite->new;
1449 eval {
1450 $cidr->add($ipv6cidr);
1451 };
1452
1453 if ($@) {
1454 confess "Invalid network address given as argument: $@";
1455 }
1456
1457 my ($min_str, $max_str) = split (/-/, join ('', $cidr->list_range));
1458
1459 # And now calculate the ranges
1460 my $min = $invocant->ipv6str_to_bigint($min_str);
1461 my $max = $invocant->ipv6str_to_bigint($max_str);
1462
1463 return ($min, $max);
1464}
1465
1466=item ipv6cidr_to_bins($;$) [PUBLIC,HYBRID]
1467
1468 Usage : my $result = Value::Convertor->ipv6cidr_to_bins($value);
1469 my $result = $convertor->ipv6cidr_to_bins($value);
1470 Purpose : Converts 'ipv6cidr string' to 'binaries'
1471 Arguments : Same as ipv6cidr_to_bigints() method
1472 Returns : BINARY $min
1473 BINARY $max
1474 Throws : Dies, if not given any conversion argument
1475
1476=cut
1477
1478sub ipv6cidr_to_bins($)
1479{
1480 my $invocant = shift;
1481 my ($ipv6cidr) = @_;
1482 confess "Expected conversion argument not given" unless defined $ipv6cidr;
1483
1484 my $cidr = Net::CIDR::Lite->new;
1485 eval {
1486 $cidr->add($ipv6cidr);
1487 };
1488
1489 if ($@) {
1490 confess "Invalid network address given as argument: $@";
1491 }
1492
1493 my ($min_str, $max_str) = split (/-/, join ('', $cidr->list_range));
1494
1495 # And now calculate the ranges
1496 my $min = $invocant->ipv6str_to_bin($min_str);
1497 my $max = $invocant->ipv6str_to_bin($max_str);
1498
1499 return ($min, $max);
1500}
1501
1502#-------------------------------------------------------------------------------
1503
1504=item ip_to_int($;$) [PUBLIC,HYBRID]
1505
1506 Usage : my $result = Value::Convertor->ip_to_int($value);
1507 my $result = $convertor->ip_to_int($value);
1508 Purpose : Converts 'ipv4' adress in any representation to 'integer'
1509 Arguments : INTEGER $ip - Value to be converted [MANDATORY]
1510 STRING $type - Type of the argument (bypass autodetection) [OPTIONAL]
1511 Returns : INTEGER|GIGINT $ipint
1512 Throws : Dies, if not given any conversion argument
1513
1514=cut
1515
1516sub ip_to_int($)
1517{
1518 my $invocant = shift;
1519 my ($ip, $type, @matches) = @_;
1520 confess "Expected conversion argument not given" unless defined $ip;
1521
1522 ($type, @matches) = $invocant->detect_ip($ip) unless $type;
1523 confess "Invalid argument '$ip' given" unless $type;
1524 confess "Do not know how to convert $type" unless exists IP_CONVERSIONS()->{$type}->{INT};
1525
1526 my $conversion = IP_CONVERSIONS()->{$type}->{INT};
1527 return ($type, $invocant->$conversion(@matches)) if $conversion;
1528 return (UNKNOWN_TYPE(), $ip);
1529}
1530
1531=item ip_to_bin($) [PUBLIC,HYBRID]
1532
1533 Usage : my $result = Value::Convertor->ip_to_bin($value);
1534 my $result = $convertor->ip_to_bin($value);
1535 Purpose : Converts 'ip' adress in any representation to 'binary'
1536 Arguments : Same as ip_to_int() method
1537 Returns : BINARY $binary
1538 Throws : Dies, if not given any conversion argument
1539 See also : ip_to_int() method
1540
1541=cut
1542
1543sub ip_to_bin($)
1544{
1545 my $invocant = shift;
1546 my ($ip, $type, @matches) = @_;
1547 confess "Expected conversion argument not given" unless defined $ip;
1548
1549 ($type, @matches) = $invocant->detect_ip($ip) unless $type;
1550 confess "Invalid argument '$ip' given" unless $type;
1551 confess "Do not know how to convert $type" unless exists IP_CONVERSIONS()->{$type}->{BIN};
1552
1553 my $conversion = IP_CONVERSIONS()->{$type}->{BIN};
1554 return ($type, $invocant->$conversion(@matches)) if $conversion;
1555 return (UNKNOWN_TYPE(), $ip);
1556}
1557
1558#-------------------------------------------------------------------------------
1559# IP address anonymizations
1560#-------------------------------------------------------------------------------
1561
1562=item ipv4_anonymize($;$) [PUBLIC,HYBRID]
1563
1564 Usage : my $result = Value::Convertor->ipv4_anonymize($value);
1565 my $result = $convertor->ipv4_anonymize($value);
1566 Purpose : Anonymizes IPv4 address
1567 Arguments : STRING ipv4 - Value to be converted [MANDATORY]
1568 INTEGER $depth - How much to anonymize [OPTIONAL,DEFAULT=1]
1569 Returns : BINARY $binary
1570 Throws : Dies, if not given any conversion argument
1571 See also : ip_to_int() method
1572
1573=cut
1574
1575sub ipv4_anonymize($;$)
1576{
1577 my $invocant = shift;
1578 my ($ipv4, $depth) = @_;
1579 confess "Expected conversion argument not given" unless defined $ipv4;
1580
1581 my @chunks = split(/\./, $ipv4);
1582 $depth = 1 unless defined $depth;
1583 $depth = int($depth);
1584 $depth = 1 if ($depth < 0 or $depth > 4);
1585
1586 for (my $i = 0; $i < $depth; $i++) { pop(@chunks); }
1587 for (my $i = 0; $i < $depth; $i++) { push(@chunks, 0); }
1588
1589 return join('.', @chunks);
1590}
1591
1592#-------------------------------------------------------------------------------
1593# MAC conversions
1594#-------------------------------------------------------------------------------
1595
1596=item macstr_to_hexstr($) [PUBLIC,HYBRID]
1597
1598 Usage : my $result = Value::Convertor->macstr_to_hexstr($value);
1599 my $result = $convertor->macstr_to_hexstr($value);
1600 Purpose : Converts 'mac_string' to 'hexadecimal string'
1601 Arguments : STRING $macstr - Value to be converted [MANDATORY]
1602 Returns : STRING $hexstr
1603 Throws : Dies, if not given any conversion argument
1604
1605=cut
1606
1607sub macstr_to_hexstr($)
1608{
1609 my $invocant = shift;
1610 my ($macstr,) = @_;
1611 confess "Expected conversion argument not given" unless defined $macstr;
1612
1613 $macstr =~ s/://g;
1614 return uc($macstr);
1615}
1616
1617=item hexstr_to_macstr($) [PUBLIC,HYBRID]
1618
1619 Usage : my $result = Value::Convertor->hexstr_to_macstr($value);
1620 my $result = $convertor->hexstr_to_macstr($value);
1621 Purpose : Converts 'hexadecimal string' to 'mac_string'
1622 Arguments : STRING $hexstr - Value to be converted [MANDATORY]
1623 Returns : STRING $macstr
1624 Throws : Dies, if not given any conversion argument
1625
1626=cut
1627
1628sub hexstr_to_macstr($)
1629{
1630 my $invocant = shift;
1631 my ($machex,) = @_;
1632 confess "Expected conversion argument not given" unless defined $machex;
1633
1634 return join(':', unpack('(A2)*', $machex));
1635}
1636
1637#-------------------------------------------------------------------------------
1638
1639=item macstr_to_bin($) [PUBLIC,HYBRID]
1640
1641 Usage : my $result = Value::Convertor->macstr_to_bin($value);
1642 my $result = $convertor->macstr_to_bin($value);
1643 Purpose : Converts 'mac_string' to 'binary'
1644 Arguments : STRING $macstr - Value to be converted [MANDATORY]
1645 Returns : BINARY $macbin
1646 Throws : Dies, if not given any conversion argument
1647
1648=cut
1649
1650sub macstr_to_bin($)
1651{
1652 my $invocant = shift;
1653 my ($macstr,) = @_;
1654 confess "Expected conversion argument not given" unless defined $macstr;
1655
1656 $macstr =~ s/://g;
1657 return $invocant->hexstr_to_bin($macstr);
1658}
1659
1660=item bin_to_macstr($) [PUBLIC,HYBRID]
1661
1662 Usage : my $result = Value::Convertor->bin_to_macstr($value);
1663 my $result = $convertor->bin_to_macstr($value);
1664 Purpose : Converts 'binary' to 'mac_string'
1665 Arguments : BINARY $macbin - Value to be converted [MANDATORY]
1666 Returns : STRING $macstr
1667 Throws : Dies, if not given any conversion argument
1668
1669=cut
1670
1671sub bin_to_macstr($)
1672{
1673 my $invocant = shift;
1674 my ($macbin,) = @_;
1675 confess "Expected conversion argument not given" unless defined $macbin;
1676
1677 return $invocant->hexstr_to_macstr($invocant->bin_to_hexstr($macbin));
1678}
1679
1680#-------------------------------------------------------------------------------
1681
1682=item macstr_to_bigint($) [PUBLIC,HYBRID]
1683
1684 Usage : my $result = Value::Convertor->macstr_to_bigint($value);
1685 my $result = $convertor->macstr_to_bigint($value);
1686 Purpose : Converts 'macstr' to 'bigint'
1687 Arguments : STRING $macstr - Value to be converted [MANDATORY]
1688 Returns : BIGINT $bigint
1689 Throws : Dies, if not given any conversion argument
1690
1691=cut
1692
1693sub macstr_to_bigint($)
1694{
1695 my $invocant = shift;
1696 my ($macstr,) = @_;
1697 confess "Expected conversion argument not given" unless defined $macstr;
1698
1699 $macstr =~ s/://g;
1700
1701 return $invocant->hexstr_to_bigint($macstr);
1702}
1703
1704=item bigint_to_macstr($) [PUBLIC,HYBRID]
1705
1706 Usage : my $result = Value::Convertor->bigint_to_macstr($value);
1707 my $result = $convertor->bigint_to_macstr($value);
1708 Purpose : Converts 'bigint' to 'macstr'
1709 Arguments : BIGINT $bigint - Value to be converted [MANDATORY]
1710 Returns : STRING $macstr
1711 Throws : Dies, if not given any conversion argument
1712
1713=cut
1714
1715sub bigint_to_macstr($)
1716{
1717 my $invocant = shift;
1718 my ($bigint,) = @_;
1719 confess "Expected conversion argument not given" unless defined $bigint;
1720
1721220µs21.30ms
# spent 658µs (14+644) within Value::Convertor::BEGIN@1721 which was called: # once (14µs+644µs) by main::BEGIN@7 at line 1721
use bigint;
# spent 658µs making 1 call to Value::Convertor::BEGIN@1721 # spent 644µs making 1 call to bigint::import
1722178µs148µs return $invocant->hexstr_to_macstr($invocant->bigint_to_hexstr($bigint,12));
# spent 48µs making 1 call to Math::BigInt::__ANON__[Math/BigInt.pm:2702]
1723}
1724
1725#-------------------------------------------------------------------------------
1726# Time and date conversions
1727#-------------------------------------------------------------------------------
1728
1729=item ntps_to_nbi($) [PUBLIC,HYBRID]
1730
1731 Usage : my $result = Value::Convertor->ntps_to_nbi($value);
1732 my $result = $convertor->ntps_to_nbi($value);
1733 Purpose : Converts 'ntp_stamp' to 'bigint'
1734 Arguments : STRING $ntpstamp - Value to be converted [MANDATORY]
1735 Returns : BIGINT $bigint
1736 Throws : Dies, if not given any conversion argument
1737
1738=cut
1739
1740sub ntps_to_nbi($)
1741{
1742 my $invocant = shift;
1743 my ($ntpstamp,) = @_;
1744 confess "Expected conversion argument not given" unless defined $ntpstamp;
1745
1746256µs2626µs
# spent 317µs (7+310) within Value::Convertor::BEGIN@1746 which was called: # once (7µs+310µs) by main::BEGIN@7 at line 1746
use bigint;
# spent 317µs making 1 call to Value::Convertor::BEGIN@1746 # spent 310µs making 1 call to bigint::import
1747
1748 my $part = qr/[0-9a-fA-F]+/;
1749 my ($high, $low) = $ntpstamp =~ /^0x($part)\.0x($part)$/;
1750
1751188µs149µs my $bigint = hex("0x$low") + (hex("0x$high") << 32);
# spent 49µs making 1 call to Math::BigInt::__ANON__[Math/BigInt.pm:2702]
1752 $bigint =~ /^[0-9]+$/;
1753
1754 return $bigint;
1755}
1756
1757=item nbi_to_ntps($) [PUBLIC,HYBRID]
1758
1759 Usage : my $result = Value::Convertor->nbi_to_ntps($value);
1760 my $result = $convertor->nbi_to_ntps($value);
1761 Purpose : Converts 'bigint' to 'ntp_stamp'
1762 Arguments : BIGINT $bigint - Value to be converted [MANDATORY]
1763 Returns : STRING $ntps
1764 Throws : Dies, if not given any conversion argument
1765
1766=cut
1767
1768sub nbi_to_ntps($)
1769{
1770 my $invocant = shift;
1771 my ($bigint,) = @_;
1772 confess "Expected conversion argument not given" unless defined $bigint;
1773
1774217µs2634µs
# spent 321µs (7+314) within Value::Convertor::BEGIN@1774 which was called: # once (7µs+314µs) by main::BEGIN@7 at line 1774
use bigint;
# spent 321µs making 1 call to Value::Convertor::BEGIN@1774 # spent 314µs making 1 call to bigint::import
1775
1776111µs146µs my $s = ($bigint >> 32);
# spent 46µs making 1 call to Math::BigInt::__ANON__[Math/BigInt.pm:2702]
1777171µs122µs my $m = $bigint - ($s <<32);
# spent 22µs making 1 call to Math::BigInt::__ANON__[Math/BigInt.pm:2702]
1778
1779 return lc(sprintf("0x%x.0x%x", $s, $m));
1780}
1781
1782#-------------------------------------------------------------------------------
1783
1784=item unixs_to_ubi($) [PUBLIC,HYBRID]
1785
1786 Usage : my $result = Value::Convertor->unixs_to_ubi($value);
1787 my $result = $convertor->unixs_to_ubi($value);
1788 Purpose : Converts 'unix_stamp' to 'bigint'
1789 Arguments : INTEGER|FLOAT $unixstamp - Value to be converted [MANDATORY]
1790 Returns : BIGINT $bigint
1791 Throws : Dies, if not given any conversion argument
1792
1793=cut
1794
1795sub unixs_to_ubi($)
1796{
1797 my $invocant = shift;
1798 my ($unixstamp,) = @_;
1799 confess "Expected conversion argument not given" unless defined $unixstamp;
1800
1801233µs2714µs
# spent 360µs (7+353) within Value::Convertor::BEGIN@1801 which was called: # once (7µs+353µs) by main::BEGIN@7 at line 1801
use bigint;
# spent 360µs making 1 call to Value::Convertor::BEGIN@1801 # spent 353µs making 1 call to bigint::import
1802
1803 my $result = $invocant->unixs_to_ints($unixstamp);
1804 my ($high, $low) = @$result;
1805
18061623µs146µs my $bigint = int($low) + (int($high) << 32);
# spent 46µs making 1 call to Math::BigInt::__ANON__[Math/BigInt.pm:2702]
1807 $bigint =~ /^[0-9]+$/;
1808
1809 return $bigint;
1810}
1811
1812=item ubi_to_unixs($) [PUBLIC,HYBRID]
1813
1814 Usage : my $result = Value::Convertor->ubi_to_unixs($value);
1815 my $result = $convertor->ubi_to_unixs($value);
1816 Purpose : Converts 'bigint' to 'unix_stamp'
1817 Arguments : BIGINT $bigint - Value to be converted [MANDATORY]
1818 Returns : INTEGER|FLOAT $unixstamp
1819 Throws : Dies, if not given any conversion argument
1820
1821=cut
1822
1823sub ubi_to_unixs($)
1824{
1825 my $invocant = shift;
1826 my ($bigint,) = @_;
1827 confess "Expected conversion argument not given" unless defined $bigint;
1828
1829 my $s = ($bigint >> 32);
1830 my $m = $bigint - ($s << 32);
1831
1832 return $invocant->ints_to_unixs([$s,$m]);
1833}
1834
1835#-------------------------------------------------------------------------------
1836
1837=item unixs_to_nbi($) [PUBLIC,HYBRID]
1838
1839 Usage : my $result = Value::Convertor->unixs_to_nbi($value);
1840 my $result = $convertor->unixs_to_nbi($value);
1841 Purpose : Converts 'unix_stamp' to 'ntp bigint'
1842 Arguments : INTEGER|FLOAT $unixstamp - Value to be converted [MANDATORY]
1843 Returns : BIGINT $bigint
1844 Throws : Dies, if not given any conversion argument
1845
1846=cut
1847
1848sub unixs_to_nbi($)
1849{
1850 my $invocant = shift;
1851 my ($unixstamp,) = @_;
1852 confess "Expected conversion argument not given" unless defined $unixstamp;
1853
1854 $unixstamp = $invocant->unixs_to_ubi($unixstamp);
1855 $unixstamp = $invocant->unixe_to_ntpe_b($unixstamp);
1856
1857 return $unixstamp;
1858}
1859
1860=item nbi_to_unixs($) [PUBLIC,HYBRID]
1861
1862 Usage : my $result = Value::Convertor->nbi_to_unixs($value);
1863 my $result = $convertor->nbi_to_unixs($value);
1864 Purpose : Converts 'bigint' to 'unix_stamp'
1865 Arguments : BIGINT $bigint - Value to be converted [MANDATORY]
1866 Returns : INTEGER|FLOAT $unixstamp
1867 Throws : Dies, if not given any conversion argument
1868
1869=cut
1870
1871sub nbi_to_unixs($)
1872{
1873 my $invocant = shift;
1874 my ($bigint,) = @_;
1875 confess "Expected conversion argument not given" unless defined $bigint;
1876
1877 $bigint = $invocant->ntpe_to_unixe_b($bigint);
1878 $bigint = $invocant->ubi_to_unixs($bigint);
1879
1880 return $bigint;
1881}
1882
1883#-------------------------------------------------------------------------------
1884
1885=item ntps_to_ints($) [PUBLIC,HYBRID]
1886
1887 Usage : my $result = Value::Convertor->ntps_to_ints($value);
1888 my $result = $convertor->ntps_to_ints($value);
1889 Purpose : Converts 'ntp_stamp' to 'integers'
1890 Arguments : STRING $ntpstamp - Value to be converted [MANDATORY]
1891 Returns : ARRAY REFERENCE CONTAINING
1892 INTEGER $seconds
1893 INTEGER $fractions
1894 Throws : Dies, if not given any conversion argument
1895
1896=cut
1897
1898sub ntps_to_ints($)
1899{
1900 my $invocant = shift;
1901 my ($ntpstamp,) = @_;
1902 confess "Expected conversion argument not given" unless defined $ntpstamp;
1903
1904 my $part = qr/[0-9a-fA-F]{1,8}/;
1905 my ($high, $low) = $ntpstamp =~ /^0x($part)\.0x($part)$/;
1906
1907 return [hex("0x$high"), hex("0x$low")];
1908}
1909
1910=item ints_to_ntps($) [PUBLIC,HYBRID]
1911
1912 Usage : my $result = Value::Convertor->ints_to_ntps($value);
1913 my $result = $convertor->ints_to_ntps($value);
1914 Purpose : Converts 'integers' to 'ntp_stamp'
1915 Arguments : ARRAY REFERENCE CONTAINING [MANDATORY]
1916 INTEGER $seconds
1917 INTEGER $fractions
1918 Returns : STRING $ntpstamp
1919 Throws : Dies, if not given any conversion argument
1920
1921=cut
1922
1923sub ints_to_ntps($)
1924{
1925 my $invocant = shift;
1926 my ($ints) = @_;
1927 confess "Expected conversion argument not given" unless defined $ints;
1928 confess "Conversion argument must be given as ARRAY reference" unless ref $ints eq 'ARRAY';
1929
1930 $ints->[1] = 0 unless $ints->[1];
1931
1932 return sprintf("0x%x.0x%x", $ints->[0], $ints->[1]);
1933}
1934
1935#-------------------------------------------------------------------------------
1936
1937=item unixs_to_ints($) [PUBLIC,HYBRID]
1938
1939 Usage : my $result = Value::Convertor->unixs_to_ints($value);
1940 my $result = $convertor->unixs_to_ints($value);
1941 Purpose : Converts 'unix_stamp' to 'integers'
1942 Arguments : INTEGER|FLOAT $unixstamp - Value to be converted [MANDATORY]
1943 Returns : ARRAY REFERENCE CONTAINING
1944 INTEGER $seconds
1945 INTEGER $fractions
1946 Throws : Dies, if not given any conversion argument
1947
1948=cut
1949
1950sub unixs_to_ints($)
1951{
1952 my $invocant = shift;
1953 my ($unixstamp,) = @_;
1954 confess "Expected conversion argument not given" unless defined $unixstamp;
1955
1956 my $part = qr/[0-9]+/;
1957 my ($high, $low) = $unixstamp =~ /^($part)(?:\.($part))?$/;
1958 $low = 0 unless $low;
1959
1960 return [$high, $low];
1961}
1962
1963=item ints_to_unixs($) [PUBLIC,HYBRID]
1964
1965 Usage : my $result = Value::Convertor->ints_to_unixs($value);
1966 my $result = $convertor->ints_to_unixs($value);
1967 Purpose : Converts 'integers' to 'unix_stamp'
1968 Arguments : ARRAY REFERENCE CONTAINING [MANDATORY]
1969 INTEGER $seconds
1970 INTEGER $fractions
1971 Returns : INTEGER|FLOAT $unixstamp
1972 Throws : Dies, if not given any conversion argument
1973
1974=cut
1975
1976sub ints_to_unixs($)
1977{
1978 my $invocant = shift;
1979 my ($ints) = @_;
1980 confess "Expected conversion argument not given" unless defined $ints;
1981 confess "Conversion argument must be given as ARRAY reference" unless ref $ints eq 'ARRAY';
1982
1983 $ints->[1] = 0 unless $ints->[1];
1984 my ($s, $m) = @$ints;
1985
1986 return "$s.0" + "0.$m";
1987}
1988
1989#-------------------------------------------------------------------------------
1990
1991=item ntpe_to_unixe($) [PUBLIC,HYBRID]
1992
1993 Usage : my $result = Value::Convertor->ntpe_to_unixe($value);
1994 my $result = $convertor->ntpe_to_unixe($value);
1995 Purpose : Converts 'ntp epoch' to 'unix epoch' (1900 -> 1970)
1996 Arguments : INTEGER|FLOAT $ntpepoch - Value to be converted [MANDATORY]
1997 Returns : INTEGER|FLOAT $unixepoch
1998 Throws : Dies, if not given any conversion argument
1999
2000=cut
2001
2002sub ntpe_to_unixe($)
2003{
2004 my $invocant = shift;
2005 my ($ntpepoch,) = @_;
2006 confess "Expected conversion argument not given" unless defined $ntpepoch;
2007
2008 return $ntpepoch - NTP_TO_EPOCH_DELTA;
2009}
2010
2011=item unixe_to_ntpe($) [PUBLIC,HYBRID]
2012
2013 Usage : my $result = Value::Convertor->($value);
2014 my $result = $convertor->($value);
2015 Purpose : Converts 'unix epoch' to 'ntp epoch' (1970 -> 1900)
2016 Arguments : INTEGER|FLOAT $unixepoch - Value to be converted [MANDATORY]
2017 Returns : INTEGER|FLOAT $ntpepoc
2018 Throws : Dies, if not given any conversion argument
2019
2020=cut
2021
2022sub unixe_to_ntpe($)
2023{
2024 my $invocant = shift;
2025 my ($unixepoch,) = @_;
2026 confess "Expected conversion argument not given" unless defined $unixepoch;
2027
2028 return $unixepoch + NTP_TO_EPOCH_DELTA;
2029}
2030
2031#-------------------------------------------------------------------------------
2032
2033=item ntpe_to_unixe_b($) [PUBLIC,HYBRID]
2034
2035 Usage : my $result = Value::Convertor->ntpe_to_unixe_b($value);
2036 my $result = $convertor->ntpe_to_unixe_b($value);
2037 Purpose : Converts 'ntp epoch' to 'unix epoch' in bigint (1900 -> 1970)
2038 Arguments : BIGINT $ntpepoch - Value to be converted [MANDATORY]
2039 Returns : BIGINT $unixepoch
2040 Throws : Dies, if not given any conversion argument
2041
2042=cut
2043
2044sub ntpe_to_unixe_b($)
2045{
2046 my $invocant = shift;
2047240µs2616µs
# spent 312µs (8+304) within Value::Convertor::BEGIN@2047 which was called: # once (8µs+304µs) by main::BEGIN@7 at line 2047
use bigint;
# spent 312µs making 1 call to Value::Convertor::BEGIN@2047 # spent 304µs making 1 call to bigint::import
2048 my ($ntpepoch,) = @_;
2049 confess "Expected conversion argument not given" unless defined $ntpepoch;
2050
2051157µs2217µs return $ntpepoch - (NTP_TO_EPOCH_DELTA() << 32);
# spent 170µs making 1 call to Math::BigInt::__ANON__[Math/BigInt.pm:121] # spent 48µs making 1 call to Math::BigInt::__ANON__[Math/BigInt.pm:2702]
2052}
2053
2054=item unixe_to_ntpe_b($) [PUBLIC,HYBRID]
2055
2056 Usage : my $result = Value::Convertor->unixe_to_ntpe_b($value);
2057 my $result = $convertor->unixe_to_ntpe_b($value);
2058 Purpose : Converts 'unix epoch' to 'ntp epoch' in bigint (1970 -> 1900)
2059 Arguments : BIGINT $unixepoch - Value to be converted [MANDATORY]
2060 Returns : BIGINT $ntpepoch
2061 Throws : Dies, if not given any conversion argument
2062
2063=cut
2064
2065sub unixe_to_ntpe_b($)
2066{
2067 my $invocant = shift;
2068242µs2642µs
# spent 324µs (7+317) within Value::Convertor::BEGIN@2068 which was called: # once (7µs+317µs) by main::BEGIN@7 at line 2068
use bigint;
# spent 324µs making 1 call to Value::Convertor::BEGIN@2068 # spent 317µs making 1 call to bigint::import
2069 my ($unixepoch,) = @_;
2070 confess "Expected conversion argument not given" unless defined $unixepoch;
2071
207211.66ms2169µs return $unixepoch + (NTP_TO_EPOCH_DELTA() << 32);
# spent 121µs making 1 call to Math::BigInt::__ANON__[Math/BigInt.pm:121] # spent 48µs making 1 call to Math::BigInt::__ANON__[Math/BigInt.pm:2702]
2073}
2074
2075#-------------------------------------------------------------------------------
2076
2077=item local_to_utc($) [PUBLIC,HYBRID]
2078
2079 Usage : my $result = Value::Convertor->local_to_utc($value);
2080 my $result = $convertor->local_to_utc($value);
2081 Purpose : Converts 'local' unix timestamp to 'utc/gmt' unix timestamp
2082 Arguments : INTEGER|FLOAT $unixstamp - Value to be converted [MANDATORY]
2083 Returns : INTEGER|FLOAT $unixstamp
2084 Throws : Dies, if not given any conversion argument
2085
2086=cut
2087
2088sub local_to_utc($)
2089{
2090 my $invocant = shift;
2091 my ($unixstamp,) = @_;
2092 confess "Expected conversion argument not given" unless defined $unixstamp;
2093
2094 return $unixstamp - $TIME_ZONE_OFFSET;
2095}
2096
2097=item utc_to_local($) [PUBLIC,HYBRID]
2098
2099 Usage : my $result = Value::Convertor->utc_to_local($value);
2100 my $result = $convertor->utc_to_local($value);
2101 Purpose : Converts 'utc/gmt' unix timestamp to 'local' unix timestamp
2102 Arguments : INTEGER|FLOAT $unixstamp - Value to be converted [MANDATORY]
2103 Returns : INTEGER|FLOAT $unixstamp
2104 Throws : Dies, if not given any conversion argument
2105
2106=cut
2107
2108sub utc_to_local($)
2109{
2110 my $invocant = shift;
2111 my ($unixstamp,) = @_;
2112 confess "Expected conversion argument not given" unless defined $unixstamp;
2113
2114 return $unixstamp + $TIME_ZONE_OFFSET;
2115}
2116
2117#-------------------------------------------------------------------------------
2118
2119=item datestr_to_unixs($) [PUBLIC,HYBRID]
2120
2121 Usage : my $result = Value::Convertor->datestr_to_unixs($value);
2122 my $result = $convertor->datestr_to_unixs($value);
2123 Purpose : Converts 'date string' to 'integer' timestamp
2124 Arguments : STRING $datestr - Value to be converted [MANDATORY]
2125 Returns : INTEGER $timestamp
2126 Throws : Dies, if not given any conversion argument
2127
2128=cut
2129
2130sub datestr_to_unixs($)
2131{
2132 my $invocant = shift;
2133 my ($datestr,) = @_;
2134 confess "Expected conversion argument not given" unless defined $datestr;
2135
2136 return str2time($datestr);
2137}
2138
2139=item unixs_to_datestr($;$) [PUBLIC,HYBRID]
2140
2141 Usage : my $result = Value::Convertor->unixs_to_datestr($value, $print_in_local);
2142 my $result = $convertor->unixs_to_datestr($value, $print_in_local);
2143 Purpose : Converts 'unix_stamp' to 'date string' formated in ISO 8601
2144 Arguments : INTEGER|FLOAT $unixstamp - Value to be converted [MANDATORY]
2145 BOOL $print_in_local - display the timestamp in local time, not UTC/GMT
2146 Returns : STRING $datestr
2147 Throws : Dies, if not given any conversion argument
2148
2149=cut
2150
2151sub unixs_to_datestr($;$)
2152{
2153 my $invocant = shift;
2154 my ($unixstamp, $print_in_local) = @_;
2155 confess "Expected conversion argument not given" unless defined $unixstamp;
2156
2157 # Assume all timestamps to be in UTC/GMT
2158 my $dt = DateTime->from_epoch(epoch => $unixstamp, time_zone => 'UTC');
2159
2160 # Print time in local time zone?
2161 unless ($print_in_local)
2162 {
2163 $dt->set_time_zone('UTC');
2164 return $dt->strftime('%F %TZ');
2165 }
2166 else {
2167 $dt->set_time_zone($TIME_ZONE);
2168 #return $dt->strftime('%F %T%z');
2169 return $dt->strftime('%F %T');
2170 }
2171}
2172
2173=item unixs_to_ldatestr($) [PUBLIC,HYBRID]
2174
2175 Usage : my $result = Value::Convertor->unixs_to_ldatestr($value);
2176 my $result = $convertor->unixs_to_ldatestr($value);
2177 Purpose : Converts 'unix_stamp' to 'date string' formated in local time, not UTC/GMT
2178 Arguments : INTEGER|FLOAT $unixstamp - Value to be converted [MANDATORY]
2179 Returns : STRING $datestr
2180 Throws : Dies, if not given any conversion argument
2181
2182=cut
2183
2184sub unixs_to_ldatestr($)
2185{
2186 my $invocant = shift;
2187 my ($unixstamp) = @_;
2188 confess "Expected conversion argument not given" unless defined $unixstamp;
2189
2190 return $invocant->unixs_to_datestr($unixstamp, 1);
2191}
2192
2193#-------------------------------------------------------------------------------
2194
2195=item unixs_to_datetime($;$) [PUBLIC,HYBRID]
2196
2197 Usage : my $result = Value::Convertor->unixs_to_datetime($value, $print_in_local);
2198 my $result = $convertor->unixs_to_datestr($value, $print_in_local);
2199 Purpose : Converts 'unix_stamp' to 'DateTime'
2200 Arguments : INTEGER|FLOAT $unixstamp - Value to be converted [MANDATORY]
2201 BOOL $print_in_local - display the timestamp in local time, not UTC/GMT
2202 Returns : DateTime
2203 Throws : Dies, if not given any conversion argument
2204
2205=cut
2206
2207sub unixs_to_datetime($;$)
2208{
2209 my $invocant = shift;
2210 my ($unixstamp, $print_in_local) = @_;
2211 confess "Expected conversion argument not given" unless defined $unixstamp;
2212
2213 # Assume all timestamps to be in UTC/GMT
2214 my $dt = DateTime->from_epoch(epoch => $unixstamp, time_zone => 'UTC');
2215
2216 # Print time in local time zone?
2217 unless ($print_in_local)
2218 {
2219 $dt->set_time_zone('UTC');
2220 return $dt;
2221 }
2222 else
2223 {
2224 $dt->set_time_zone($TIME_ZONE);
2225 return $dt;
2226 }
2227}
2228
2229#-------------------------------------------------------------------------------
2230
2231=item ntps_to_datestr($;$) [PUBLIC,HYBRID]
2232
2233 Usage : my $result = Value::Convertor->ntps_to_datestr($value, $print_in_local);
2234 my $result = $convertor->ntps_to_datestr($value, $print_in_local);
2235 Purpose : Converts 'ntp_stamp' to 'date string' formated in ISO 8601
2236 Arguments : STRING $ntpstamp - Value to be converted [MANDATORY]
2237 BOOL $print_in_local - display the timestamp in local time, not UTC/GMT
2238 Returns : STRING $datestr
2239 Throws : Dies, if not given any conversion argument
2240
2241=cut
2242
2243sub ntps_to_datestr($;$)
2244{
2245 my $invocant = shift;
2246 my ($ntpstamp, $print_in_local) = @_;
2247 confess "Expected conversion argument not given" unless defined $ntpstamp;
2248
2249 # Convert the ntpstamp to bigint representation
2250 $ntpstamp = $invocant->ntps_to_nbi($ntpstamp);
2251 # Change epoch from 1900 to 1970
2252 $ntpstamp = $invocant->ntpe_to_unixe_b($ntpstamp);
2253 # And format the result
2254 return $invocant->ubi_to_datestr($ntpstamp, $print_in_local);
2255}
2256
2257=item ntps_to_ldatestr($;$) [PUBLIC,HYBRID]
2258
2259 Usage : my $result = Value::Convertor->ntps_to_ldatestr($value);
2260 my $result = $convertor->ntps_to_ldatestr($value);
2261 Purpose : Converts 'ntp_stamp' to 'date string' formated in ISO 8601
2262 Arguments : STRING $ntpstamp - Value to be converted [MANDATORY]
2263 Returns : STRING $datestr
2264 Throws : Dies, if not given any conversion argument
2265
2266=cut
2267
2268sub ntps_to_ldatestr($;$)
2269{
2270 my $invocant = shift;
2271 my ($ntpstamp, ) = @_;
2272 confess "Expected conversion argument not given" unless defined $ntpstamp;
2273
2274 # Convert the ntpstamp to bigint representation
2275 $ntpstamp = $invocant->ntps_to_nbi($ntpstamp);
2276 # Change epoch from 1900 to 1970
2277 $ntpstamp = $invocant->ntpe_to_unixe_b($ntpstamp);
2278 # And format the result
2279 return $invocant->ubi_to_datestr($ntpstamp, 1);
2280}
2281
2282#-------------------------------------------------------------------------------
2283
2284=item datestr_to_ubi($) [PUBLIC,HYBRID]
2285
2286 Usage : my $result = Value::Convertor->datestr_to_ubi($value);
2287 my $result = $convertor->datestr_to_ubi($value);
2288 Purpose : Converts 'date string' to 'unix bigint' timestamp
2289 Arguments : STRING $datestr - Value to be converted [MANDATORY]
2290 Returns : BIGINT $bigint
2291 Throws : Dies, if not given any conversion argument
2292
2293=cut
2294
2295sub datestr_to_ubi($)
2296{
2297 my $invocant = shift;
2298 my ($datestr,) = @_;
2299 confess "Expected conversion argument not given" unless defined $datestr;
2300
2301 # Parse the time string and get the epoch timestamp
2302 my $epoch = str2time($datestr);
2303 # Convert epoch to bigint
2304 return $invocant->unixs_to_ubi($epoch);
2305}
2306
2307=item ubi_to_datestr($;$) [PUBLIC,HYBRID]
2308
2309 Usage : my $result = Value::Convertor->ubi_to_datestr($value, $print_in_local);
2310 my $result = $convertor->ubi_to_datestr($value, $print_in_local);
2311 Purpose : Converts 'unix bigint' timestamp to 'date string'
2312 Arguments : BIGINT $bigint - Value to be converted [MANDATORY]
2313 Returns : STRING $datestr
2314 Throws : Dies, if not given any conversion argument
2315
2316=cut
2317
2318sub ubi_to_datestr($;$)
2319{
2320 my $invocant = shift;
2321 my ($unixstamp, $print_in_local) = @_;
2322 confess "Expected conversion argument not given" unless defined $unixstamp;
2323
2324 # Separate seconds and fractions from bigint timestamp
2325 my $s = ($unixstamp >> 32);
2326 my $m = $unixstamp - ($s << 32);
2327
2328 return $invocant->unixs_to_datestr("$s.$m", $print_in_local);
2329}
2330
2331#-------------------------------------------------------------------------------
2332
2333=item datestr_to_nbi($) [PUBLIC,HYBRID]
2334
2335 Usage : my $result = Value::Convertor->datestr_to_nbi($value);
2336 my $result = $convertor->datestr_to_nbi($value);
2337 Purpose : Converts 'date string' to 'ntp bigint' timestamp
2338 Arguments : STRING $datestr - Value to be converted [MANDATORY]
2339 Returns : BIGINT $bigint
2340 Throws : Dies, if not given any conversion argument
2341
2342=cut
2343
2344sub datestr_to_nbi($)
2345{
2346 my $invocant = shift;
2347 my ($datestr,) = @_;
2348 confess "Expected conversion argument not given" unless defined $datestr;
2349
2350 # Parse the time string and get the epoch timestamp
2351 my $epoch = str2time($datestr);
2352 # Convert epoch from unix to NTP (1970 -> 1900)
2353 $epoch = $invocant->unixe_to_ntpe($epoch);
2354 # Convert epoch to bigint
2355 return $invocant->unixs_to_ubi($epoch);
2356}
2357
2358=item nbi_to_datestr($;$) [PUBLIC,HYBRID]
2359
2360 Usage : my $result = Value::Convertor->nbi_to_datestr($value, $print_in_local);
2361 my $result = $convertor->nbi_to_datestr($value, $print_in_local);
2362 Purpose : Converts 'ntp bigint' timestamp to 'date string'
2363 Arguments : BIGINT $bigint - Value to be converted [MANDATORY]
2364 Returns : STRING $datestr
2365 Throws : Dies, if not given any conversion argument
2366
2367=cut
2368
2369sub nbi_to_datestr($;$)
2370{
2371 my $invocant = shift;
2372 my ($bigint, $print_in_local) = @_;
2373 confess "Expected conversion argument not given" unless defined $bigint;
2374
2375 # Convert epoch from NTP to unix (1900 -> 1970)
2376 $bigint = $invocant->ntpe_to_unixe_b($bigint);
2377 return $invocant->ubi_to_datestr($bigint, $print_in_local);
2378}
2379
2380#-------------------------------------------------------------------------------
2381
2382=item periodstr_to_nbis($;$) [PUBLIC,HYBRID]
2383
2384 Usage : my $result = Value::Convertor->periodstr_to_nbis($value);
2385 my $result = $convertor->periodstr_to_nbis($value);
2386 Purpose : Converts 'period string' to 'ntp bigints' timestamp
2387 Arguments : STRING $datestr - Value to be converted [MANDATORY]
2388 STRING $second - Optional second argument [OPTIONAL]
2389 Returns : BIGINT $bigint
2390 Throws : Dies, if not given any conversion argument
2391
2392=cut
2393
2394sub periodstr_to_nbis($;$)
2395{
2396 my $invocant = shift;
2397 my ($datestr,$second) = @_;
2398 confess "Expected conversion argument not given" unless defined $datestr;
2399
2400 unless ($second)
2401 {
2402 my $ptrn = $TS_QRS{TS_PERIOD()};
2403 my @matches = ($datestr =~ /$ptrn/);
2404 ($datestr,$second) = @matches if @matches;
2405 }
2406 confess "Invalid period start time given as argument" unless defined $datestr;
2407 confess "Invalid period end time given as argument" unless defined $second;
2408
2409 # Convert the IP address to integer
2410 my $min = $invocant->datestr_to_nbi($datestr);
2411 my $max = $invocant->datestr_to_nbi($second);
2412
2413 return ($min, $max);
2414}
2415
2416=item nbis_to_periodstr($$;$) [PUBLIC,HYBRID]
2417
2418 Usage : my $result = Value::Convertor->nbis_to_periodstr($min, $max, $print_in_local);
2419 my $result = $convertor->nbis_to_periodstr($min, $max, $print_in_local);
2420 Purpose : Converts 'ntp bigint' timestamp to 'date string'
2421 Arguments : BIGINT $min - Value to be converted [MANDATORY]
2422 BIGINT $max - Value to be converted [MANDATORY]
2423 Returns : STRING $datestr
2424 Throws : Dies, if not given any conversion argument
2425
2426=cut
2427
2428sub nbis_to_periodstr($$;$)
2429{
2430 my $invocant = shift;
2431 my ($min, $max, $print_in_local) = @_;
2432 confess "Expected conversion arguments not given" unless defined $min and defined $max;
2433
2434 # Convert epoch from NTP to unix (1900 -> 1970)
2435 $min = $invocant->ntpe_to_unixe_b($min);
2436 $max = $invocant->ntpe_to_unixe_b($max);
2437
2438 return $invocant->ubi_to_datestr($min, $print_in_local) . '..' . $invocant->ubi_to_datestr($max, $print_in_local);
2439}
2440
2441#-------------------------------------------------------------------------------
2442
2443=item detect_datetime($) [PUBLIC,HYBRID]
2444
2445 Usage : my $result = Value::Convertor->detect_datetime($value);
2446 my $result = $convertor->detect_datetime($value);
2447 Purpose : Detect type of date/timestamp
2448 Arguments : STRING $timestamp - Value to be converted [MANDATORY]
2449 Returns : STRING $type
2450 Throws : Dies, if not given any conversion argument
2451
2452=cut
2453
2454sub detect_datetime($)
2455{
2456 my $invocant = shift;
2457 my ($timestamp,) = @_;
2458 confess "Expected conversion argument not given" unless defined $timestamp;
2459
2460 my ($type, $ptrn, @matches);
2461 foreach my $t (@$QR_TS)
2462 {
2463 ($type, $ptrn) = @$t;
2464 @matches = ($timestamp =~ /$ptrn/);
2465 return ($type, @matches) if @matches;
2466 }
2467 return undef;
2468}
2469
2470#-------------------------------------------------------------------------------
2471
2472=item datetime_to_nbi($) [PUBLIC,HYBRID]
2473
2474 Usage : my $result = Value::Convertor->datetime_to_nbii($value);
2475 my $result = $convertor->datetime_to_nbi($value);
2476 Purpose : Converts any 'datetime' to 'ntp bigint' timestamp
2477 Arguments : STRING $datestr - Value to be converted [MANDATORY]
2478 Returns : BIGINT $bigint
2479 Throws : Dies, if not given any conversion argument
2480
2481=cut
2482
2483sub datetime_to_nbi($)
2484{
2485 my $invocant = shift;
2486 my ($timestamp, $type, @matches) = @_;
2487 confess "Expected conversion argument not given" unless defined $timestamp;
2488
2489 ($type, @matches) = $invocant->detect_datetime($timestamp) unless $type;
2490 confess "Invalid argument '$timestamp' given" unless $type;
2491 confess "Do not know how to convert $type" unless exists TS_CONVERSIONS()->{$type};
2492
2493 my $conversion = TS_CONVERSIONS()->{$type};
2494
2495 return ($type, $invocant->$conversion(@matches)) if $conversion;
2496 return (UNKNOWN_TYPE(), $timestamp);
2497}
2498
2499#-------------------------------------------------------------------------------
2500
2501=item duration_to_string($) [PUBLIC,HYBRID]
2502
2503 Usage : my $result = Value::Convertor->duration_to_string($value);
2504 my $result = $convertor->duration_to_string($value);
2505 Purpose : Converts numeric 'duration' to 'string'
2506 Arguments : INTEGER $duration - Value to be converted [MANDATORY]
2507 Returns : STRING $duration_str
2508 Throws : Dies, if not given any conversion argument
2509
2510=cut
2511
2512sub duration_to_string($)
2513{
2514 my $invocant = shift;
2515 my ($duration,) = @_;
2516 confess "Expected conversion argument not given" unless defined $duration;
2517
2518 my @chunks = (); my $tmp;
2519 $tmp = int($duration / 604800);
2520 if ($tmp) { $duration %= 604800; push(@chunks, sprintf('%dw',$tmp)); }
2521 $tmp = int($duration / 86400);
2522 if ($tmp) { $duration %= 86400; push(@chunks, sprintf('%dd',$tmp)); }
2523 $tmp = int($duration / 3600);
2524 if ($tmp) { $duration %= 3600; push(@chunks, sprintf('%dh',$tmp)); }
2525 $tmp = int($duration / 60);
2526 if ($tmp) { $duration %= 60; push(@chunks, sprintf('%dm',$tmp)); }
2527
2528 if (scalar @chunks) { push(@chunks, sprintf('%ds', $duration)) if $duration; }
2529 else { push(@chunks, sprintf('%.4fs', $duration)); }
2530 return join(' ', @chunks);
2531}
2532
2533=item string_to_duration($) [PUBLIC,HYBRID]
2534
2535 Usage : my $result = Value::Convertor->string_to_duration($value);
2536 my $result = $convertor->string_to_duration($value);
2537 Purpose : Converts string 'duration' to numeric representation
2538 Arguments : INTEGER $duration - Value to be converted [MANDATORY]
2539 Returns : STRING $duration_str
2540 Throws : Dies, if not given any conversion argument
2541
2542=cut
2543
2544sub string_to_duration($)
2545{
2546 my $invocant = shift;
2547 my ($duration,) = @_;
2548 confess "Expected conversion argument not given" unless defined $duration;
2549
2550 my $numduration = 0;
2551 $duration =~ s/\s+//g;
2552 if ($duration =~ /([\d]+)w/) { $numduration += (604800 * $1); $duration =~ s/([\d]+w)//; }
2553 if ($duration =~ /([\d]+)d/) { $numduration += ( 86400 * $1); $duration =~ s/([\d]+d)//; }
2554 if ($duration =~ /([\d]+)h/) { $numduration += ( 3600 * $1); $duration =~ s/([\d]+h)//; }
2555 if ($duration =~ /([\d]+)m/) { $numduration += ( 60 * $1); $duration =~ s/([\d]+m)//; }
2556
2557 $duration =~ s/s//g;
2558 $numduration += $duration;
2559
2560 return $numduration;
2561}
2562
2563#-------------------------------------------------------------------------------
2564
2565=item list_to_array($) [PUBLIC,HYBRID]
2566
2567 Usage : my $result = Value::Convertor->list_to_array($value);
2568 my $result = $convertor->list_to_array($value);
2569 Purpose : Converts 'list' to 'array'
2570 Arguments : STRING $list - Value to be converted [MANDATORY]
2571 Returns : ARRAY REFERENCE $array
2572 Throws : Dies, if not given any conversion argument
2573
2574=cut
2575
2576sub list_to_array($)
2577{
2578 my $invocant = shift;
2579 my ($list,) = @_;
2580 confess "Expected conversion argument not given" unless defined $list;
2581
2582 my @items = split(/[,; ]+/, $list);
2583 return \@items;
2584}
2585
2586=item array_to_list($;$) [PUBLIC,HYBRID]
2587
2588 Usage : my $result = Value::Convertor->array_to_list($value);
2589 my $result = $convertor->array_to_list($value);
2590 Purpose : Converts 'array' to 'list'
2591 Arguments : ARRAY REFERENCE $array - Value to be converted [MANDATORY]
2592 STRING $separator - string separator [OPTIONAL, DEFAULT = ',']
2593 Returns : STRING $list
2594 Throws : Dies, if not given any conversion argument
2595
2596=cut
2597
2598sub array_to_list($;$)
2599{
2600 my $invocant = shift;
2601 my ($array, $separator) = @_;
2602 confess "Expected conversion argument not given" unless defined $array;
2603
2604 $separator = ',' unless $separator;
2605
2606 return join($separator, @$array);
2607}
2608
2609#-------------------------------------------------------------------------------
2610# Private interface
2611#-------------------------------------------------------------------------------
2612
2613# _init [PROTECTED]
2614#
2615# Usage : return $self->_init(@_);
2616# Purpose : Initialize the new Value::Convertor instance
2617# Arguments : None
2618# Returns : Value::Convertor reference
2619# Throws : Dies, if invoked on class
2620# Comments : This method must never be called directly, it is supposed to be overloaded by subclasses.
2621
2622sub _init
2623{
2624 my $self = shift;
2625 confess "Instance method not invoked on object instance" unless blessed($self);
2626 return $self;
2627}
2628
2629# _zero_pad($$$) [PROTECTED]
2630#
2631# Usage : my $str = $invocant->_zero_pad($str, $lenght);
2632# Purpose : Pad string from left or right with zeros to specified length
2633# Arguments : STRING $str - string to be padded
2634# INTEGER $len - length of resulting string
2635# BOOL $right - 0 = pad from left, 1 = pad from right
2636# Returns : STRING $str
2637# Throws : Nothing
2638
2639sub _zero_pad($$$)
2640{
2641 my $invocant = shift;
2642 my ($str, $len, $right) = @_;
2643
2644 return $str unless $len and int($len);
2645
2646 my $i;
2647 my $strlen = length($str);
2648 for ($i = 0; $i < $len - $strlen; $i++)
2649 {
2650 unless ($right) { $str = "0" . $str; }
2651 else { $str = $str . "0"; }
2652 }
2653 return $str;
2654}
2655
2656# _zero_pad_n($$$) [PROTECTED]
2657#
2658# Usage : my $str = $invocant->_zero_pad_n($str, $mod);
2659# Purpose : Pad string from left or right with zeros to length with specified
2660# modulus (for example binary to octets, or hexadecimal to pairs)
2661# Arguments : STRING $str - string to be padded
2662# INTEGER $mod - modulus of the length of resulting string
2663# BOOL $right - 0 = pad from left, 1 = pad from right
2664# Returns : STRING $str
2665# Throws : Nothing
2666
2667sub _zero_pad_n($$$)
2668{
2669 my $invocant = shift;
2670 my ($str, $mod, $right) = @_;
2671
2672 return $str unless $mod and int($mod);
2673
2674 my $i;
2675 my $strlen = length($str);
2676 my $short = $strlen % $mod;
2677
2678 return $str unless $short;
2679
2680 for ($i = 0; $i < $mod - $short; $i++)
2681 {
2682 unless ($right) { $str = "0" . $str; }
2683 else { $str = $str . "0"; }
2684 }
2685 return $str;
2686}
2687
2688=pod
2689
2690=back
2691
2692=cut
2693
269413µs1;
 
# spent 4µs within Value::Convertor::CORE:match which was called: # once (4µs+0s) by Value::Convertor::ipv4str_to_int at line 557
sub Value::Convertor::CORE:match; # opcode
# spent 10µs within Value::Convertor::CORE:pack which was called 3 times, avg 3µs/call: # 2 times (3µs+0s) by Value::Convertor::ipv4int_to_bin at line 728, avg 1µs/call # once (7µs+0s) by Value::Convertor::ipv4str_to_int at line 559
sub Value::Convertor::CORE:pack; # opcode
# spent 39µs within Value::Convertor::CORE:qr which was called 19 times, avg 2µs/call: # 8 times (16µs+0s) by Value::Convertor::BEGIN@153 at line 162, avg 2µs/call # 7 times (17µs+0s) by Value::Convertor::BEGIN@153 at line 173, avg 2µs/call # 4 times (7µs+0s) by Value::Convertor::BEGIN@153 at line 183, avg 2µs/call
sub Value::Convertor::CORE:qr; # opcode
# spent 1µs within Value::Convertor::CORE:regcomp which was called: # once (1µs+0s) by Value::Convertor::ipv4str_to_int at line 557
sub Value::Convertor::CORE:regcomp; # opcode
# spent 5µs within Value::Convertor::CORE:unpack which was called 3 times, avg 2µs/call: # 2 times (3µs+0s) by Value::Convertor::bin_to_hexstr at line 441, avg 1µs/call # once (2µs+0s) by Value::Convertor::ipv4str_to_int at line 559
sub Value::Convertor::CORE:unpack; # opcode