1825775 Members
1981 Online
109687 Solutions
New Discussion

Time calculation

 
Jose Mosquera
Honored Contributor

Time calculation

Any way to calculate the difference between a file date creation and current date? This difference will be able to being of hours or fraction.

Rgds.
4 REPLIES 4
Pete Randall
Outstanding Contributor

Re: Time calculation

Jose,

Search the forums for "date hammer" or just wait a while and Mr Hammer (otherwise known as A. Clay Stephenson) will be along to explain caljd.sh to you.

Pete

Pete
Jean-Louis Phelix
Honored Contributor

Re: Time calculation

Hi,

This very simple C program (not enough controls on args and return codes) could do it ...

Regards.

#include
#include
#include
#include

main (argc, argv)
int argc;
char *argv[];
{
time_t t_now;
int days,hours,minutes,seconds;
struct stat bufstat;

if (stat(argv[1], &bufstat) != 0)
{
perror("stat");
exit(1);
}
time(&t_now);
seconds=(int)(t_now - bufstat.st_mtime);
days=seconds / 86400;
seconds=seconds % 86400;
hours=seconds / 3600;
seconds=seconds % 3600;
minutes=seconds / 60;
seconds=seconds % 60;
printf("'%s' has been modified %d days %d hours %s minutes %d seconds ago\n",
argv[1], days, hours, minutes, seconds);
}
It works for me (© Bill McNAMARA ...)
Christian Gebhardt
Honored Contributor

Re: Time calculation

Hi
you can also use perl:

#!/opt/perl/bin/perl
use File::stat;

$st = stat($ARGV[0]) or die "No $file: $!";
$diff=time()-$st->mtime;
print "diff=$diff s\n";


to convert the seconds in Days/Hours/Minutes you can use the same syntax as Jean-Louis in his c-program

Chris
H.Merijn Brand (procura
Honored Contributor

Re: Time calculation

This is for modification time. -C and -A also available, but -M is probably what you want

a5:/tmp 112 > perl -e'printf"%8.2f %s\n",24*-M$_,$_ for@ARGV' xx*
165.22 xx
0.26 xx.A
2043.71 xx.L
2573.41 xx.c
2567.42 xx.c.idx
486.43 xx.pl
1325.97 xx.sql
0.29 xxfin
1847.94 xxx
a5:/tmp 113 > ll xx*
118 -rw-rw-rw- 1 merijn softwr 15360 Oct 30 17:19 xx
515 -rw-rw-rw- 1 gert softwr 4054 Nov 6 14:17 xx.A
290 -rw-rw-rw- 1 merijn softwr 8712 Aug 13 11:50 xx.L
209 -rw-rw-rw- 1 merijn softwr 227 Jul 22 10:08 xx.c
253 -rw-rw-rw- 1 merijn softwr 24576 Jul 22 16:07 xx.c.idx
171 -rw-rw-rw- 1 merijn softwr 221 Oct 17 09:07 xx.pl
192 -rw-rw-rw- 1 linda softwr 164 Sep 12 09:34 xx.sql
358 -rw-rw-rw- 1 henk softwr 16470 Nov 6 14:15 xxfin
276 -rw-rw-rw- 1 merijn softwr 49 Aug 21 15:36 xxx
a5:/tmp 114 >
Enjoy, Have FUN! H.Merijn