|
DateTime::Format::Strptime - Parse and format strp and strf time patterns |
DateTime::Format::Strptime - Parse and format strp and strf time patterns
use DateTime::Format::Strptime;
my $Strp = new DateTime::Format::Strptime(
pattern => '%T',
locale => 'en_AU',
time_zone => 'Australia/Melbourne',
);
my $dt = $Strp->parse_datetime('23:16:42');
$Strp->format_datetime($dt);
# 23:16:42
# Croak when things go wrong:
my $Strp = new DateTime::Format::Strptime(
pattern => '%T',
locale => 'en_AU',
time_zone => 'Australia/Melbourne',
on_error => 'croak',
);
$newpattern = $Strp->pattern('%Q');
# Unidentified token in pattern: %Q in %Q at line 34 of script.pl
# Do something else when things go wrong:
my $Strp = new DateTime::Format::Strptime(
pattern => '%T',
locale => 'en_AU',
time_zone => 'Australia/Melbourne',
on_error => \&phone_police,
);
This module implements most of strptime(3), the POSIX function that
is the reverse of strftime(3), for DateTime. While strftime
takes a DateTime and a pattern and returns a string, strptime takes
a string and a pattern and returns the DateTime object
associated.
time_zone and a locale. If you specify a time zone
then any resulting DateTime object will be in that time zone. If you
do not specify a time_zone parameter, but there is a time zone in the
string you pass to parse_datetime, then the resulting DateTime will
use that time zone.
You can optionally use an on_error parameter. This parameter has three valid options:
This is the default behavior. The module will return undef whenever it gets upset. The error can be accessed using the $object->errstr method. This is the ideal behaviour for interactive use where a user might provide an illegal pattern or a date that doesn't match the pattern.
This used to be the default behaviour. The module will croak with an error message whenever it gets upset.
sub{$_[0]->{errmsg} = $_[1]; 1},
This class offers the following methods.
parse_datetime($string)DateTime object.
If given a string that doesn't match the pattern, the formatter will croak or return undef, depending on the setting of on_error in the constructor.
format_datetime($datetime)DateTime object, this methods returns a string formatted in
the object's format. This method is synonymous with DateTime's
strftime method.
locale($locale)If successful this method returns the current locale. (After processing as above).
pattern($strptime_pattern)on_error parameter)
If successful this method returns the current pattern. (After processing as above)
time_zone($time_zone)DateTime::TimeZone object, this method
sets the object's time zone. This effects the DateTime object
returned by parse_datetime
If the time zone is invalid, the method will croak or return undef
(depending on the value of the on_error parameter)
If successful this method returns the current time zone. (After processing as above)
This code emulates a $DateTime::Format::Strptime with
the on_error parameter equal to 'croak':
$Strp-pattern($pattern) or die $DateTime::Format::Strptime::errmsg>
There are no methods exported by default, however the following are available:
DateTime
object.
DateTime object this function will return a
formatted string.
The following tokens are allowed in the pattern string for strptime (parse_datetime):
%[number]N.
DateTime standard.
DateTime's
strftime() method, but can be passed to format_datetime().
Support for this module is provided via the datetime@perl.org email list. See http://lists.perl.org/ for more details.
Alternatively, log them via the CPAN RT system via the web or email:
bug-datetime-format-strptime@rt.cpan.org
This makes it much easier for me to track things and thus means your problem is less likely to be neglected.
Copyright © Rick Measham, 2003-2005. All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the licenses can be found in the LICENCE file included with this module.
Rick Measham <rickm@cpan.org>
datetime@perl.org mailing list.
the perl manpage, DateTime, the DateTime::TimeZone manpage
|
DateTime::Format::Strptime - Parse and format strp and strf time patterns |