dealing with time

L

Larry

Hi,

I'm using File::stat to get the last mod time of a file:

my $sb = stat $file;
my $lmod = $sb->mtime;

Now I would like know how many minutes ago (from now) was that file
modified. Can it actually be done?

thanks
 
D

Dr.Ruud

Larry schreef:
my $sb = stat $file;
my $lmod = $sb->mtime;

Now I would like know how many minutes ago (from now) was that file
modified. Can it actually be done?

for f in stat time; do perldoc -f $f; done
 
P

Peter Wyzl

Larry said:
Hi,

I'm using File::stat to get the last mod time of a file:

my $sb = stat $file;
my $lmod = $sb->mtime;

Now I would like know how many minutes ago (from now) was that file
modified. Can it actually be done?

Since both time now and time thern are given in seconds (since the epoch
which varies by system), a bit of basic mathematics derives the result.

P
 
J

John W. Krahn

Larry said:
I'm using File::stat to get the last mod time of a file:

my $sb = stat $file;
my $lmod = $sb->mtime;

Now I would like know how many minutes ago (from now) was that file
modified. Can it actually be done?

my $minutes_last_modified = 1440 * -M $file;



John
 
L

Larry

Peter Wyzl said:
Since both time now and time thern are given in seconds (since the epoch
which varies by system), a bit of basic mathematics derives the result.

in fact I got this:

use File::stat;

my $file = "Mondo.pdf";
my $sb = stat $file;
my $lmod = $sb->mtime;
my $tnow = time;

my $diff = $tnow - $lmod;

Is there anyway to sprintf the $diff value to rappresent: hours, mins,
secs ??

thanks
 
P

Peter J. Holzer

my $minutes_last_modified = 1440 * -M $file;

Nope. -M computes the difference between the modification time and the
time the program was started. There may be a considerable difference
between "when the program was started" and "now".

hp
 
J

Jürgen Exner

Larry said:
Is there anyway to sprintf the $diff value to rappresent: hours, mins,
secs ??

Not to make too fine a point but isn't that basic arithmetic at second
grade level?
Of course, there is always Time::Format, too.

jue
 
L

Larry

J?rgen Exner said:
Not to make too fine a point but isn't that basic arithmetic at second
grade level?

well, I actually am doing the following:

use File::stat;

my $file = "time.pl";
my $sb = stat $file;
my $lmod = $sb->mtime;
my $tnow = time;

my $diff = $tnow - $lmod;

my ($sec,$min,$hour) = (localtime($diff))[0,1,2];

my $time = sprintf( "%1d hr %1d mins %1d secs", $hour - 1, $min, $sec );

Now, I would like to keep showing hours like 25,26 etc... (not days) any
ideas?

thanks
 
L

Larry

J?rgen Exner said:
Seconds modulo (60*60)?

I really don't get that. What I'm trying to do is:

59 secs

1 min 0 secs

59 min 0 secs

1 hr 0 min 0 secs

24 hr 0 min 0 secs

25 hr 0 min 0 secs
 
J

Jürgen Exner

Larry said:
I really don't get that. What I'm trying to do is:

Sorry, that was very confusing, indeed.

Try
use warnings; use strict;
my $seconds = 123456;

my $s = $seconds % 60;
my $m = (($seconds - $s) % (60*60))/60;
my $h = ($seconds - $m*60 - $s) / (60*60);
print "$seconds seconds are $h hours, $m minutes, and $s
seconds\n";

jue
 
M

Martien Verbruggen

I really don't get that. What I'm trying to do is:

The modulus operator is %. It does an integer division of two numbers,
and gives you the remainder. Together with division, this can be used to
do what you want (check the perlop documentation for more detailed
information on the operator as implemented in Perl, and Google for more
information about the mathematics.).

my $seconds = int $diff % 60;
my $minutes = int ($diff/60) % 60;
my $hours = int $diff/3600;

printf "$file: %02d:%02d:%02d\n", $hours, $minutes, $seconds;

note that 3600 comes from 60 * 60. I use int() here to make sure that
the three variables contain whole numbers. The %d in the printf format
would also get rid of any fractional part, but I think this is neater.

