skahlert2010
asked on
Convert timestamp to epoch time and calculate hours plus conversion backwards
Dear experts, after my last problem I stumbled upon another one.
I need to convert "dd.mm.yyyy:hh24:mi:ss" to epoch time and add some hours to it (e.g. 4h). This output has to be compared to the current time/date.
I need to do so in ksh or perl. Any hints are welcome.
I need to convert "dd.mm.yyyy:hh24:mi:ss" to epoch time and add some hours to it (e.g. 4h). This output has to be compared to the current time/date.
I need to do so in ksh or perl. Any hints are welcome.
You should be able to use the perl DateTime module to do what you want
Here is a quick example that takes a date string in the format you specify and prints the difference in hours from the current datetime.
Here is a quick example that takes a date string in the format you specify and prints the difference in hours from the current datetime.
use DateTime;
$datestring = "15.06.2005:15:30:30";
$datestring =~ m#(\d{2})\.(\d{2})\.(\d{4})\:(\d{2})\:(\d{2})\:(\d{2})#;
my $dt = DateTime -> new ( year => $3, month => $2, day => $1,
hour => $4, minute => $5, second => $6);
$dt->add(hours => 4);
my $cmp = DateTime->now()-$dt;
print $cmp->in_units('hours');
ASKER
Hi sarathy_sri,
I only receive an error using that code ==> syntax error: `(' unexpected
I only receive an error using that code ==> syntax error: `(' unexpected
ASKER
@deiaccord.
I fear perl is not the best soltion as the Perl DateTime Package is not installed on my Solaris machine. The solution needs to be universal in order to be applied on various hosts.
Any idea how to solve it differently? In Ksh maybe?
Otherwise you solution looks promising without being able to test it! Sorry!
I fear perl is not the best soltion as the Perl DateTime Package is not installed on my Solaris machine. The solution needs to be universal in order to be applied on various hosts.
Any idea how to solve it differently? In Ksh maybe?
Otherwise you solution looks promising without being able to test it! Sorry!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi deiaaccord,
thanks for your solution. I compiled a small perl script using your code and invoke it from within my shell script. However, I cannot pass a parameter to perl in order to bind $datestring.
In ksh:
In Perl:
Do you have a hint?
thanks for your solution. I compiled a small perl script using your code and invoke it from within my shell script. However, I cannot pass a parameter to perl in order to bind $datestring.
In ksh:
/tmp/epoch.pl $LS
while LS equals "15.06.2005:15:30:30"In Perl:
$datestring = $LS;
Do you have a hint?
ASKER
It seems to work with:
$datestring = $ENV{'LS'};
ASKER
Well unfortunately this works only if I export the Variable in ksh but not within the script.
Thus, the question is still open.
Thus, the question is still open.
Glad you got it working, you could have also set it with the first argument to the script if you want to make it a more re-usable script
$datestring = $ARGV[0];
ASKER
Thanks a lot! Just what I needed! I appreciate your hint regarding passing the variable from ksh to perl with ARGV[n]!
my @time = localtime(time);
print "@time\n";
print scalar localtime(time);