File Coverage

blib/lib/Time/Piece/Guess.pm
Criterion Covered Total %
statement 37 161 22.9
branch 52 212 24.5
condition 3 12 25.0
subroutine 6 6 100.0
pod 2 2 100.0
total 100 393 25.4


line stmt bran cond sub pod time code
1             package Time::Piece::Guess;
2              
3 2     2   151186 use 5.006;
  2         11  
4 2     2   11 use strict;
  2         3  
  2         68  
5 2     2   12 use warnings;
  2         4  
  2         66  
6 2     2   549 use Time::Piece;
  2         12662  
  2         11  
7              
8             =head1 NAME
9              
10             Time::Piece::Guess - Compares the passed string against common patterns and returns a format to use with Time::Piece or object
11              
12             =head1 VERSION
13              
14             Version 0.1.0
15              
16             =cut
17              
18             our $VERSION = '0.1.0';
19              
20             =head1 SYNOPSIS
21              
22             use Time::Piece::Guess;
23             use Time::Piece;
24              
25             my $string='2023-02-27T11:00:18.33';
26             my ($format, $ms_clean_regex) = Time::Piece::Guess->guess('2023-02-27T11:00:18');
27             # apply the regex if needed
28             if (defined( $ms_clean_regex )){
29             $string=~s/$ms_clean_regex//;
30             }
31             my $tp_object;
32             if (!defined( $format )){
33             print "No matching format found\n";
34             }else{
35             $tp_object = Time::Piece->strptime( '2023-02-27T11:00:18' , $format );
36             }
37              
38             $tp_object = Time::Piece::Guess->guess_to_object('2023-02-27T11:00:18');
39             if (!defined( $tp_object )){
40             print "No matching format found\n";
41             }
42              
43             =head1 METHODS
44              
45             =head2 guess
46              
47             Compares it against various patterns and returns the matching string for use with
48             parsing that format.
49              
50             If one can't be matched, undef is returned. Two values are returned. The first is the format
51             of it and the second is a regexp to remove microseconds if needed.
52              
53             This will attempt to remove microseconds as below.
54              
55             my $string='2023-02-27T11:00:18.33';
56             my ($format, $ms_clean_regex) = Time::Piece::Guess->guess('2023-02-27T11:00:18');
57             # apply the regex if needed
58             if (defined( $ms_clean_regex )){
59             $string=~s/$ms_clean_regex//;
60             }
61             my $tp_object;
62             if (!defined( $format )){
63             print "No matching format found\n";
64             }else{
65             $tp_object = Time::Piece->strptime( '2023-02-27T11:00:18' , $format );
66             }
67              
68              
69             =cut
70              
71             sub guess {
72 3     3 1 112 my $string = $_[1];
73              
74 3 50       10 if ( !defined($string) ) {
75 0         0 return undef;
76             }
77              
78             # remove micro seconds if they are present
79 3         4 my $regex;
80 3 100       12 if ( $string =~ /\.\d+/ ) {
81 1         5 $regex = qr/\.\d+/;
82 1         6 $string =~ s/$regex//;
83             }
84              
85 3         5 my $format;
86 3 50       92 if ( $string =~ /^\d+$/ ) {
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    100          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    50          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
87 0         0 $format = '%s';
88             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9][-+]\d+$/ ) {
89 0         0 $format = '%Y-%m-%d %H:%M%z';
90             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9]Z$/ ) {
91 0         0 $format = '%Y-%m-%d %H:%MZ';
92             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9]\ .+$/ ) {
93 0         0 $format = '%Y-%m-%d %H:%M %Z';
94             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9][-+]\d+$/ ) {
95 0         0 $format = '%Y-%m-%d %H:%M:%S%z';
96             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]Z$/ ) {
97 0         0 $format = '%Y-%m-%d %H:%M:%SZ';
98             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\ .+$/ ) {
99 0         0 $format = '%Y-%m-%d %H:%M:%S %Z';
100             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9][-+]\d+$/ ) {
101 0         0 $format = '%Y-%m-%dT%H:%M%z';
102             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9]Z$/ ) {
103 0         0 $format = '%Y-%m-%dT%H:%MZ';
104             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9]\ .+$/ ) {
105 0         0 $format = '%Y-%m-%dT%H:%M %Z';
106             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9][-+]\d+$/ ) {
107 2         3 $format = '%Y-%m-%dT%H:%M:%S%z';
108             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]Z$/ ) {
109 0         0 $format = '%Y-%m-%dT%H:%M:%SZ';
110             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\ .+$/ ) {
111 0         0 $format = '%Y-%m-%dT%H:%M:%S %Z';
112             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9][-+]\d+$/ ) {
113 0         0 $format = '%Y-%m-%dT%H:%M%z';
114             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9]Z$/ ) {
115 0         0 $format = '%Y-%m-%dT%H:%MZ';
116             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9]\ .+$/ ) {
117 0         0 $format = '%Y-%m-%dT%H:%M %Z';
118             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9][-+]\d+$/ ) {
119 0         0 $format = '%Y-%m-%d/%H:%M:%S%z';
120             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]Z$/ ) {
121 0         0 $format = '%Y-%m-%d/%H:%M:%SZ';
122             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\ .+$/ ) {
123 0         0 $format = '%Y-%m-%d/%H:%M:%S %Z';
124             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9][-+]\d+$/ ) {
125 0         0 $format = '%Y%m%d %H:%M%z';
126             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9]Z$/ ) {
127 0         0 $format = '%Y%m%d %H:%M%Z';
128             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9]\ .+$/ ) {
129 0         0 $format = '%Y%m%d %H:%M %Z';
130             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9][-+]\d+$/ ) {
131 0         0 $format = '%Y%m%d %H:%M:%S%z';
132             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]Z$/ ) {
133 0         0 $format = '%Y%m%d %H:%M:%SZ';
134             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\ .+$/ ) {
135 0         0 $format = '%Y%m%d %H:%M:%S %Z';
136             } elsif ( $string =~ /\^d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9][-+]\d+$/ ) {
137 0         0 $format = '%Y%m%dT%H:%M%z';
138             } elsif ( $string =~ /\^d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9]Z$/ ) {
139 0         0 $format = '%Y%m%dT%H:%MZ';
140             } elsif ( $string =~ /\^d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9]\ .+$/ ) {
141 0         0 $format = '%Y%m%dT%H:%M %Z';
142             } elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9][-+]\d+$/ ) {
143 0         0 $format = '%Y%m%dT%H:%M:%S%z';
144             } elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]Z$/ ) {
145 0         0 $format = '%Y%m%dT%H:%M:%SZ';
146             } elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\ .+$/ ) {
147 0         0 $format = '%Y%m%dT%H:%M:%S %Z';
148             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9][-+]\d+$/ ) {
149 0         0 $format = '%Y%m%dT%H:%M%z';
150             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9]Z$/ ) {
151 0         0 $format = '%Y%m%dT%H:%MZ';
152             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9]\ .+$/ ) {
153 0         0 $format = '%Y%m%dT%H:%M %Z';
154             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9][-+]\d+$/ ) {
155 0         0 $format = '%Y%m%d/%H:%M:%S%z';
156             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]Z$/ ) {
157 0         0 $format = '%Y%m%d/%H:%M:%SZ';
158             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\ .+$/ ) {
159 0         0 $format = '%Y%m%d/%H:%M:%S %Z';
160             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9]$/ ) {
161 0         0 $format = '%Y-%m-%d %H:%M';
162             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]$/ ) {
163 0         0 $format = '%Y-%m-%d %H:%M:%S';
164             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9]$/ ) {
165 0         0 $format = '%Y-%m-%dT%H:%M';
166             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]$/ ) {
167 1         4 $format = '%Y-%m-%dT%H:%M:%S';
168             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9]$/ ) {
169 0         0 $format = '%Y-%m-%dT%H:%M';
170             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]$/ ) {
171 0         0 $format = '%Y-%m-%d/%H:%M:%S';
172             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9]$/ ) {
173 0         0 $format = '%Y%m%d %H:%M';
174             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]$/ ) {
175 0         0 $format = '%Y%m%d %H:%M:%S';
176             } elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9]$/ ) {
177 0         0 $format = '%Y%m%dT%H:%M';
178             } elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]$/ ) {
179 0         0 $format = '%Y%m%dT%H:%M:%S';
180             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9]$/ ) {
181 0         0 $format = '%Y%m%dT%H:%M';
182             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]$/ ) {
183 0         0 $format = '%Y%m%d/%H:%M:%S';
184             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9][-+]\d+$/ ) {
185 0         0 $format = '%Y-%m-%d %H%M%z';
186             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9]Z$/ ) {
187 0         0 $format = '%Y-%m-%d %H%MZ';
188             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9]\ .+$/ ) {
189 0         0 $format = '%Y-%m-%d %H%M %Z';
190             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9][0-5][0-9][-+]\d+$/ ) {
191 0         0 $format = '%Y-%m-%d %H%M%S%z';
192             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9][0-5][0-9]Z$/ ) {
193 0         0 $format = '%Y-%m-%d %H%M%SZ';
194             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9][0-5][0-9]\ .+$/ ) {
195 0         0 $format = '%Y-%m-%d %H%M%S %Z';
196             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9][-+]\d+$/ ) {
197 0         0 $format = '%Y-%m-%dT%H%M%z';
198             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9]Z$/ ) {
199 0         0 $format = '%Y-%m-%dT%H%MZ';
200             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9]\ .+$/ ) {
201 0         0 $format = '%Y-%m-%dT%H%M %Z';
202             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9][0-5][0-9][-+]\d+$/ ) {
203 0         0 $format = '%Y-%m-%dT%H%M%S%z';
204             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9][0-5][0-9]Z$/ ) {
205 0         0 $format = '%Y-%m-%dT%H%M%SZ';
206             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9][0-5][0-9]\ .+$/ ) {
207 0         0 $format = '%Y-%m-%dT%H%M%S %Z';
208             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9][0-5][0-9][-+]\d+$/ ) {
209 0         0 $format = '%Y-%m-%dT%H%M%z';
210             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9][0-5][0-9]Z$/ ) {
211 0         0 $format = '%Y-%m-%dT%H%MZ';
212             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9][0-5][0-9]\ .+$/ ) {
213 0         0 $format = '%Y-%m-%dT%H%M %Z';
214             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9][0-5][0-9][0-5][0-9][-+]\d+$/ ) {
215 0         0 $format = '%Y-%m-%d/%H%M%S%z';
216             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9][0-5][0-9][0-5][0-9]Z$/ ) {
217 0         0 $format = '%Y-%m-%d/%H%M%SZ';
218             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9][0-5][0-9][-+]\d+$/ ) {
219 0         0 $format = '%Y%m%d %H%M%z';
220             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9][0-5][0-9]Z$/ ) {
221 0         0 $format = '%Y%m%d %H%M%Z';
222             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9][0-5][0-9][0-5][0-9][-+]\d+$/ ) {
223 0         0 $format = '%Y%m%d %H%M%S%z';
224             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9][0-5][0-9][0-5][0-9]Z$/ ) {
225 0         0 $format = '%Y%m%d %H%M%SZ';
226             } elsif ( $string =~ /\^d\d\d\d\d\d\d\dT[0-2][0-9][0-5][0-9][-+]\d+$/ ) {
227 0         0 $format = '%Y%m%dT%H%M%z';
228             } elsif ( $string =~ /\^d\d\d\d\d\d\d\dT[0-2][0-9][0-5][0-9]Z$/ ) {
229 0         0 $format = '%Y%m%dT%H%MZ';
230             } elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9][0-5][0-9][0-5][0-9][-+]\d+$/ ) {
231 0         0 $format = '%Y%m%dT%H%M%S%z';
232             } elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9][0-5][0-9][0-5][0-9]Z$/ ) {
233 0         0 $format = '%Y%m%dT%H%M%SZ';
234             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9][0-5][0-9][-+]\d+$/ ) {
235 0         0 $format = '%Y%m%dT%H%M%z';
236             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9][0-5][0-9]Z$/ ) {
237 0         0 $format = '%Y%m%dT%H%MZ';
238             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9][0-5][0-9][0-5][0-9][-+]\d+$/ ) {
239 0         0 $format = '%Y%m%d/%H%M%S%z';
240             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9][0-5][0-9][0-5][0-9]Z$/ ) {
241 0         0 $format = '%Y%m%d/%H%M%SZ';
242             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9]$/ ) {
243 0         0 $format = '%Y-%m-%d %H%M';
244             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9][0-5][0-9]$/ ) {
245 0         0 $format = '%Y-%m-%d %H%M%S';
246             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9]$/ ) {
247 0         0 $format = '%Y-%m-%dT%H%M';
248             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9][0-5][0-9]$/ ) {
249 0         0 $format = '%Y-%m-%dT%H%M%S';
250             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9][0-5][0-9]$/ ) {
251 0         0 $format = '%Y-%m-%dT%H%M';
252             } elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9][0-5][0-9][0-5][0-9]$/ ) {
253 0         0 $format = '%Y-%m-%d/%H%M%S';
254             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9][0-5][0-9]$/ ) {
255 0         0 $format = '%Y%m%d %H%M';
256             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9][0-5][0-9][0-5][0-9]$/ ) {
257 0         0 $format = '%Y%m%d %H%M%S';
258             } elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9][0-5][0-9]$/ ) {
259 0         0 $format = '%Y%m%dT%H%M';
260             } elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9][0-5][0-9][0-5][0-9]$/ ) {
261 0         0 $format = '%Y%m%dT%H%M%S';
262             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9][0-5][0-9]$/ ) {
263 0         0 $format = '%Y%m%dT%H%M';
264             } elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9][0-5][0-9][0-5][0-9]$/ ) {
265 0         0 $format = '%Y%m%d/%H%M%S';
266              
267             }
268              
269 3         11 return $format, $regex;
270             } ## end sub guess
271              
272             =head2 guess_to_object
273              
274             Takes the string, calles guess on it. If it gets a hit, it then returns
275             the Time::Piece object.
276              
277             Optionally there is the option to enable specials as well. See the section
278             on specials further down.
279              
280             If it fails, undef is returned.
281              
282             $tp_object = Time::Piece::Guess->guess_to_object('2023-02-27T11:00:18');
283             if (!defined( $tp_object )){
284             print "No matching format found\n";
285             }
286              
287             The same thing, but enabling specials and, resulting in it appending the local timezone
288             offset data that would correspond to %z.
289              
290             $tp_object = Time::Piece::Guess->guess_to_object('2023-02-27T11:00:18zz', 1);
291              
292             =cut
293              
294             sub guess_to_object {
295 1     1 1 122 my $string = $_[1];
296 1         3 my $special = $_[2];
297              
298 1 50       4 if ( !defined($string) ) {
299 0         0 return undef;
300             }
301              
302             # if special is enabled and it looks like a now special
303             # return the current time with the diffence applied
304 1 0 0     4 if (
      33        
305             $special
306             && ( $string =~ /^now$/
307             || $string =~ /^now[\-\+]\d+[mhdw]?$/
308             || $string =~ /^[\-\+]\d+[mhdw]?$/ )
309             )
310             {
311 0         0 my $t = localtime;
312              
313             # if just now, it is asking for the current time, so just return that
314 0 0       0 if ( $string eq 'now' ) {
315 0         0 return $t;
316             }
317              
318             # since this is more than just now, remove the now part and proceed to
319             # figure out what the off set is
320 0         0 $string =~ s/^now//;
321              
322             # figure out what to multiply the offset by
323             # nothing, seconds
324 0         0 my $multiplier = 1;
325 0 0       0 if ( $string =~ /m$/ ) {
    0          
    0          
    0          
326             # minutes
327 0         0 $multiplier = 60;
328 0         0 $string =~ s/m$//;
329             } elsif ( $string =~ /h$/ ) {
330             # hours
331 0         0 $multiplier = 60 * 60;
332 0         0 $string =~ s/h$//;
333             } elsif ( $string =~ /d$/ ) {
334             # days
335 0         0 $multiplier = 60 * 60 * 24;
336 0         0 $string =~ s/d$//;
337             } elsif ( $string =~ /w$/ ) {
338             # weeks
339 0         0 $multiplier = 60 * 60 * 24 * 7;
340 0         0 $string =~ s/w$//;
341             }
342              
343             # figure out the direction we are going
344             # multiply it
345             # apply the offset
346 0 0       0 if ( $string =~ /^\-/ ) {
347 0         0 $string =~ s/^\-//;
348 0         0 $string = $string * $multiplier;
349 0         0 $t = $t - $string;
350             } else {
351 0         0 $string =~ s/^\+//;
352 0         0 $string = $string * $multiplier;
353 0         0 $t = $t + $string;
354             }
355              
356 0         0 return $t;
357             } ## end if ( $special && ( $string =~ /^now$/ || $string...))
358              
359             # if special is enabled and ZZ or zz is used at the end
360             # append the timezone abbreviation
361 1         2 my $make_local = 0;
362 1 50 33     10 if ( $special
    50 33        
363             && $string =~ /zz$/ )
364             {
365 0         0 my $t = localtime;
366 0         0 my $zone = $t->strftime("%z");
367 0         0 $string =~ s/zz$/$zone/;
368 0         0 $make_local = 1;
369             } elsif ( $special
370             && $string =~ /ZZ$/ )
371             {
372 0         0 my $t = localtime;
373 0         0 my $zone = $t->strftime("%Z");
374 0         0 $string =~ s/\ ?ZZ$/\ $zone/;
375 0         0 $make_local = 1;
376             }
377              
378 1         4 my ( $format, $ms_clean_regex ) = Time::Piece::Guess->guess($string);
379              
380 1 50       21 if ( !defined($format) ) {
381 0         0 return undef;
382             }
383              
384 1 50       5 if ( defined($ms_clean_regex) ) {
385 0         0 $string =~ s/$ms_clean_regex//;
386             }
387              
388 1         2 my $t;
389 1         2 eval { $t = Time::Piece->strptime( $string, $format ); };
  1         14  
390 1 50       70 if ($@) {
391 0         0 return undef;
392             }
393              
394             # unfortunately Time::Piece lakes the ability to set this currently
395 1 50       5 if ($make_local) {
396 0         0 $t->[10] = 1;
397             }
398              
399 1         5 return $t;
400             } ## end sub guess_to_object
401              
402             =head1 SPECIAL FORMATS
403              
404             =head2 now
405              
406             Now is returns current time.
407              
408             =head2 now[-+]\d+[mhdw]?
409              
410             Returns the current time after adding or subtracting the specified number of seconds.
411              
412             The following may be applied to the end to act as a multipler.
413              
414             - m :: The specified number is minutes.
415              
416             - h :: The specified number is hours.
417              
418             - d :: The specified number is hours.
419              
420             - w :: The specified number is weeks.
421              
422             So 'now-5' would be now minus five seconds and 'now-5m' would be now minus five minutes.
423              
424             =head2 zz
425              
426             Apply the current time zone to the end prior to parsing. Offset is determined by %z.
427              
428             '2023-07-23T17:34:00zz' if in -0500 would become '2023-07-23T17:34:00-0500'.
429              
430             =head2 ZZ
431              
432             Apply the current time zone name to the end prior to parsing. The name is determined
433             by %Z.
434              
435             =head1 AUTHOR
436              
437             Zane C. Bowers-Hadley, C<< >>
438              
439             =head1 BUGS
440              
441             Please report any bugs or feature requests to C, or through
442             the web interface at L. I will be notified, and then you'll
443             automatically be notified of progress on your bug as I make changes.
444              
445              
446              
447              
448             =head1 SUPPORT
449              
450             You can find documentation for this module with the perldoc command.
451              
452             perldoc Time::Piece::Guess
453              
454              
455             You can also look for information at:
456              
457             =over 4
458              
459             =item * RT: CPAN's request tracker (report bugs here)
460              
461             L
462              
463             =item * CPAN Ratings
464              
465             L
466              
467             =item * Search CPAN
468              
469             L
470              
471             =back
472              
473              
474             =head1 ACKNOWLEDGEMENTS
475              
476              
477             =head1 LICENSE AND COPYRIGHT
478              
479             This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
480              
481             This is free software, licensed under:
482              
483             The Artistic License 2.0 (GPL Compatible)
484              
485              
486             =cut
487              
488             1; # End of Time::Piece::Guess