Epoch and Date Time Conversion in Perl
Perl is a well known programming language. Perl has many built in date time functions and modules for date time manipulations. We can easily get current epoch and can convert epoch to human readable date with different options.
Here we will explain Perl Date Time functions to get current epoch or unix timestamp, convert timestamp to date and convert date to epoch or unix timestamp.
Get Current Epoch or Unix Timestamp in Perl
We can use the time()
function from Perl to get current epoch time or unix timestamp.
$currentTimestamp = time();
Convert from Epoch or Unix Timestamp to Human Readable Date in Perl
We can use the localtime()
function from Perl to convert epoch or unix timestamp to human readable date.
$currentTimestamp = time(); $date = localtime($currentTimestamp);
Convert from Human Readable Date to Epoch or Unix Timestamp in Perl
We can use the Time::Local
module to convert the human readable date to epoch or unix timestamp.
use Time::Local; timelocal($second,$minute,$hour,$day,$month-1,$year
Format Date and Time in Perl
We can use localtime()
Perl function to get the current date time. We can use the printf()
function to format the date and time according to requirement.
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(); printf("%02d:%02d:%02d", $hour, $min, $sec);
More about date time in Perl