Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Convert timestamp to epoch time and calculate hours plus conversion backwards

Posted on 2013-01-17
10
1,252 Views
Last Modified: 2013-01-18
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.
0
Comment
Question by:skahlert2010
  • 6
  • 3
10 Comments
 

Expert Comment

by:sarathy_sri
ID: 38786611
#! /usr/bin/perl
my @time = localtime(time);
print "@time\n";

print scalar localtime(time);
0
 
LVL 6

Expert Comment

by:deiaccord
ID: 38786638
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.

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');

Open in new window

0
 

Author Comment

by:skahlert2010
ID: 38786642
Hi sarathy_sri,

I only receive an error using that code ==> syntax error: `(' unexpected
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:skahlert2010
ID: 38786678
@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!
0
 
LVL 6

Accepted Solution

by:
deiaccord earned 500 total points
ID: 38787020
I believe Time::Local is standard with most perl distributions so you should be able to use that to get the difference in epoc seconds though it's not quite as flexible.

use Time::Local;

$datestring = "15.06.2005:15:30:30";
$datestring =~ m#(\d{2})\.(\d{2})\.(\d{4})\:(\d{2})\:(\d{2})\:(\d{2})#;
$time = timelocal ($6,$5,$4,$1,$2-1,$3-1900); # seconds, mins, hours, mday, month (0-11), year (difference from 1900)
print "Difference is " . (time - $time) . " seconds\n";

Open in new window

0
 

Author Comment

by:skahlert2010
ID: 38792056
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:

/tmp/epoch.pl $LS

Open in new window

while LS equals "15.06.2005:15:30:30"

In Perl:

$datestring = $LS;

Open in new window


Do you have a hint?
0
 

Author Comment

by:skahlert2010
ID: 38792098
It seems to work with:

$datestring = $ENV{'LS'};

Open in new window

0
 

Author Comment

by:skahlert2010
ID: 38792243
Well unfortunately this works only if I export the Variable in ksh but not within the script.
Thus, the question is still open.
0
 
LVL 6

Expert Comment

by:deiaccord
ID: 38792256
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];

Open in new window

0
 

Author Closing Comment

by:skahlert2010
ID: 38792467
Thanks a lot! Just what I needed! I appreciate your hint regarding passing the variable from ksh to perl with ARGV[n]!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question