These Perl examples use Unix seconds for the portable baseline and show milliseconds only where the runtime exposes them naturally. The question to answer before copying a snippet is simple: what unit does the next API expect?

Get the current Unix time

my $seconds = time();

Convert a timestamp to UTC

my $seconds = 1700000000;
my @utc = gmtime($seconds);

Production note

time returns Unix seconds. Use gmtime for UTC output and localtime only when the local machine timezone is intentionally part of the result.

Keep the unit in the field name when a value crosses a boundary: createdAtSeconds and createdAtMs are longer than createdAt, but much less mysterious during an incident.

Frequent questions:

Q: Why does localtime() return a list instead of a string?
A: 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.
Q: How do I get sub-second timestamps in Perl?
A: 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.