You should remember this technique. It's rather fundamental when you
need to work with numbers.

Martien
 
J

John W. Krahn

Peter said:
Nope. -M computes the difference between the modification time and the
time the program was started. There may be a considerable difference
between "when the program was started" and "now".

$^T = time;
my $minutes_last_modified = 1440 * -M $file;


John
 
L

Larry

J?rgen Exner said:
print "$seconds seconds are $h hours, $m minutes, and $s
seconds\n";

the code worked great...now I would love to show hours if $h has value >
0, minutes if $m > 0.

the thing is if I get "1 hour, 0 min, 10 secs" min won't show up...this
is what I'm stuck at!

just out of interest; I tried to run the code you posted with $seconds =
time; it gave me: 340295 hours, 34 minutes, and 20 seconds is the the
actaul hours since 1 jan 1970?

thanks
 
J

Jürgen Exner

Larry said:
the code worked great...now I would love to show hours if $h has value >
0, minutes if $m > 0.

Oh, please.......

print "$seconds seconds are ";
print "$h hours, " if $h;
print "$m minutes, " if $m;
print "$s seconds" if $s;
print "\n";

If you need more fancyful formatting, like no plural 's' for single
values or correct punctuation if one part is omitted, then you need to
be more elaborate in your if conditions and differentiate between more
cases.
just out of interest; I tried to run the code you posted with $seconds =
time; it gave me: 340295 hours, 34 minutes, and 20 seconds is the the
actaul hours since 1 jan 1970?

Let's see: 340295 / 24 / 365 = 38.8 years (minus a few leap days).
Well, late 2008 - 38.8 years looks like early 1970 to me.

jue
 
D

Doug Miller

the code worked great...now I would love to show hours if $h has value >
0, minutes if $m > 0.

the thing is if I get "1 hour, 0 min, 10 secs" min won't show up...this
is what I'm stuck at!

Are you familiar with the 'if' command??
just out of interest; I tried to run the code you posted with $seconds =
time; it gave me: 340295 hours, 34 minutes, and 20 seconds is the the
actaul hours since 1 jan 1970?

Are you honestly unable to figure that out on your own?
 
L

Larry

J?rgen Exner said:
Oh, please.......

print "$seconds seconds are ";
print "$h hours, " if $h;
print "$m minutes, " if $m;
print "$s seconds" if $s;
print "\n";

I got this. But what If I have 2hr 0 min 10 secs? it'll show up 2hr 10
secs. Anyway, I'll make do with it. Thanks
 
J

Jürgen Exner

Larry said:
I got this. But what If I have 2hr 0 min 10 secs? it'll show up 2hr 10
secs. Anyway, I'll make do with it. Thanks

In your last posting you wrote:
<quote>
now I would love to show hours if $h has value >
0, minutes if $m > 0.
<\quote>
That is exactly, what the code above does.

Before that you were asking for
<quote>
I would like to keep showing hours like 25,26 etc... (not days)
</quote>

Before that
<quote>
Is there anyway to sprintf the $diff value to rappresent: hours, mins,
secs ??

And before that
<quote>
I would like know how many minutes ago was that file modified
</quote>

Do you think you could make up you rmind and tell us under
_EXACTLY_WHICH_ CONDITIONS_ you want to print _EXACTLY_ WHAT_?

Chasing a running target is no fun, you know....

jue
 
P

Peter J. Holzer

$^T = time;
my $minutes_last_modified = 1440 * -M $file;

Do you want to win some kind of obfuscation contest?

What's wrong with

my $sb = stat $file;
my $minutes_last_modified = (time - $sb->mtime) / 60;

?

hp
 
L

Larry

J?rgen Exner said:
Chasing a running target is no fun, you know....

yeah, I know. The is that your code MAY show: "X hour X secs" if minutes
is 0 or it may show "X hour X min" if seconds is 0. Also, it may show "X
min X secs" if hours is 0 which is OK! How can I get the code show value
nontheless its 0 value based on the value next to it? "X hour 0 min X
secs"

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top