DateTim...trptime(3)
π NAME π
DateTime::Format::Strptime β bestie, this is the module that parses and formats strp and strf time patterns β¨
(also hi, you're reading DateTim...trptime(3) from the User Contributed Perl Documentation section, iykyk π«Ά)
πΈ VERSION πΈ
version 1.80 β periodt.
β¨ SYNOPSIS β¨
Okay so first things first, bestie, you gotta load it:
use DateTime::Format::Strptime;
Then make yourself a lil object, like this:
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
And when things go wrong (π it happens), you can make it croak:
# Croak when things go wrong:
my $strp = DateTime::Format::Strptime->new(
pattern => '%T',
locale => 'en_AU',
time_zone => 'Australia/Melbourne',
on_error => 'croak',
);
Or like, do literally anything else, slay:
# 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,
);
π¦ DESCRIPTION π¦
So this module implements most of strptime(3) β that's the POSIX function that's the reverse of strftime(3) β but for DateTime. The way that works is: strftime takes a DateTime and a pattern and hands you back a string, and strptime does the opposite, taking a string and a pattern and returning the DateTime object that goes with it. Iconic behaviour honestly π
π METHODS π
This class offers the following methods, no cap:
π DateTime::Format::Strptime->new(%args)
This method creates a new object (main character energy fr fr). It accepts the following arguments:
patternβ this is the pattern to use for parsing. It's required, so likeβ¦ don't forget it πstrictβ a boolean that 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. BUT π that also means this pattern will match any string that contains 14 or more numbers! This behaviour can be very surprising, lowkey.
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 it's at the beginning or end of a string, or when it's 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. Like, actually do it bestie π
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 azone_mapparameter 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. Your map, your rules π
localeβ the locale to use for objects returned from parsing.on_errorβ this can be one of'undef'(the string, not anundef),'croak', or a subroutine reference.'undef'β this is the default behavior. The module will returnundefon errors. The error can be accessed using the$object->errmsgmethod. 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'.
π§ $strptime->parse_datetime($string)
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 an empty list or undef, depending on the setting of on_error in the constructor.
π $strptime->format_datetime($datetime)
Given a DateTime object, this method returns a string formatted in the object's format. This method is synonymous with DateTime's strftime method π
π $strptime->locale
This method returns the locale passed to the object's constructor.
β $strptime->pattern
This method returns the pattern passed to the object's constructor.
π¦ $strptime->time_zone
This method returns the time zone passed to the object's constructor.
π $strptime->errmsg
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 π (very helpful, we love a debugging queen)
π EXPORTS π
These subs are available as optional exports, iykyk:
π« strptime( $strptime_pattern, $string )
Given a pattern and a string this function will return a new DateTime object.
π« strftime( $strftime_pattern, $datetime )
Given a pattern and a DateTime object this function will return a formatted string.
π₯ STRPTIME PATTERN TOKENS π₯
The following tokens are allowed in the pattern string for strptime (parse_datetime), and yes, there are a LOT of them, so buckle up bestie π
%%β the%character.%aor%Aβ the weekday name according to the given locale, in abbreviated form or the full name.%bor%Bor%hβ the month name according to the given locale, in abbreviated form or the full name.%cβ the datetime format according to the given locale.
Note that this format can change without warning in new versions ofDateTime::Locale. You should not use this pattern unless the string you are parsing was generated by using this pattern withDateTimeand you are sure that this string was generated with the same version ofDateTime::Localethat the parser is using. Fr fr, be careful π
%Cβ the century number (0-99).%dor%eβ the day of month (01-31). This will parse single digit numbers as well, cute β¨%Dβ equivalent to%m/%d/%y. (This is the American style date, very confusing to non-Americans, especially since%d/%m/%yis widely used in Europe. The ISO 8601 standard pattern is%F.)%Fβ equivalent to%Y-%m-%d. (This is the ISO style date)%gβ the year corresponding to the ISO week number, but without the century (0-99).%Gβ the 4-digit year corresponding to the ISO week number.%Hβ the hour (00-23). This will parse single digit numbers as well.%Iβ the hour on a 12-hour clock (1-12).%jβ the day number in the year (1-366).%mβ the month number (01-12). This will parse single digit numbers as well.%Mβ the minute (00-59). This will parse single digit numbers as well.%nβ arbitrary whitespace.%Nβ nanoseconds. For other sub-second values use%[number]N.%por%Pβ the equivalent of AM or PM according to the locale in use. SeeDateTime::Locale.%rβ equivalent to%I:%M:%S %p.%Rβ equivalent to%H:%M.%sβ number of seconds since the Epoch.%Sβ the second (0-60; 60 may occur for leap seconds. SeeDateTime::LeapSecond).%tβ arbitrary whitespace.%Tβ equivalent to%H:%M:%S.%Uβ the week number with Sunday the first day of the week (0-53). The first Sunday of January is the first day of week 1.%uβ the weekday number (1-7) with Monday = 1. This is theDateTimestandard.%wβ the weekday number (0-6) with Sunday = 0.%Wβ the week number with Monday the first day of the week (0-53). The first Monday of January is the first day of week 1.%xβ the date format according to the given locale.
Note that this format can change without warning in new versions ofDateTime::Locale. You should not use this pattern unless the string you are parsing was generated by using this pattern withDateTimeand you are sure that this string was generated with the same version ofDateTime::Localethat the parser is using. Same warning as before, bestie π
%Xβ the time format according to the given locale.
Note that this format can change without warning in new versions ofDateTime::Locale. You should not use this pattern unless the string you are parsing was generated by using this pattern withDateTimeand you are sure that this string was generated with the same version ofDateTime::Localethat the parser is using. Yes, again π
%yβ the year within century (0-99). When a century is not otherwise specified (with a value for%C), values in the range69-99refer to years in the twentieth century (1969-1999); values in the range00-68refer to years in the twenty-first century (2000-2068).%Yβ a 4-digit year, including century (for example,1991).%zβ an RFC-822/ISO 8601 standard time zone specification. (For example+1100) [See note below]%Zβ the timezone name. (For exampleEST-- which is ambiguous) [See note below]%Oβ this extended token allows the use of Olson Time Zone names to appear in parsed strings. NOTE: This pattern cannot be passed toDateTime'sstrftime()method, but can be passed toformat_datetime(). Special little guy π«Ά
πΈ AUTHOR EMERITUS πΈ
This module was created by Rick Measham. We stan a legend π
π SEE ALSO π
datetime@perl.org mailing list.
perl, DateTime, DateTime::TimeZone, DateTime::Locale
π BUGS π
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. Teamwork, bestie π
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>.
β SOURCE β
The source code repository for DateTime-Format-Strptime can be found at <https://github.com/houseabsolute/DateTime-Format-Strptime>.
πΈ DONATIONS πΈ
Okay bestie, real talk for a sec π«Ά β if you've been living under this module and you're feeling grateful, the author says you're welcome to send a lil "donation" via PayPal. He spends a whole lot of free time making free software, and honestly? Any support you feel like offering would be deeply appreciated. β¨
Now, important vibe check π β he is not saying you must do this for him to keep working on this particular software. He'll keep at it, just like he has in the past, for as long as it interests him. No pressure, no guilt trip, no hostage situation. π
Same energy for the reverse: a donation made this way will probably not make him work on this software much more β unless he gets so many donations that he can consider working on free software full time (and yes, let's all have a chuckle at that together π).
So if you're feeling generous, here's the whole thing: log into PayPal and send money to autarch@urth.org, or just use the button at <https://houseabsolute.com/foss-donations/>. π
π AUTHORS π
- Dave Rolsky <autarch@urth.org>
- Rick Measham <rickm@cpan.org>
π CONTRIBUTORS π
- Christian Hansen <chansen@cpan.org>
- D. Ilmari MannsΓ₯ker <ilmari.mannsaker@net-a-porter.com>
- gregor herrmann <gregoa@debian.org>
- key-amb <yasutake.kiyoshi@gmail.com>
- Mohammad S Anwar <mohammad.anwar@yahoo.com>
π COPYRIGHT AND LICENSE π
This software is Copyright (c) 2015 - 2025 by Dave Rolsky.
It's free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
The full text of the license lives in the LICENSE file that ships with this distribution β go read it if you're the type who actually reads licenses (iconic of you, honestly π§).
aaaand that's the whole page, bestie! π This one's perl v5.42.0, dated 2026-01-17, and it's the DateTim...trptime(3) page signing off. Go parse some dates, slay, periodt. πΈπ
π¦ where this page comes from
- fedora 44: perl-DateTime-Format-Strptime 1:1.80-2.fc44 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms