Link to home
Create AccountLog in
Avatar of semIT
semIT

asked on

Convert dd-MMM-yy into seconds since epoch

I have been searching the web for functions that will allow me to change date into seconds since epoch. However I was unable to fine any.

How could I convert a string read in format dd-MMM-yy (e.g. 12-Jan-05)  into seconds since epoch for comparesens.
Avatar of manav_mathur
manav_mathur

use Date::Calc ;
Avatar of semIT

ASKER

don't have Date::Calc, and wan't be able to install it as its the unis unix machines.
if you have Date::Manip then this would do it for you

taken from cpan documentation for Date::Manip

$secs = Date_SecsSince1970($m,$d,$y,$h,$mn,$s);
Look to see if your perl has Time::Local available (it should, it has been a standard part of Perl5 for quite a long time - replacing the timelocal.pl routine that was distributed with earlier versions).

To use the timelocal method, you have to parse your time value into year, month, day, etc. to fill in the values of an array like the localtime function returns. Let us know if you need help with a regular expression to do this.
Avatar of semIT

ASKER

nope don't have Date::Manip either already tried this before.

yes it has got Time:Local function.

/(.*)-(.*)-(.*)/

$day = $1;
$mon = $2;
$year = $3;

so how will i be able to change this into seconds since epoch using Time:Local


Avatar of semIT

ASKER

^already converted month in format MMM (Jan) using hash map into numeric value (0-11).
ASKER CERTIFIED SOLUTION
Avatar of manav_mathur
manav_mathur

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Oops, Manav was a little ahead of me. And I think he's right that the year adjustment is merely to add 100 for two-digit years assumed to be in the range 2000-2038.
>$year+2000

perldoc Time::Local says that
<quote>
Strictly speaking, the year should also be specified in a form
consistent with localtime(), i.e. the offset from 1900. In order to make......
</quote>

Also, the author's string has first letter caps for month names.
Manav -

I see that the version of Time::Local on the machine I was using is years out of date! Let's try to gather the corrections into one response:

use Time::Local;

my %month = ( jan=>0, feb=>1, mar=>2, apr=>3, may=>4, jun=>5,
   jul=>6, aug=>7, sep=>8, oct=>9, nov=>10, dec=>11);

# your code here to put date string in $_

my ($day, $mon, $year) = /(.*)-(.*)-(.*)/;

$Seconds = timelocal( 0, 0, 0, $day, $month{lc $mon}, $year+100);
Avatar of semIT

ASKER

Thanks for the help.
>lc $mon
even more sturdy!!

also, my $Seconds  = .... if you want it strict vars compliant.
jmcg,

semIT has unneccesarily accepted my solution in his earlier post as it didnt answer his question(although it would if someone had Date::manip).

https://www.experts-exchange.com/questions/21343596/Compare-time-and-date.html

If you consider it so, you can unaccept and delete it.

Manav
 
   
Avatar of semIT

ASKER

manav, it did answer the question in that you found an easier solution if I had the possibility to use Manip.
I believe it is a valid good answer so thanks for that.
The other option using Date::Manip is:

my %month = ( jan=>0, feb=>1, mar=>2, apr=>3, may=>4, jun=>5,
   jul=>6, aug=>7, sep=>8, oct=>9, nov=>10, dec=>11);

# your code here to put date string in $_

my ($day, $mon, $year) = /(.*)-(.*)-(.*)/;
my $date = "$day-" . $month{$mon}" . "-$year";
my $secs_since_epoch = UnixDate($date, "%s");