What is the opposite of localtime function in Perl?

In Perl, localtime takes a Unix timestamp and returns back year / month / day / hour / min / sec, etc. I am looking for the opposite of localtime : I have parts and how do I create a unix timestamp from them.

+7
timestamp perl
source share
3 answers
+9
source share

You can use the timelocal function in the CPAN module Time :: Local .

NAME

Time :: Local - effective calculation of time from local and GMT

SYNTAX

 $time = timelocal($sec,$min,$hour,$mday,$mon,$year); $time = timegm($sec,$min,$hour,$mday,$mon,$year); 

DESCRIPTION

This module provides functions that are the inverted perl built-in functions of localtime () and gmtime (). They take the date as a six-element array and return the corresponding time (2) in seconds since the system era (Midnight, January 1, 1970 GMT on Unix, for example). This value can be positive or negative, although POSIX requires support only for positive values, so dates before the system era may not work on all operating systems.

It is worth paying particular attention to the expected ranges for the values ​​provided. The value for the day of the month is the actual day (i.e. 1..31), and the month is the number of months since January (0.11). This is consistent with the return values ​​from localtime () and gmtime ().

Note: POSIX :: mktime is just a wrapper around your C mktime() library. Time :: Local is a pure-Perl implementation and always returns results matching Perl localtime . In addition, Time :: Local offers gmtime , while mktime only works in local time. (Well, you can try changing $ENV{TZ} , but this does not work on some systems.)

+14
source share

DateTime for using CPAN. It also has many time manipulation / translation methods.

Just create a DateTime using your parts and call $ datetime-> formatter ("% s");

+7
source share

All Articles