For Developers

Unix Timestamp in Perl

How to get the current timestamp, convert it to a date, and convert a date back to a timestamp in Perl — plus the legacy localtime() array that's still the top search result.

my $ts = time();
use POSIX qw(strftime);
my $ts = 1750000000;
print strftime("%Y-%m-%d %H:%M:%S", gmtime($ts));
use Time::Local qw(timegm);
my $ts = timegm(0, 0, 12, 15, 5, 2025); # sec,min,hour,mday,mon(0-11),year

The Perl-specific pitfall

localtime() and gmtime() both return a 9-element list where the year is years since 1900 and the month is zero-indexed — so June 2025 shows up as month 5, year 125, in the raw list. Code that forgets the + 1900 or the off-by-one month has been a running joke in Perl codebases for decades, and it's exactly the kind of bug that only surfaces once a year swaps or a report runs in a new month. Time::Piece's object-oriented interface avoids this by giving you a real ->year and ->mon that read correctly, and is worth pulling in for anything beyond a one-off script.

Have a timestamp right now?

Paste it into the Timestamp ⇄ Date Converter to check it instantly, in any timezone, without writing any code. Working across several languages on the same project? The full Epoch Time in Programming Languages guide has all of them side by side for quick comparison.

Related

Related tools & reading