Perl Snippets

Perl Unix Timestamp Snippets

Perl examples for getting the current Unix timestamp with time(), formatting dates with POSIX::strftime(), and converting calendar dates back to epoch seconds with Time::Local.

Current Unix timestamp (seconds)
my $ts = time();
Current Unix timestamp (milliseconds)
use Time::HiRes qw(time);
my $ms = int(time() * 1000);
Format timestamp as UTC string
use POSIX qw(strftime);
my $str = strftime("%Y-%m-%d %H:%M:%S", gmtime(1700000000));
# "2023-11-15 06:13:20"
Format timestamp as local time string
use POSIX qw(strftime);
my $str = strftime("%Y-%m-%d %H:%M:%S", localtime(1700000000));
Timestamp from date components (UTC)
use Time::Local qw(timegm);
# timegm(sec, min, hour, mday, mon, year)
# mon is 0-indexed: 10 = November
my $ts = timegm(20, 13, 6, 15, 10, 2023);
Timestamp from date components (local)
use Time::Local qw(timelocal);
my $ts = timelocal(20, 13, 6, 15, 10, 2023);
Parse ISO date string to timestamp
use Time::Piece;
my $t = Time::Piece->strptime("2023-11-15T06:13:20Z", "%Y-%m-%dT%H:%M:%SZ");
my $ts = $t->epoch;

Perl timestamp basics

Perl's built-in time() function returns the current Unix timestamp as an integer number of seconds since the epoch. The localtime() and gmtime() built-ins convert a timestamp to a list of calendar values (second, minute, hour, day, month, year, weekday, yearday, DST flag). The month from gmtime/localtime is 0-indexed (0 = January) and the year is years since 1900.

Formatting and parsing

The POSIX module ships with all standard Perl installations and provides strftime(), which formats a timestamp with the same format codes as C. For sub-second precision, Time::HiRes (also in core since Perl 5.8) overrides time() and sleep() to accept floating-point values. Time::Piece (core since Perl 5.10) adds object-oriented parsing via strptime().

Perl production notes

Perl's time() returns a native integer, so it is not subject to the Year 2038 overflow on 64-bit Perl builds. On 32-bit Perl builds, time_t is still 32-bit; check $Config{ivsize} from the Config module. Use timegm() from Time::Local when building UTC timestamps from calendar components, not timelocal(), which interprets arguments as local time.

  • Time::HiRes, POSIX, and Time::Local are all core modules and need no installation
  • The month argument to timegm() and timelocal() is 0-indexed: pass 10 for November
  • The year argument is years since 1900: pass 123 for 2023
  • Use Time::Piece->strptime for parsing arbitrary date strings instead of manual regex splitting

FAQ

Why does localtime() return a list instead of a string?
In list context, localtime($ts) returns (sec, min, hour, mday, mon, year, wday, yday, isdst). In scalar context it returns a ctime-style string. Use POSIX::strftime to produce a custom formatted string from the list.
How do I get sub-second timestamps in Perl?
Use Time::HiRes: 'use Time::HiRes qw(time); my $ms = int(time() * 1000);'. The module is a core Perl module so no CPAN install is needed.