Skip navigation.
Home
Your source for Perl tips, howto's, faq and tutorials
( categories: )

In most cases it's simpler to perform date calculations using epoch time (the numbers of seconds that have elapsed since a given date, in Unix is January 1, 1970).

If you have a date and want to convert it to epoch time, use the functions timelocal and timegm availabe in the module Time::Local.

These functions take as a parameter a date value (represented as a six-element array) and returns the epoch time.

#!/usr/bin/perl
use Time::Local
 
$time = timelocal($sec,$min,$hour,$mday,$mon,$year);
$time = timegm($sec,$min,$hour,$mday,$mon,$year);

Allowed range values

  • day of the month: 1..31
  • month: 0..11 (0 = January, 1 = February, etc)
  • year:
    • If value>999: the value is interpreted as the actual year
    • If value is 100..999: the value is interpreted as an offset from 1900 (for example, 199 is considered year 2099)
    • If value is 0..99: the value is interpreted based on the current year: if the current year is in the second half of the century (for example 1970), then the values in the range 50..99 are considered of be in the current century and the values in the range 0..49 are in the next century. If the current year is in the first half of the century (for example 2007), then the range 0..49 is interpreted as values in the current century and the range 50..99 is interpreted as years from the previous century.