Date Subtraction with time.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date Subtraction with time.
# 1  
Old 06-10-2010
Date Subtraction with time.

HI gurus...
I have a PERL file that help me extract the date and time
of the file.
The format of this is: yyyymmddhhmmss.
Example:
20100430070935 (April 30 2010 07:09:35)

How can i subtract the acquired time from system's time..??
The answer i need it in seconds.

I have been through the forum i found date calculation. But
subtraction of date with time. I couldn't get any idea.
Any kind of help is appreciated.
Thank you in advance.
# 2  
Old 06-10-2010
Use the Time::Local module (included in the core distribution), eg
Code:
$ cat time.pl
#!/usr/bin/perl

use strict;
use warnings;

use Time::Local;

my $tstr = "20100430070935";

my @tval = ( $tstr =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/ );

my $time =
  timelocal( $tval[5], $tval[4], $tval[3], $tval[2], $tval[1] - 1, $tval[0] );
print "Seconds: ", ( time() - $time ), "\n";
$ LANG=C date; perl time.pl
Thu Jun 10 14:07:46 CEST 2010
Seconds: 3567491

# 3  
Old 06-11-2010
Hi puldi,
Thanx for the quick reply but, I'm getting an error:
Code:
Can't locate warnings.pm in @INC (@INC contains: /usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503 /usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_perl/5.005 .) at time.pl line 4.
BEGIN failed--compilation aborted at time.pl line 4.


Last edited by pludi; 06-11-2010 at 04:10 AM.. Reason: corrected code tag
# 4  
Old 06-11-2010
You're using Perl 5.5.3 (which is more than 11 years old, btw), but the "warnings" pragma was only introduced with 5.6. You can try running Perl using the -w switch, but unlike the pragma this will enable warnings globally. But in this case you can just remove the
Code:
use warnings;

line.
This User Gave Thanks to pludi For This Post:
# 5  
Old 06-11-2010
Thanx piuldi it worked...
If possible for u can u explain ur script..??
I tried but jus got a bit confused...

Neways thanx again...
# 6  
Old 06-11-2010
Code:
#!/usr/bin/perl

use strict;
use warnings;

use Time::Local;

# Input time
my $tstr = "20100430070935";

# Run a regexp match on the input, and save the matches (capturing groups)
# into an array, instead of $1, $2, ...
my @tval = ( $tstr =~ /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/ );

# timelocal (from Time::Local) takes the array and returns the seconds since
# the epoch (usually 01.01.1970 00:00:00 GMT) corresponding to the date.
# For more details see perldoc Time::Local
my $time =
  timelocal( $tval[5], $tval[4], $tval[3], $tval[2], $tval[1] - 1, $tval[0] );

# Take the current seconds since the epoch, and substract the seconds for the
# given date -> seconds elapsed since
# See perldoc -f time
print "Seconds: ", ( time() - $time ), "\n";

This User Gave Thanks to pludi For This Post:
# 7  
Old 06-11-2010
hey thanx pludi, that was a great help for me to start.
Will work on it...
Thanx again...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Date Subtraction

Hello All, I am a newbie to unix shell scripting and need to write a script that displays the difference between two variables that stores date value. For example, F1=`ls -ltr file1* | tail -1 |tr -s ' ' |cut -d' ' -f6,7,8` F2=`ls -ltr file2* | tail -1 |tr -s ' ' |cut -d' ' -f6,7,8` F1... (3 Replies)
Discussion started by: priyaa2010
3 Replies

2. Shell Programming and Scripting

Need Help on date subtraction

I have dates as follows in a file 20121029135649 20121029135721 20121030091540 20121030093420 20121030094340 20121030095427 20121030095856 20121030100104 20121030100251 All these dates are in sorted order. I need to find out the difference between the dates as follows 2nd row... (6 Replies)
Discussion started by: meetsriharsha
6 Replies

3. Solaris

modifying date and time and time zone on solaris 5.10 with (redundant server) veritas

I have a cluster of two Solaris server (veritas cluster). one working and the other is standby I am going to change the date on them , and am looking for a secure solution as it is giving an important service. my opinion is that the active one doesn't need to be restarted (if I don't change the... (1 Reply)
Discussion started by: barry1946
1 Replies

4. UNIX for Dummies Questions & Answers

Converting string date time to unix time in AWK

I'd like to convert a date string in the form of sun aug 19 09:03:10 EDT 2012, to unixtime timestamp using awk. I tried This is how each line of the file looks like, different date and time in this format Sun Aug 19 08:33:45 EDT 2012, user1(108.6.217.236) all: test on the 17th ... (2 Replies)
Discussion started by: bkkid
2 Replies

5. Shell Programming and Scripting

Time subtraction using perl

var=50 perl -le "print scalar localtime (time() - "$var"*60);" The above does subtraction of time,but i need the time format to be date +%Y%m%d%H%M%S. How can i achive this (6 Replies)
Discussion started by: tomjones
6 Replies

6. Shell Programming and Scripting

Date subtraction

hi, i set up a script on my server to do a particular task once files from an external system are ftpd in the format compaq_20100110 (YYDDMM). Interestingly, the source of ftp is sending the files in the format e.g 20100109 i.e. previous date and for some reason this fails.kindly see my script... (2 Replies)
Discussion started by: bigtejus
2 Replies

7. Shell Programming and Scripting

Convert Epoch Time to Standard Date and Time & Vice Versa

Hi guys, I know that this topic has been discuss numerous times, and I have search the net and this forum for it. However, non able to address the problem I faced so far. I am on Solaris Platform and unable to install additional packages like the GNU date and gawk to make use of their... (5 Replies)
Discussion started by: DrivesMeCrazy
5 Replies

8. Linux

date subtraction(URGENT)

Hi all, I need the date subtraction fuctionality using shell commands. For example: date1:Wed Apr 5 08:35:21 IST 2006 date2:Tue Apr 4 10:35:44 IST 2006 I need the date subtraction result like " 22 hours 23 seconds". Please guide me to complete this task. Can you please help me ASAP. ... (3 Replies)
Discussion started by: uday123
3 Replies

9. UNIX for Dummies Questions & Answers

subtraction from date

hi gurus! i realize that my question shows my stupidness, but i need your help! i have: s_date=`date +%m-%d-%Y_%I%p` variable and i need the same, but minus one hour.. what i made: s_date=time(`date +%m-%d-%Y_%I%p`) - 3600 but i'm getting: daily_exports.sh: line 20: syntax error near... (4 Replies)
Discussion started by: MarGur
4 Replies

10. Shell Programming and Scripting

Date Subtraction in KSH

I need to figure out the numeric representation of the previous month (in an automated monthly-running script) so that I may append it to a filename. I have tried statements such as variable=`date +%m -1` (and several variations) but with no success. I have also tried simply assigning the value... (3 Replies)
Discussion started by: mharley
3 Replies
Login or Register to Ask a Question