DateTimeX::Easy(3pm)
π NAME π
okay bestie, meet DateTimeX::Easy (you'll see it in the manual as DateTimeX::Easy(3pm)) β it parses a date/time string using the best method available. that's it. that's the whole personality. π
β¨ SYNOPSIS β¨
# Make DateTimeX object for "now":
my $dt = DateTimeX::Easy->new("today");
# Same thing:
my $dt = DateTimeX::Easy->new("now");
# Uses ::F::Natural's coolness (similar in capability to Date::Manip)
my $dt = DateTimeX::Easy->new("last monday");
# ... but in 1969:
my $dt = DateTimeX::Easy->new("last monday", year => 1969);
# ... at the 100th nanosecond:
my $dt = DateTimeX::Easy->new("last monday", year => 1969, nanosecond => 100);
# ... in US/Eastern: (This will NOT do a timezone conversion)
my $dt = DateTimeX::Easy->new("last monday", year => 1969, nanosecond => 100, timezone => "US/Eastern");
# This WILL do a proper timezone conversion:
my $dt = DateTimeX::Easy->new("last monday", year => 1969, nanosecond => 100, timezone => "US/Pacific");
$dt->set_time_zone("US/Eastern");
# Custom DateTimeX ability:
my $dt = DateTimeX::Easy->new("last second of last month");
$dt = DateTimeX::Easy->new("last second of first month of last year");
$dt = DateTimeX::Easy->new("last second of first month of 2000");
like. look at that. it just works. π«Ά
π¦ DESCRIPTION π¦
DateTimeX::Easy makes DateTime object creation quick and easy. no cap. it hands off the heavy lifting of parsing to a whole roster of DateTime::Format packages, and then adds some custom tweaks of its own to smooth out the rough edges β mostly the drama around timezone detection and selection, because timezones are the group chat that never stops typing. π
π PARSING π
as of right now, DateTimeX::Easy tries to parse your input in this exact order:
- DateTime β is the input a DateTime object?
- ICal β was DT::F::ICal able to parse the input?
- DateParse β was DT::F::DateParse able to parse the input?
- a caveat, bestie: it actually uses a modified version of DateParse in order to avoid DateParse's default timezone selection.
- Natural β was DT::F::Natural able to parse the input?
- since this module barfs pretty loudly on strange input, it uses a silent
$SIG{__WARN__}to hide errors. π
- since this module barfs pretty loudly on strange input, it uses a silent
- Flexible β was DT::F::Flexible able to parse the input?
- this step also looks at the string to see if there is any timezone information at the end.
- DateManip β was DT::F::DateManip able to parse the input?
- DateManip isn't very nice with preserving the input timezone, but it's here as a last resort.
and then, floating in the middle of the source like a lost lil ghost, there's this stray fragment β no explanation, it's just there: last second of first month of year of 2005"
DateTimeX::Easy also provides additional parsing and transformation for input like:
"first day of last month"
"last day of last month"
"last day of this month"
"last day of next month"
"last second of first month of last year"
"ending day of month of 2007-10-02"
"last second of first month of year of 2005"
"last second of last month of year of 2005"
"beginning day of month of 2007-10-02"
"last month of year of 2007"
it will look at each sequence of "<first|last> of <period>" and do ->add, ->subtract, and ->truncate operations on the parsed DateTime object. slay. π
also β and this is the important part, bestie β it's best to be as explicit as possible. the following will work:
"last month of 2007"
"last second of last month of 2005"
"beginning day of 2007-10-02"
this won't, though:
"last day of 2007"
you'll have to do this instead:
"last day of year of 2007"
the reason is that the date portion is opaque to the parser. it doesn't know whether it has "2007" or "2007-10" or "now" as the last input. to fix this, you can give a hint to the parser, like "<period> of <date/time>" (as in "year of 2007" above). considerate of it, honestly.
WARNING: this feature is still somewhat new, so there may be bugs lurking about. please forward failing tests/scenarios. π do it for the culture.
π₯ METHODS π₯
the whole squad, and yes they all do the same vibe:
DateTimeX::Easy->new( ... )DateTimeX::Easy->parse( ... )DateTimeX::Easy->parse_date( ... )DateTimeX::Easy->parse_datetime( ... )DateTimeX::Easy->date( ... )DateTimeX::Easy->datetime( ... )DateTimeX::Easy->new_date( ... )DateTimeX::Easy->new_datetime( ... )
parse the given date/time specification using ::F::Flexible or ::F::Natural and use the result to create a DateTime object. returns a DateTime object. iconic behaviour fr fr.
you can pass the following in:
parse # The string or DateTime object to parse.
year # A year to override the result of parsing
month # A month to override the result of parsing
day # A day to override the result of parsing
hour # A hour to override the result of parsing
minute # A minute to override the result of parsing
second # A second to override the result of parsing
truncate # A truncation parameter (e.g. year, day, month, week, etc.)
time_zone # - Can be:
timezone # * A timezone (e.g. US/Pacific, UTC, etc.)
tz # * A DateTime special timezone (e.g. floating, local)
#
# - If neither "tz", "timezone", nor "time_zone" is set, then it'll use whatever is parsed.
# - If no timezone is parsed, then the default is floating.
# - If the given timezone is different from the parsed timezone,
# then a time conversion will take place (unless "soft_time_zone_conversion" is set).
# - Either "time_zone", "timezone", "tz" will work (in that order), with "time_zone" having highest precedence
# - See below for examples!
soft_time_zone_conversion # Set this flag to 1 if you don't want the time to change when a given timezone is
# different from a parsed timezone. For example, "10:00 UTC" soft converted to
# America/Los_Angeles would be "10:00 America/Los_Angeles".
time_zone_if_floating # The value of this option should be a valid timezone. If this option is set, then a DateTime object
# with a floating timezone has it's timezone set to the value.
default_time_zone # Same as "time_zone_if_floating"
ambiguous # Set this flag to 0 if you want to disallow ambiguous input like:
# "last day of 2007" or "first minute of April"
# This will require you to specify them as "last day of year of 2007" and "first minute of month of April"
# instead. This flag is 1 (false) by default.
... and anything else that you want to pass to the DateTime->new constructor
if truncate is specified, then DateTime->truncate will be run after object creation. π
furthermore, you can simply pass the value for parse as the first positional argument of the DateTimeX::Easy call, e.g.:
# This:
DateTimeX::Easy->new("today", year => 2008, truncate => "hour");
# ... is the same as this:
DateTimeX::Easy->new(parse => "today", year => 2008, truncate => "hour");
π°οΈ timezone processing
timezone processing can be a little complicated. here are some examples:
DateTimeX::Easy->parse("today"); # Will use a floating timezone
DateTimeX::Easy->parse("2007-07-01 10:32:10"); # Will ALSO use a floating timezone
DateTimeX::Easy->parse("2007-07-01 10:32:10 US/Eastern"); # Will use US/Eastern as a timezone
DateTimeX::Easy->parse("2007-07-01 10:32:10"); # Will use the floating timezone
DateTimeX::Easy->parse("2007-07-01 10:32:10", time_zone_if_floating => "local"); # Will use the local timezone
DateTimeX::Easy->parse("2007-07-01 10:32:10 UTC", time_zone => "US/Pacific"); # Will convert from UTC to US/Pacific
my $dt = DateTime->now->set_time_zone("US/Eastern");
DateTimeX::Easy->parse($dt); # Will use US/Eastern as the timezone
DateTimeX::Easy->parse($dt, time_zone => "floating"); # Will use a floating timezone
DateTimeX::Easy->parse($dt, time_zone => "US/Pacific", soft_time_zone_conversion => 1);
# Will use US/Pacific as the timezone with NO conversion
# For example, "22:00 US/Eastern" will become "22:00 America/Los_Angeles"
DateTimeX::Easy->parse($dt)->set_time_zone("US/Pacific"); # Will use US/Pacific as the timezone WITH conversion
# For example, "22:00 US/Eastern" will become "19:00 America/Los_Angeles"
DateTimeX::Easy->parse($dt, time_zone => "US/Pacific"); # Will ALSO use US/Pacific as the timezone WITH conversion
see. told you it was complicated. but we got through it together. π«Άβ¨
πΈ EXPORT πΈ
parse( ... )parse_date( ... )parse_datetime( ... )date( ... )datetime( ... )new_date( ... )new_datetime( ... )
same syntax as above. see above for more information. π
π MOTIVATION π
although the author really likes using DateTime for date/time handling, he was often frustrated by its inability to parse even the simplest of date/time strings. there does exist a wide variety of DateTime::Format::* modules, but they all have different interfaces and different capabilities. coming from a Date::Manip background, he wanted something that gave him the power of ParseDate while still returning a DateTime object. most importantly, he wanted explicit control of the timezone setting at every step of the way. DateTimeX::Easy is the result. and honestly? main character energy. β¨
π THANKS π
Dave Rolsky and crew for writing DateTime. π«Ά absolute legends.
π SEE ALSO π
DateTimeDateTime::Format::NaturalDateTime::Format::FlexibleDateTime::Format::DateManipDateTime::Format::ParseDateDateTime::Format::ICalDate::Manip
π AUTHOR π
Robert Krimen <rokr@cpan.org>
π COPYRIGHT AND LICENSE π
this software is copyright (c) 2022 by Robert Krimen and others, see the git log.
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
okay that's the whole page bestie, you're officially fluent π this was DateTimeX::Easy(3pm) from the User Contributed Perl Documentation, as shipped with perl v5.40.0, dated 2025-01-11. go forth and parse. π¦β
π¦ where this page comes from
- debian trixie: libdatetimex-easy-perl 0.092-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libdatetimex-easy-perl 0.092-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdatetimex-easy-perl 0.092-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms