dealing with time

Discussion in 'Perl Misc' started by Larry, Oct 26, 2008.

  1. Larry

    Larry Guest

    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
     
    Larry, Oct 26, 2008
    #1
    1. Advertising

  2. Larry

    Dr.Ruud Guest

    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

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Oct 26, 2008
    #2
    1. Advertising

  3. Larry

    Peter Wyzl Guest

    "Larry" <> wrote in message
    news:...
    > 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
     
    Peter Wyzl, Oct 26, 2008
    #3
  4. Larry wrote:
    >
    > 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
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Oct 26, 2008
    #4
  5. Larry

    Larry Guest

    In article <AHXMk.8581$>,
    "Peter Wyzl" <> wrote:

    > 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
     
    Larry, Oct 26, 2008
    #5
  6. On 2008-10-26 13:08, John W. Krahn <> wrote:
    > Larry wrote:
    >> 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;


    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
     
    Peter J. Holzer, Oct 26, 2008
    #6
  7. Larry <> wrote:
    >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
     
    Jürgen Exner, Oct 26, 2008
    #7
  8. Larry

    Larry Guest

    In article <>,
    J?rgen Exner <> wrote:

    > 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
     
    Larry, Oct 26, 2008
    #8
  9. Larry <> wrote:
    >In article <>,
    > J?rgen Exner <> wrote:
    >
    >> Not to make too fine a point but isn't that basic arithmetic at second
    >> grade level?

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


    Seconds modulo (60*60)?

    jue
     
    Jürgen Exner, Oct 26, 2008
    #9
  10. Larry

    Larry Guest

    In article <>,
    J?rgen Exner <> wrote:

    > 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
     
    Larry, Oct 26, 2008
    #10
  11. Larry <> wrote:
    >In article <>,
    > J?rgen Exner <> wrote:
    >
    >> Seconds modulo (60*60)?

    >
    >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
     
    Jürgen Exner, Oct 26, 2008
    #11
  12. On Sun, 26 Oct 2008 21:45:29 +0100,
    Larry <> wrote:
    > In article <>,
    > J?rgen Exner <> wrote:
    >
    >> Seconds modulo (60*60)?

    >
    > 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
    --
    |
    Martien Verbruggen | That's not a lie, it's a terminological
    | inexactitude.
    |
     
    Martien Verbruggen, Oct 26, 2008
    #12
  13. Peter J. Holzer wrote:
    > On 2008-10-26 13:08, John W. Krahn <> wrote:
    >> Larry wrote:
    >>> 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;

    >
    > 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
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Oct 26, 2008
    #13
  14. Larry

    Larry Guest

    In article <>,
    J?rgen Exner <> wrote:

    > 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
     
    Larry, Oct 26, 2008
    #14
  15. Larry <> wrote:
    >In article <>,
    > J?rgen Exner <> wrote:
    >
    >> 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.


    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
     
    Jürgen Exner, Oct 27, 2008
    #15
  16. Larry

    Doug Miller Guest

    In article <>, Larry <> wrote:
    >In article <>,
    > J?rgen Exner <> wrote:
    >
    >> 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!


    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?
     
    Doug Miller, Oct 27, 2008
    #16
  17. Larry

    Larry Guest

    In article <>,
    J?rgen Exner <> wrote:

    > 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
     
    Larry, Oct 27, 2008
    #17
  18. Larry <> wrote:
    >In article <>,
    > J?rgen Exner <> wrote:
    >
    >> 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


    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
     
    Jürgen Exner, Oct 27, 2008
    #18
  19. On 2008-10-26 23:20, John W. Krahn <> wrote:
    > Peter J. Holzer wrote:
    >> On 2008-10-26 13:08, John W. Krahn <> wrote:
    >>> Larry wrote:
    >>>> 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;

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


    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
     
    Peter J. Holzer, Oct 27, 2008
    #19
  20. Larry

    Larry Guest

    In article <>,
    J?rgen Exner <> wrote:

    > 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
     
    Larry, Oct 27, 2008
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. engsol
    Replies:
    2
    Views:
    981
    Dan Bishop
    Jan 26, 2004
  2. Replies:
    8
    Views:
    495
    Magnus Lycka
    Aug 5, 2005
  3. Peter Hansen
    Replies:
    0
    Views:
    727
    Peter Hansen
    Feb 22, 2006
  4. Peter Hansen
    Replies:
    0
    Views:
    621
    Peter Hansen
    Feb 22, 2006
  5. flamesrock
    Replies:
    8
    Views:
    517
    Hendrik van Rooyen
    Nov 24, 2006
Loading...

Share This Page