File Coverage

blib/lib/Time/Piece/Guess.pm
Criterion Covered Total %
statement 32 125 25.6
branch 49 192 25.5
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 89 325 27.3


line stmt bran cond sub pod time code
1             package Time::Piece::Guess;
2              
3 2     2   124118 use 5.006;
  2         8  
4 2     2   9 use strict;
  2         4  
  2         58  
5 2     2   10 use warnings;
  2         3  
  2         61  
6 2     2   504 use Time::Piece;
  2         10436  
  2         9  
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.0.3
15              
16             =cut
17              
18             our $VERSION = '0.0.3';
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 82 my $string = $_[1];
73              
74 3 50       7 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       9 if ($string =~ /\.\d+/) {
81 1         4 $regex=qr/\.\d+/;
82 1         5 $string=~s/$regex//;
83             }
84              
85 3         4 my $format;
86 3 50       81 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             }
89             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9][-+]\d+$/ ) {
90 0         0 $format = '%Y-%m-%d %H:%M%z';
91             }
92             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9]Z$/ ) {
93 0         0 $format = '%Y-%m-%d %H:%MZ';
94             }
95             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9]\ .+$/ ) {
96 0         0 $format = '%Y-%m-%d %H:%M %Z';
97             }
98             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9][-+]\d+$/ ) {
99 0         0 $format = '%Y-%m-%d %H:%M:%S%z';
100             }
101             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]Z$/ ) {
102 0         0 $format = '%Y-%m-%d %H:%M:%SZ';
103             }
104             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\ .+$/ ) {
105 0         0 $format = '%Y-%m-%d %H:%M:%S %Z';
106             }
107             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9][-+]\d+$/ ) {
108 0         0 $format = '%Y-%m-%dT%H:%M%z';
109             }
110             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9]Z$/ ) {
111 0         0 $format = '%Y-%m-%dT%H:%MZ';
112             }
113             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9]\ .+$/ ) {
114 0         0 $format = '%Y-%m-%dT%H:%M %Z';
115             }
116             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9][-+]\d+$/ ) {
117 2         4 $format = '%Y-%m-%dT%H:%M:%S%z';
118             }
119             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]Z$/ ) {
120 0         0 $format = '%Y-%m-%dT%H:%M:%SZ';
121             }
122             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\ .+$/ ) {
123 0         0 $format = '%Y-%m-%dT%H:%M:%S %Z';
124             }
125             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9][-+]\d+$/ ) {
126 0         0 $format = '%Y-%m-%dT%H:%M%z';
127             }
128             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9]Z$/ ) {
129 0         0 $format = '%Y-%m-%dT%H:%MZ';
130             }
131             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9]\ .+$/ ) {
132 0         0 $format = '%Y-%m-%dT%H:%M %Z';
133             }
134             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9][-+]\d+$/ ) {
135 0         0 $format = '%Y-%m-%d/%H:%M:%S%z';
136             }
137             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]Z$/ ) {
138 0         0 $format = '%Y-%m-%d/%H:%M:%SZ';
139             }
140             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\ .+$/ ) {
141 0         0 $format = '%Y-%m-%d/%H:%M:%S %Z';
142             }
143             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9][-+]\d+$/ ) {
144 0         0 $format = '%Y%m%d %H:%M%z';
145             }
146             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9]Z$/ ) {
147 0         0 $format = '%Y%m%d %H:%M%Z';
148             }
149             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9]\ .+$/ ) {
150 0         0 $format = '%Y%m%d %H:%M %Z';
151             }
152             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9][-+]\d+$/ ) {
153 0         0 $format = '%Y%m%d %H:%M:%S%z';
154             }
155             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]Z$/ ) {
156 0         0 $format = '%Y%m%d %H:%M:%SZ';
157             }
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             }
161             elsif ( $string =~ /\^d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9][-+]\d+$/ ) {
162 0         0 $format = '%Y%m%dT%H:%M%z';
163             }
164             elsif ( $string =~ /\^d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9]Z$/ ) {
165 0         0 $format = '%Y%m%dT%H:%MZ';
166             }
167             elsif ( $string =~ /\^d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9]\ .+$/ ) {
168 0         0 $format = '%Y%m%dT%H:%M %Z';
169             }
170             elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9][-+]\d+$/ ) {
171 0         0 $format = '%Y%m%dT%H:%M:%S%z';
172             }
173             elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]Z$/ ) {
174 0         0 $format = '%Y%m%dT%H:%M:%SZ';
175             }
176             elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]\ .+$/ ) {
177 0         0 $format = '%Y%m%dT%H:%M:%S %Z';
178             }
179             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9][-+]\d+$/ ) {
180 0         0 $format = '%Y%m%dT%H:%M%z';
181             }
182             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9]Z$/ ) {
183 0         0 $format = '%Y%m%dT%H:%MZ';
184             }
185             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9]\ .+$/ ) {
186 0         0 $format = '%Y%m%dT%H:%M %Z';
187             }
188             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9][-+]\d+$/ ) {
189 0         0 $format = '%Y%m%d/%H:%M:%S%z';
190             }
191             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]Z$/ ) {
192 0         0 $format = '%Y%m%d/%H:%M:%SZ';
193             }
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             }
197             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9]$/ ) {
198 0         0 $format = '%Y-%m-%d %H:%M';
199             }
200             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]$/ ) {
201 0         0 $format = '%Y-%m-%d %H:%M:%S';
202             }
203             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9]$/ ) {
204 0         0 $format = '%Y-%m-%dT%H:%M';
205             }
206             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]$/ ) {
207 1         4 $format = '%Y-%m-%dT%H:%M:%S';
208             }
209             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9]$/ ) {
210 0         0 $format = '%Y-%m-%dT%H:%M';
211             }
212             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]$/ ) {
213 0         0 $format = '%Y-%m-%d/%H:%M:%S';
214             }
215             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9]$/ ) {
216 0         0 $format = '%Y%m%d %H:%M';
217             }
218             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]$/ ) {
219 0         0 $format = '%Y%m%d %H:%M:%S';
220             }
221             elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9]$/ ) {
222 0         0 $format = '%Y%m%dT%H:%M';
223             }
224             elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]$/ ) {
225 0         0 $format = '%Y%m%dT%H:%M:%S';
226             }
227             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9]$/ ) {
228 0         0 $format = '%Y%m%dT%H:%M';
229             }
230             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9]\:[0-5][0-9]\:[0-5][0-9]$/ ) {
231 0         0 $format = '%Y%m%d/%H:%M:%S';
232             }
233             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9][-+]\d+$/ ) {
234 0         0 $format = '%Y-%m-%d %H%M%z';
235             }
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-%d %H%MZ';
238             }
239             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9]\ .+$/ ) {
240 0         0 $format = '%Y-%m-%d %H%M %Z';
241             }
242             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9][0-5][0-9][-+]\d+$/ ) {
243 0         0 $format = '%Y-%m-%d %H%M%S%z';
244             }
245             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9][0-5][0-9]Z$/ ) {
246 0         0 $format = '%Y-%m-%d %H%M%SZ';
247             }
248             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9][0-5][0-9]\ .+$/ ) {
249 0         0 $format = '%Y-%m-%d %H%M%S %Z';
250             }
251             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9][-+]\d+$/ ) {
252 0         0 $format = '%Y-%m-%dT%H%M%z';
253             }
254             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9]Z$/ ) {
255 0         0 $format = '%Y-%m-%dT%H%MZ';
256             }
257             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9]\ .+$/ ) {
258 0         0 $format = '%Y-%m-%dT%H%M %Z';
259             }
260             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9][0-5][0-9][-+]\d+$/ ) {
261 0         0 $format = '%Y-%m-%dT%H%M%S%z';
262             }
263             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9][0-5][0-9]Z$/ ) {
264 0         0 $format = '%Y-%m-%dT%H%M%SZ';
265             }
266             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9][0-5][0-9]\ .+$/ ) {
267 0         0 $format = '%Y-%m-%dT%H%M%S %Z';
268             }
269             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9][0-5][0-9][-+]\d+$/ ) {
270 0         0 $format = '%Y-%m-%dT%H%M%z';
271             }
272             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9][0-5][0-9]Z$/ ) {
273 0         0 $format = '%Y-%m-%dT%H%MZ';
274             }
275             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9][0-5][0-9]\ .+$/ ) {
276 0         0 $format = '%Y-%m-%dT%H%M %Z';
277             }
278             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9][0-5][0-9][0-5][0-9][-+]\d+$/ ) {
279 0         0 $format = '%Y-%m-%d/%H%M%S%z';
280             }
281             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9][0-5][0-9][0-5][0-9]Z$/ ) {
282 0         0 $format = '%Y-%m-%d/%H%M%SZ';
283             }
284             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9][0-5][0-9][-+]\d+$/ ) {
285 0         0 $format = '%Y%m%d %H%M%z';
286             }
287             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9][0-5][0-9]Z$/ ) {
288 0         0 $format = '%Y%m%d %H%M%Z';
289             }
290             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9][0-5][0-9][0-5][0-9][-+]\d+$/ ) {
291 0         0 $format = '%Y%m%d %H%M%S%z';
292             }
293             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9][0-5][0-9][0-5][0-9]Z$/ ) {
294 0         0 $format = '%Y%m%d %H%M%SZ';
295             }
296             elsif ( $string =~ /\^d\d\d\d\d\d\d\dT[0-2][0-9][0-5][0-9][-+]\d+$/ ) {
297 0         0 $format = '%Y%m%dT%H%M%z';
298             }
299             elsif ( $string =~ /\^d\d\d\d\d\d\d\dT[0-2][0-9][0-5][0-9]Z$/ ) {
300 0         0 $format = '%Y%m%dT%H%MZ';
301             }
302             elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9][0-5][0-9][0-5][0-9][-+]\d+$/ ) {
303 0         0 $format = '%Y%m%dT%H%M%S%z';
304             }
305             elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9][0-5][0-9][0-5][0-9]Z$/ ) {
306 0         0 $format = '%Y%m%dT%H%M%SZ';
307             }
308             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9][0-5][0-9][-+]\d+$/ ) {
309 0         0 $format = '%Y%m%dT%H%M%z';
310             }
311             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9][0-5][0-9]Z$/ ) {
312 0         0 $format = '%Y%m%dT%H%MZ';
313             }
314             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9][0-5][0-9][0-5][0-9][-+]\d+$/ ) {
315 0         0 $format = '%Y%m%d/%H%M%S%z';
316             }
317             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9][0-5][0-9][0-5][0-9]Z$/ ) {
318 0         0 $format = '%Y%m%d/%H%M%SZ';
319             }
320             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9]$/ ) {
321 0         0 $format = '%Y-%m-%d %H%M';
322             }
323             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\ [0-2][0-9][0-5][0-9][0-5][0-9]$/ ) {
324 0         0 $format = '%Y-%m-%d %H%M%S';
325             }
326             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9]$/ ) {
327 0         0 $format = '%Y-%m-%dT%H%M';
328             }
329             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\dT[0-2][0-9][0-5][0-9][0-5][0-9]$/ ) {
330 0         0 $format = '%Y-%m-%dT%H%M%S';
331             }
332             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9][0-5][0-9]$/ ) {
333 0         0 $format = '%Y-%m-%dT%H%M';
334             }
335             elsif ( $string =~ /^\d\d\d\d\-\d\d-\d\d\/[0-2][0-9][0-5][0-9][0-5][0-9]$/ ) {
336 0         0 $format = '%Y-%m-%d/%H%M%S';
337             }
338             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9][0-5][0-9]$/ ) {
339 0         0 $format = '%Y%m%d %H%M';
340             }
341             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\ [0-2][0-9][0-5][0-9][0-5][0-9]$/ ) {
342 0         0 $format = '%Y%m%d %H%M%S';
343             }
344             elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9][0-5][0-9]$/ ) {
345 0         0 $format = '%Y%m%dT%H%M';
346             }
347             elsif ( $string =~ /^\d\d\d\d\d\d\d\dT[0-2][0-9][0-5][0-9][0-5][0-9]$/ ) {
348 0         0 $format = '%Y%m%dT%H%M%S';
349             }
350             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9][0-5][0-9]$/ ) {
351 0         0 $format = '%Y%m%dT%H%M';
352             }
353             elsif ( $string =~ /^\d\d\d\d\d\d\d\d\/[0-2][0-9][0-5][0-9][0-5][0-9]$/ ) {
354 0         0 $format = '%Y%m%d/%H%M%S';
355              
356             }
357              
358 3         9 return $format, $regex;
359             }
360              
361             =head2 guess_to_object
362              
363             Takes the string, calles guess on it. If it gets a hit, it then returns
364             the Time::Piece object.
365              
366             If it fails, undef is returned.
367              
368             $tp_object = Time::Piece::Guess->guess_to_object('2023-02-27T11:00:18');
369             if (!defined( $tp_object )){
370             print "No matching format found\n";
371             }
372              
373             =cut
374              
375             sub guess_to_object {
376 1     1 1 101 my $string = $_[1];
377              
378 1 50       3 if ( !defined($string) ) {
379 0         0 return undef;
380             }
381              
382 1         3 my ($format, $ms_clean_regex) = Time::Piece::Guess->guess($string);
383              
384 1 50       3 if ( !defined($format) ) {
385 0         0 return undef;
386             }
387              
388 1 50       3 if (defined( $ms_clean_regex )){
389 0         0 $string=~s/$ms_clean_regex//;
390             }
391              
392 1         2 my $t;
393 1         9 eval { $t = Time::Piece->strptime( $string, $format ); };
  1         4  
394 1 50       45 if ($@) {
395 0         0 return undef;
396             }
397              
398 1         3 return $t;
399             }
400              
401             =head1 AUTHOR
402              
403             Zane C. Bowers-Hadley, C<< >>
404              
405             =head1 BUGS
406              
407             Please report any bugs or feature requests to C, or through
408             the web interface at L. I will be notified, and then you'll
409             automatically be notified of progress on your bug as I make changes.
410              
411              
412              
413              
414             =head1 SUPPORT
415              
416             You can find documentation for this module with the perldoc command.
417              
418             perldoc Time::Piece::Guess
419              
420              
421             You can also look for information at:
422              
423             =over 4
424              
425             =item * RT: CPAN's request tracker (report bugs here)
426              
427             L
428              
429             =item * CPAN Ratings
430              
431             L
432              
433             =item * Search CPAN
434              
435             L
436              
437             =back
438              
439              
440             =head1 ACKNOWLEDGEMENTS
441              
442              
443             =head1 LICENSE AND COPYRIGHT
444              
445             This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
446              
447             This is free software, licensed under:
448              
449             The Artistic License 2.0 (GPL Compatible)
450              
451              
452             =cut
453              
454             1; # End of Time::Piece::Guess