DateTime::Format::Strptime(3pm) User Contributed Perl Documentation DateTime::Format::Strptime(3pm)

DateTime::Format::Strptime - Parse and format strp and strf time patterns

version 1.79

    use DateTime::Format::Strptime;
    my $strp = DateTime::Format::Strptime->new(
        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 = DateTime::Format::Strptime->new(
        pattern   => '%T',
        locale    => 'en_AU',
        time_zone => 'Australia/Melbourne',
        on_error  => 'croak',
    );
    # Do something else when things go wrong:
    my $strp = DateTime::Format::Strptime->new(
        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.

This class offers the following methods.

This methods creates a new object. It accepts the following arguments:

  • pattern

    This is the pattern to use for parsing. This is required.

  • strict

    This is a boolean which disables or enables strict matching mode.

    By default, this module turns your pattern into a regex that will match anywhere in a string. So given the pattern "%Y%m%d%H%M%S" it will match a string like 20161214233712. However, this also means that a this pattern will match any string that contains 14 or more numbers! This behavior can be very surprising.

    If you enable strict mode, then the generated regex is wrapped in boundary checks of the form "/(?:\A|\b)...(?:\b|\z_/)". These checks ensure that the pattern will only match when at the beginning or end of a string, or when it is separated by other text with a word boundary ("\w" versus "\W").

    By default, strict mode is off. This is done for backwards compatibility. Future releases may turn it on by default, as it produces less surprising behavior in many cases.

    Because the default may change in the future, you are strongly encouraged to explicitly set this when constructing all "DateTime::Format::Strptime" objects.

  • time_zone

    The default time zone to use for objects returned from parsing.

  • zone_map

    Some time zone abbreviations are ambiguous (e.g. PST, EST, EDT). By default, the parser will die when it parses an ambiguous abbreviation. You may specify a "zone_map" parameter as a hashref to map zone abbreviations however you like:

        zone_map => { PST => '-0800', EST => '-0600' }
        

    Note that you can also override non-ambiguous mappings if you want to as well.

  • locale

    The locale to use for objects returned from parsing.

  • on_error

    This can be one of 'undef' (the string, not an "undef"), 'croak', or a subroutine reference.

  • 'undef'

    This is the default behavior. The module will return "undef" on errors. The error can be accessed using the "$object->errmsg" 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.

  • 'croak'

    The module will croak with an error message on errors.

  • sub{...} or \&subname

    When given a code ref, the module will call that sub on errors. The sub receives two parameters: the object and the error message.

    If your sub does not die, then the formatter will continue on as if "on_error" was 'undef'.

Given a string in the pattern specified in the constructor, this method will return a new "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.

Given a "DateTime" object, this methods returns a string formatted in the object's format. This method is synonymous with "DateTime"'s strftime method.

This method returns the locale passed to the object's constructor.

This method returns the pattern passed to the object's constructor.

This method returns the time zone passed to the object's constructor.

If the on_error behavior of the object is 'undef', you can retrieve error messages with this method so you can work out why things went wrong.

These subs are available as optional exports.

Given a pattern and a string this function will return a new "DateTime" object.

Given a pattern and a "DateTime" object this function will return a formatted string.

The following tokens are allowed in the pattern string for strptime (parse_datetime):

This module was created by Rick Measham.

"datetime@perl.org" mailing list.

http://datetime.perl.org/

perl, DateTime, DateTime::TimeZone, DateTime::Locale

Please report any bugs or feature requests to "bug-datetime-format-strptime@rt.cpan.org", or through the web interface at <http://rt.cpan.org>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

Bugs may be submitted at <https://github.com/houseabsolute/DateTime-Format-Strptime/issues>.

There is a mailing list available for users of this distribution, <mailto:datetime@perl.org>.

I am also usually active on IRC as 'autarch' on "irc://irc.perl.org".

The source code repository for DateTime-Format-Strptime can be found at <https://github.com/houseabsolute/DateTime-Format-Strptime>.

If you'd like to thank me for the work I've done on this module, please consider making a "donation" to me via PayPal. I spend a lot of free time creating free software, and would appreciate any support you'd care to offer.

Please note that I am not suggesting that you must do this in order for me to continue working on this particular software. I will continue to do so, inasmuch as I have in the past, for as long as it interests me.

Similarly, a donation made in this way will probably not make me work on this software much more, unless I get so many donations that I can consider working on free software full time (let's all have a chuckle at that together).

To donate, log into PayPal and send money to autarch@urth.org, or use the button at <https://www.urth.org/fs-donation.html>.

This software is Copyright (c) 2015 - 2021 by Dave Rolsky.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

The full text of the license can be found in the LICENSE file included with this distribution.

2021-08-22 perl v5.32.1