Time::Piece(3pm)
๐ฐ๏ธ NAME โ Time::Piece - Object Oriented time objects
Hiii bestie ๐ this is the page for Time::Piece(3pm) from the Perl Programmers Reference Guide, and it's literally the object-oriented time objects module. Iconic behavior only. โจ
โจ SYNOPSIS โจ
use Time::Piece;
my $t = localtime;
print "Time is $t\n";
print "Year is ", $t->year, "\n";
๐ฆ DESCRIPTION ๐ฆ
Okay so this module replaces the standard localtime and gmtime functions with implementations that return objects. The way it does this is backwards compatible, so if you use localtime/gmtime the way perlfunc documents them, you'll still get what you expect. No cap. ๐ซถ
The module actually implements most of an interface described by Larry Wall on the perl5-porters mailing list right here: https://www.nntp.perl.org/group/perl.perl5.porters/2000/01/msg5283.html
๐ซถ USAGE ๐ซถ
After you import this module, when you use localtime or gmtime in scalar context, you don't get an ordinary scalar string representing the date and time anymore. Instead you get a Time::Piece object, and its stringification just so happens to produce the same effect as the localtime and gmtime functions. It's giving glow-up but make it backwards compatible. ๐
There's also a new() constructor, which is the same as localtime(), except when you pass it a Time::Piece object โ in that case it's a copy constructor. Obsessed.
The following methods are available on the object:
$t->secโ also available as$t->second$t->minโ also available as$t->minute$t->hourโ 24 hour$t->mdayโ also available as$t->day_of_month$t->monโ 1 = January$t->_monโ 0 = January$t->monnameโ Feb$t->monthโ same as$t->monname$t->fullmonthโ February$t->yearโ based at 0 (year 0 AD is, of course 1 BC)$t->_yearโ year minus 1900$t->yyโ 2 digit year$t->wdayโ 1 = Sunday$t->_wdayโ 0 = Sunday$t->day_of_weekโ 0 = Sunday$t->wdaynameโ Tue$t->dayโ same aswdayname$t->fulldayโ Tuesday$t->ydayโ also available as$t->day_of_year, 0 = Jan 01$t->isdstโ also available as$t->daylight_savings$t->hmsโ 12:34:56$t->hms(".")โ 12.34.56$t->timeโ same as$t->hms$t->ymdโ 2000-02-29$t->dateโ same as$t->ymd$t->mdyโ 02-29-2000$t->mdy("/")โ 02/29/2000$t->dmyโ 29-02-2000$t->dmy(".")โ 29.02.2000$t->datetimeโ 2000-02-29T12:34:56 (ISO 8601)$t->cdateโ Tue Feb 29 12:34:56 2000"$t"โ same as$t->cdate$t->epochโ seconds since the epoch$t->tzoffsetโ timezone offset in aTime::Secondsobject$t->julian_dayโ number of days since Julian period began$t->mjdโ modified Julian date (JD-2400000.5 days)$t->weekโ week number (ISO 8601)$t->is_leap_yearโ true if it's a leap year$t->month_last_dayโ 28-31$t->time_separator($s)โ set the default separator (default":")$t->date_separator($s)โ set the default separator (default"-")$t->day_list(@days)โ set the default weekdays$t->mon_list(@days)โ set the default months$t->strftime(FORMAT)โ same asPOSIX::strftime(without the overhead of the full POSIX extension)$t->strftime()โ"Tue, 29 Feb 2000 12:34:56 GMT"Time::Piece->strptime(STRING, FORMAT)โ seestrptimeman page. Creates a newTime::Pieceobject
Note that localtime and gmtime are not listed above. If called as methods on a Time::Piece object, they act as constructors, returning a new Time::Piece object for the current time. In other words: they're not useful as methods. ๐
๐ธ Local Locales ๐ธ
Both wdayname (day) and monname (month) allow passing in a list to use to index the name of the days against. This can be useful if you need to implement some form of localisation without actually installing or using locales. Note that this is a global override and will affect all Time::Piece instances. ๐
my @days = qw( Dimanche Lundi Merdi Mercredi Jeudi Vendredi Samedi );
my $french_day = localtime->day(@days);
These settings can be overridden globally too:
Time::Piece::day_list(@days);
Or for months:
Time::Piece::mon_list(@months);
And locally for months:
print localtime->month(@months);
Or to populate with your current system locale call:
Time::Piece->use_locale();
๐งฎ Date Calculations ๐งฎ
It's possible to use simple addition and subtraction of objects:
use Time::Seconds;
my $seconds = $t1 - $t2;
$t1 += ONE_DAY; # add 1 day (constant from Time::Seconds)
The following are valid ($t1 and $t2 are Time::Piece objects):
$t1 - $t2; # returns Time::Seconds object
$t1 - 42; # returns Time::Piece object
$t1 + 533; # returns Time::Piece object
However adding a Time::Piece object to another Time::Piece object will cause a runtime error. Not me finding out the hard way ๐ญ.
Note that the first of the above returns a Time::Seconds object, so while examining the object will print the number of seconds (because of the overloading), you can also get the number of minutes, hours, days, weeks and years in that delta, using the Time::Seconds API.
In addition to adding seconds, there are two APIs for adding months and years:
$t = $t->add_months(6);
$t = $t->add_years(5);
The months and years can be negative for subtractions. Note that there is some "strange" behaviour when adding and subtracting months at the ends of months. Generally when the resulting month is shorter than the starting month then the number of overlap days is added. For example subtracting a month from 2008-03-31 will not result in 2008-02-31 as this is an impossible date. Instead you will get 2008-03-02. This appears to be consistent with other date manipulation tools. Slay for consistency, honestly. ๐
โ๏ธ Truncation โ๏ธ
Calling the truncate method returns a copy of the object but with the time truncated to the start of the supplied unit.
$t = $t->truncate(to => 'day');
This example will set the time to midnight on the same date which $t had previously. Allowed values for the "to" parameter are: "year", "quarter", "month", "day", "hour", "minute" and "second".
โ๏ธ Date Comparisons โ๏ธ
Date comparisons are also possible, using the full suite of <, >, <=, >=, <=>, == and !=. Vibe check your dates, bestie. ๐ฆ
๐ Date Parsing ๐
Time::Piece has a built-in strptime() function (from FreeBSD), allowing you incredibly flexible date parsing routines. For example:
my $t = Time::Piece->strptime("Sunday 3rd Nov, 1943",
"%A %drd %b, %Y");
print $t->strftime("%a, %d %b %Y");
Outputs:
Wed, 03 Nov 1943
(see, it's even smart enough to fix my obvious date bug) ๐ญโจ
For more information see man strptime, which should be on all unix systems.
Alternatively look here: http://www.unix.com/man-page/FreeBSD/3/strftime/
๐ CAVEAT %A, %a, %B, %b, and friends ๐
Time::Piece::strptime by default can only parse American English date names. Meanwhile, Time::Piece->strftime() will return date names that use the current configured system locale. This means dates returned by strftime might not be able to be parsed by strptime. This is the default behavior and can be overridden by calling Time::Piece->use_locale(). This builds a list of the current locale's day and month names which strptime will use to parse with. Note this is a global override and will affect all Time::Piece instances.
For instance with a German locale:
localtime->day_list();
Returns
( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' )
While:
Time::Piece->use_locale();
localtime->day_list();
Returns
( 'So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa' )
๐๏ธ YYYY-MM-DDThh:mm:ss โฐ
The ISO 8601 standard defines the date format to be YYYY-MM-DD, and the time format to be hh:mm:ss (24 hour clock), and if combined, they should be concatenated with date first and with a capital 'T' in front of the time.
๐ข Week Number ๐ข
The week number may be an unknown concept to some readers. The ISO 8601 standard defines that weeks begin on a Monday and week 1 of the year is the week that includes both January 4th and the first Thursday of the year. In other words, if the first Monday of January is the 2nd, 3rd, or 4th, the preceding days of the January are part of the last week of the preceding year. Week numbers range from 1 to 53.
๐ Global Overriding ๐
Finally, it's possible to override localtime and gmtime everywhere, by including the ':override' tag in the import list:
use Time::Piece ':override';
๐จ CAVEATS ๐จ
๐งต Setting $ENV{TZ} in Threads on Win32 ๐ช
Note that when using perl in the default build configuration on Win32 (specifically, when perl is built with PERL_IMPLICIT_SYS), each perl interpreter maintains its own copy of the environment and only the main interpreter will update the process environment seen by strftime.
Therefore, if you make changes to $ENV{TZ} from inside a thread other than the main thread then those changes will not be seen by strftime if you subsequently call that with the %Z formatting code. You must change $ENV{TZ} in the main thread to have the desired effect in this case (and you must also call _tzset() in the main thread to register the environment change). No cap, thread drama is real. ๐
Furthermore, remember that this caveat also applies to fork(), which is emulated by threads on Win32.
โณ Use of epoch seconds โณ
This module internally uses the epoch seconds system that is provided via the perl time() function and supported by gmtime() and localtime().
If your perl does not support times larger than 2^31 seconds then this module is likely to fail at processing dates beyond the year 2038. There are moves afoot to fix that in perl. Alternatively use 64 bit perl. Or if none of those are options, use the DateTime module which has support for years well into the future and past. Future-proofing is main character energy, honestly. ๐
Also, the internal representation of Time::Piece->strftime deviates from the standard POSIX implementation in that it uses the epoch (instead of separate year, month, day parts). This change was added in version 1.30. If you must have a more traditional strftime (which will normally never calculate day light saving times correctly), you can pass the date parts from Time::Piece into the strftime function provided by the POSIX module (see strftime in POSIX ).
๐ AUTHOR ๐
Okay so this iconic module was brought to you by:
- Matt Sergeant, like, reachable at
matt@sergeant.orgโ slay. - Jarkko Hietaniemi, at
jhi@iki.fiโ he did his thing while creatingTime::Piecefor core perl, no big deal, just main character energy fr fr ๐ซถ
๐ COPYRIGHT AND LICENSE ๐
So legally speaking (and yes bestie, we gotta say it):
Copyright 2001, Larry Wall.
This module is free software, and you may distribute it under the same terms as Perl. Periodt. ๐
๐ฆ SEE ALSO ๐ฆ
If you're lowkey obsessed with calendars now (and honestly? same), go check out the excellent Calendar FAQ at:
http://www.tondering.dk/claus/calendar.html
It's giving โจknowledgeโจ โ iykyk.
๐ BUGS ๐
Not gonna lie, the test harness leaves much to be desired ๐ญ like it's fine but it's not fine-fine. Patches welcome though!! Come through and help a girl out ๐๐ซถ
And that's the whole page, bestie โ Time::Piece(3pm) from perl v5.42.3, dated 2026-11-03. Go forth and format your timestamps like the icon you are ๐๐
๐ฆ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms