- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Need to find the time difference between the file ...
Operating System - HP-UX
1820696
Members
2713
Online
109627
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-14-2008 09:53 AM
тАО07-14-2008 09:53 AM
Need to find the time difference between the file tmp_071508 cretaed and the current time
I has create a script, which will create the file with name tmp_($date +%H%M%S).I want to create the script to know the time difference, when the file was created and the current time.
"Plans Never Fails, People Fails To Plan"
- Tags:
- date arithmetic
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-14-2008 10:05 AM
тАО07-14-2008 10:05 AM
Re: Need to find the time difference between the file tmp_071508 cretaed and the current time
Hi Manish:
# perl -le 'die unless -f $ARGV[0];print +(time-(stat($ARGV[0]))[9])," seconds ago"' file
# touch /tmp/me
# perl -le 'die unless -f $ARGV[0];print +(time-(stat($ARGV[0]))[9])," seconds ago"' /tmp/me
2 seconds ago
Regards!
...JRF...
# perl -le 'die unless -f $ARGV[0];print +(time-(stat($ARGV[0]))[9])," seconds ago"' file
# touch /tmp/me
# perl -le 'die unless -f $ARGV[0];print +(time-(stat($ARGV[0]))[9])," seconds ago"' /tmp/me
2 seconds ago
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-14-2008 10:36 AM
тАО07-14-2008 10:36 AM
Re: Need to find the time difference between the file tmp_071508 cretaed and the current time
The solution JRF proposes is probably fine.
But pleae realize that it relies on stat[9] = mtime = "last modify time in seconds since the epoch, " to be close to the creation time (which Unix does not record).
It does NOT use the time in the name.
So this only works fine for marker file which is created and not touched anymore.
A more exact, but much more tedious, solution would be to take the name apart into time component, stuff in time array and get the seconds since epoch.
For example:
#---- test.pl ------
$_=shift;
m/(\d+)(\d\d)(\d\d)$/;
$then = $3 + $2*60 + $1*3600;
@now=localtime;
$now = $now[0] + $now[1]*60 + $now[2]*3600;
print $now - $then, " Seconds\n";
#-------------------
Or a little cleaner still
#---- test.pl ------
use Time::Local;
$now=time;
$_=shift;
m/(\d+)(\d\d)(\d\d)$/;
@then=localtime($now);
@then[0..2]=($3,$2,$1);
$then=timelocal(@then);
print $now - $then, " Seconds\n";
#------------------------
$ cat > tmp_$(date +%H%M%S)
test
$ sleep 10
$ ls tmp_*
tmp_142711
$ perl test.pl tmp_142711
11 Seconds
Of course, the design of the file name will cause this only to work 'today'.
If you ever need to cross a day boundary, then you may want to add something like
$elapsed = $now - $then;
$elapsed +=86400 if $elapsed < 0;
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-14-2008 01:33 PM
тАО07-14-2008 01:33 PM
Re: Need to find the time difference between the file tmp_071508 cretaed and the current time
Hi (again) Manish:
We could also invoke the Date::Calc module to do:
# cat .fileage
#!/usr/bin/perl
use strict;
use warnings;
use Date::Calc qw( Delta_DHMS );
die "Usage: $0 file\n" unless @ARGV && -f $ARGV[0];
my $file = shift;
my ( $fsec, $fmin, $fhrs, $fday, $fmon, $fyrs )
= ( localtime( ( stat($file) )[9] ) )[ 0 .. 5 ];
my ( $csec, $cmin, $chrs, $cday, $cmon, $cyrs ) = (localtime)[ 0 .. 5 ];
$fmon++;
$fyrs += 1900;
$cmon++;
$cyrs += 1900;
my ( $Dd, $Dh, $Dm, $Ds ) = Delta_DHMS(
$fyrs, $fmon, $fday, $fhrs, $fmin, $fsec,
$cyrs, $cmon, $cday, $chrs, $cmin, $csec
);
printf "'%s' is %d day%s %d hour%s %d minute%s %d second%s old\n",
$file, $Dd, $Dd == 1 ? "" : "s", $Dh, $Dh == 1 ? "" : "s", $Dm,
$Dm == 1 ? "" : "s", $Ds, $Ds == 1 ? "" : "s";
1;
# ls -l /stand/vmunix
-rwxr-xr-x 1 root sys 18949752 Mar 3 15:16 /stand/vmunix
# ./fileage /stand/vmunix
'/stand/vmunix' is 133 days 2 hours 15 minutes 14 seconds old
Regards!
...JRF...
We could also invoke the Date::Calc module to do:
# cat .fileage
#!/usr/bin/perl
use strict;
use warnings;
use Date::Calc qw( Delta_DHMS );
die "Usage: $0 file\n" unless @ARGV && -f $ARGV[0];
my $file = shift;
my ( $fsec, $fmin, $fhrs, $fday, $fmon, $fyrs )
= ( localtime( ( stat($file) )[9] ) )[ 0 .. 5 ];
my ( $csec, $cmin, $chrs, $cday, $cmon, $cyrs ) = (localtime)[ 0 .. 5 ];
$fmon++;
$fyrs += 1900;
$cmon++;
$cyrs += 1900;
my ( $Dd, $Dh, $Dm, $Ds ) = Delta_DHMS(
$fyrs, $fmon, $fday, $fhrs, $fmin, $fsec,
$cyrs, $cmon, $cday, $chrs, $cmin, $csec
);
printf "'%s' is %d day%s %d hour%s %d minute%s %d second%s old\n",
$file, $Dd, $Dd == 1 ? "" : "s", $Dh, $Dh == 1 ? "" : "s", $Dm,
$Dm == 1 ? "" : "s", $Ds, $Ds == 1 ? "" : "s";
1;
# ls -l /stand/vmunix
-rwxr-xr-x 1 root sys 18949752 Mar 3 15:16 /stand/vmunix
# ./fileage /stand/vmunix
'/stand/vmunix' is 133 days 2 hours 15 minutes 14 seconds old
Regards!
...JRF...
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP