How do I convert a date/time to epoch time (unix time/seconds since 1970) in Perl?

If you’re using the DateTime module, you can call the epoch() method on a DateTime object, since that’s what you think of as unix time.

Using DateTimes allows you to convert fairly easily from epoch, to date objects.

Alternativly, localtime and gmtime will convert an epoch into an array containing day month and year, and timelocal and timegm from the Time::Local module will do the opposite, converting an array of time elements (seconds, minutes, …, days, months etc.) into an epoch.

Leave a Comment