1. Perl
  2. Module
  3. here

Time::Local - Convert time information to epoch seconds

The Time::Local module is the opposite of localtime function and gmtime function. It is a module for converting date and time.

Use the timelocal function of the Time::Local module to convert dates and times (local time) to epoch seconds.

use Time::Local 'timelocal';
my $sec_from_epoch = timelocal ($sec, $min, $hour, $mday, $month, $year);

To convert the date and time(Coordinated Universal Time) to epoch seconds, use the timegm function of the Time::Local module.

my $sec_from_epoch = timegm ($sec, $min, $hour, $mday, $month, $year);

For general information about dates and times, please refer to the following.

Time::Local example

This is an example of the Time::Local module.

This is an example to convert date and time(local time) to epoch seconds.

use strict;
use warnings;

use Time::Local 'timelocal';

print "(1) Convert date and time(local time) to epoch seconds.\n";
my $sec = 10;
my $min = 30;
my $hour = 12;
my $mday = 1;

# January is 0, February is 1, so subtract 1.
my $month = 10 -1;

# Perl treats the year as the number of days from 1900, so subtract 1900
my $year = 2000 - 1900;

my $sec_from_epoch = timelocal ($sec, $min, $hour, $mday, $month, $year);
print $sec_from_epoch;

Convert date and time(local time) to epoch seconds

my $sec = 10;
my $min = 30;
my $hour = 12;
my $mday = 1;

# January is 0, February is 1, so subtract 1.
my $month = 10 -1;

# Perl treats the year as the number of days from 1900, so subtract 1900
my $year = 2000 - 1900;

my $sec_from_epoch = timelocal ($sec, $min, $hour, $mday, $month, $year);

To convert a date and time(local time) to epoch seconds, use the timelocal function of the Time::Local module.

The timelocal function does the exact opposite of the built-in function localtime. The arguments passed to the timelocal function must be the same as the conversion rules performed by localtime.

The month starts at 0, so you need to use the actual month minus 1. The year is from 1900, so you need to subtract 1900.

To convert the date and time(Coordinated Universal Time) to epoch seconds, use the timegm function of the Time::Local module.

Example using timegm function

use strict;
use warnings;

use Time::Local 'timegm';

print "(1) Convert date and time(Coordinated Universal Time) to Epoch seconds.\n";
my $sec = 10;
my $min = 30;
my $hour = 12;
my $mday = 1;

# January is 0, February is 1, so subtract 1.
my $month = 10 - 1;

# Perl treats the year as the number of days from 1900, so subtract 1900
my $year = 2000 - 1900;

my $sec_from_epoch = timegm ($sec, $min, $hour, $mday, $month, $year);
print $sec_from_epoch;

Convert date and time(Coordinated Universal Time) to epoch seconds.

use Time::Local 'timegm';

my $sec = 10;
my $min = 30;
my $hour = 12;
my $mday = 1;

# January is 0, February is 1, so subtract 1.
my $month = 10 - 1;

# Perl treats the year as the number of days from 1900, so subtract 1900
my $year = 2000 - 1900;

my $sec_from_epoch = timegm ($sec, $min, $hour, $mday, $month, $year);

To convert the date and time(Coordinated Universal Time) to epoch seconds, use the timegm function.

The timegm function does the exact opposite of the built-in function gmtime. The arguments passed to the timegm function must be the same as the conversion rules performed by gmtime.

The month starts at 0, so you need to use the actual month minus 1. The year is from 1900, so you need to subtract 1900.

You can also use the timelocal function of the Time::Local module to convert dates and times (local time) to epoch seconds.

Related Informatrion