NAME
Time::Piece::Guess - Compares the passed string against common patterns and returns a format to use with Time::Piece or object
VERSION
Version 0.1.1
SYNOPSIS
use Time::Piece::Guess;
use Time::Piece;
my $string='2023-02-27T11:00:18.33';
my ($format, $ms_clean_regex) = Time::Piece::Guess->guess('2023-02-27T11:00:18');
# apply the regex if needed
if (defined( $ms_clean_regex )){
$string=~s/$ms_clean_regex//;
}
my $tp_object;
if (!defined( $format )){
print "No matching format found\n";
}else{
$tp_object = Time::Piece->strptime( '2023-02-27T11:00:18' , $format );
}
$tp_object = Time::Piece::Guess->guess_to_object('2023-02-27T11:00:18');
if (!defined( $tp_object )){
print "No matching format found\n";
}
METHODS
guess
Compares it against various patterns and returns the matching string for use with parsing that format.
If one can't be matched, undef is returned. Two values are returned. The first is the format of it and the second is a regexp to remove microseconds if needed.
This will attempt to remove microseconds as below.
my $string='2023-02-27T11:00:18.33';
my ($format, $ms_clean_regex) = Time::Piece::Guess->guess('2023-02-27T11:00:18');
# apply the regex if needed
if (defined( $ms_clean_regex )){
$string=~s/$ms_clean_regex//;
}
my $tp_object;
if (!defined( $format )){
print "No matching format found\n";
}else{
$tp_object = Time::Piece->strptime( '2023-02-27T11:00:18' , $format );
}
Worth noting that Time::Piece as of the writing of this has a bug in which if just hours are present for %z and minutes are not it will error.
if ($format =~ /\%z/ && $string =~ /[-+]\d\d$/) {
sstring=$string.'00';
}
guess_to_object
Takes the string, calles guess on it. If it gets a hit, it then returns the Time::Piece object.
Optionally there is the option to enable specials as well. See the section on specials further down.
If it fails, undef is returned.
$tp_object = Time::Piece::Guess->guess_to_object('2023-02-27T11:00:18');
if (!defined( $tp_object )){
print "No matching format found\n";
}
The same thing, but enabling specials and, resulting in it appending the local timezone offset data that would correspond to %z.
$tp_object = Time::Piece::Guess->guess_to_object('2023-02-27T11:00:18zz', 1);
SPECIAL FORMATS
now
Now is returns current time.
now[-+]\d+[mhdw]?
Returns the current time after adding or subtracting the specified number of seconds.
The following may be applied to the end to act as a multipler.
- m :: The specified number is minutes.
- h :: The specified number is hours.
- d :: The specified number is hours.
- w :: The specified number is weeks.
So 'now-5' would be now minus five seconds and 'now-5m' would be now minus five minutes.
zz
Apply the current time zone to the end prior to parsing. Offset is determined by %z.
'2023-07-23T17:34:00zz' if in -0500 would become '2023-07-23T17:34:00-0500'.
ZZ
Apply the current time zone name to the end prior to parsing. The name is determined by %Z.
AUTHOR
Zane C. Bowers-Hadley, <vvelox at vvelox.net>
BUGS
Please report any bugs or feature requests to bug-time-piece-guess at rt.cpan.org
, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Time-Piece-Guess. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Time::Piece::Guess
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
CPAN Ratings
Search CPAN
ACKNOWLEDGEMENTS
LICENSE AND COPYRIGHT
This software is Copyright (c) 2023 by Zane C. Bowers-Hadley.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)