Rand Stats

DateTime::strftime

zef:lizmat

Actions Status Actions Status Actions Status

NAME

DateTime::strftime - provide strftime() formatting for DateTime objects

SYNOPSIS

use DateTime::strftime;

say strftime(DateTime.now, '%c');  # default": EN
# Sun Jan 19 13:19:02 +0100 2025

say strftime(DateTime.now, '%c', "NL");
# zon 19 jan 13:19:02 +0100 2025

DESCRIPTION

The DateTime::strftime distribution provides a strftime subroutine that takes a DateTime object, a strftime format string (or its :text: equivalent), and an optional locale indicator (which defaults to the dynamic variable $*LOCALE-DATES or "EN").

It returns the representation of the DateTime object according to that format and locale.

FORMAT SPECIFICATION

The supported formats are a (large) subset of strftime. The E and O modifiers are not supported.

Since the strftime format codes are rather cryptic (although very widely in use), one can also use an alternate :text: format. So instead of the rather cryptic "%c", one can also use ":datetime:".

Code:text:Value
%awkdnameabbreviated weekday name
%Aweekdaynamefull weekday name
%bmnthnameabbreviated month name
%Bmonthnamefull month name
%cdatetimepreferred date/time representation
%Ccenturycentury number (year div 100)
%d0dayday of month ("01" .. "31")
%Dusadateshort for: %m/%d/%y
%edayday of month (" 1" .. "31")
%fsame as %M
%Fisodateshort for: %Y/%m/%d
%gweekYYYY of this week ("00" .. "99")
%GweekYYYYfull year of this week
%hsame as %b
%H0hour24-hour hour ("00" .. "23")
%I0amhour12-hour hour ("01" .. "12")
%jyeardayday of the year ("001" .. "366")
%khour24-hour hour (" 1" .. "23")
%lamhour12-hour hour (" 1" .. "12")
%Lsame as %Y
%mmonthmonth ("01" .. "12")
%Mminuteminute ("00" .. "59")
%nnewline"\n"
%pAMPM"AM" | "PM"
%Pampm"am" | "pm"
%ramtimeshort for: %I:%M:%S %p
%RHHMMshort for: %H:%M
%sepochseconds since epoch (midnight 1970-01-01 UTC)
%Ssecondsecond ("00" .. "59")
%ttab"\t"
%THHMMSSshort for: %H:%M:%S
%uweekdayweekday (1 .. 7)
%UweekSunweek number (first Sunday) ("00" .. "53")
%vDD-MON-YYYYshort for: %e-%b-%Y
%VweekISOweek number (ISO 8601) ("00" .. "53")
%w0weekdayweekday (0 .. 6)
%WweekMonweek number (first Monday) ("00" .. "53")
%xdatepreferred date representation
%Xtimepreferred time representation
%yYYyear without century ("00" .. "99")
%Yyearyear (yyyy)
%ztzoffsetnumeric timezone (±HHMM)
%Ztimezonetimezone (no name: %z)
%+unixdateshort for: %a %b %e %T %Z %G
%%percent"%"

DEFAULT LOCALIZATION

my $*LOCALE-DATES = "NL";
say strftime($dt, ':weekdayname:');        # zondag
say strftime($dt, ':weekdayname:', "EN");  # Sunday

The $*LOCALE-DATES dynamic variable can be set with the string of the desired localization, or with an instantiated Locale::Dates object. It will then affect any call to strftime that does not have an explicit localization specified.

LEXICAL REFINEMENT

use DateTime::strftime :refine;

say DateTime.now.strftime('%c');  # default": EN
# Sun Jan 19 13:19:02 +0100 2025

say DateTime.now.strftime(':datetime:', "NL");
# zon 19 jan 13:19:02 +0100 2025

You can also use DateTime::strftime with the :refine parameter. This will add a strftime method to the DateTime class in the lexical scope in which the use statement is located. This allows one to not to have to change existing code using the DateTime class, while still having the added functionality of a DateTime.strftime method.

AUTHOR

Elizabeth Mattijsen liz@raku.rocks

Source can be located at: https://github.com/lizmat/DateTime-strftime . Comments and Pull Requests are welcome.

If you like this module, or what I'm doing more generally, committing to a small sponsorship would mean a great deal to me!

COPYRIGHT AND LICENSE

Copyright 2025 Elizabeth Mattijsen

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.