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.
How could I convert a string read in format dd-MMM-yy (e.g. 12-Jan-05) into seconds since epoch for comparesens.
use Date::Calc ;
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);
taken from cpan documentation for Date::Manip
$secs = Date_SecsSince1970($m,$d,$
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.
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.
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
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
ASKER
^already converted month in format MMM (Jan) using hash map into numeric value (0-11).
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
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.
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);
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);
ASKER
Thanks for the help.
>lc $mon
even more sturdy!!
also, my $Seconds = .... if you want it strict vars compliant.
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
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
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.
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");
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");