Perl time() function

In the Perl programming language has a built-in function time(). This function returns an integer — the number of seconds since the beginning of the era. Almost all modern systems the start time is zero seconds, zero minutes, zero hours on the first of January 1970 in the time zone UTC (1970-01-01T00:00:00Z). This number is often called timestamp.

Here is an example of using this feature:

▶ Run
#!/usr/bin/perl

print time();

At the time of this writing, the program brought the number 1577113985that corresponds to the date 2019-12-23T15:13:05Z.

Arguments

Function time() does not accept arguments. If you try to give her an argument, it will error:

▶ Run
#!/usr/bin/perl

print time(8);

Here is the output:

syntax error at script.pl line 3, near "(8"
Execution of script.pl aborted due to compilation errors.

Precision

Function time() (aka CORE::time()) returns the number of whole seconds since the beginning of the era. If accuracy to the second is not enough, you can use function Time::HiRes::time(), which returns the number of float number of seconds since the beginning of the era.

Example program which shows the use of two of these functions:

▶ Run
#!/usr/bin/perl

use feature qw(say);

use Time::HiRes;

say time();
say Time::HiRes::time();

Sample output:

1577116390
1577116390.36876

Official documentation

Here is the output of the command perldoc -f time:

       time    Returns the number of non-leap seconds since whatever time the
               system considers to be the epoch, suitable for feeding to
               "gmtime" and "localtime".  On most systems the epoch is
               00:00:00 UTC, January 1, 1970; a prominent exception being Mac
               OS Classic which uses 00:00:00, January 1, 1904 in the current
               local time zone for its epoch.

               For measuring time in better granularity than one second, use
               the Time::HiRes module from Perl 5.8 onwards (or from CPAN
               before then), or, if you have gettimeofday(2), you may be able
               to use the "syscall" interface of Perl.  See perlfaq8 for
               details.

               For date and time processing look at the many related modules
               on CPAN.  For a comprehensive date and time representation look
               at the DateTime module.

Other articles