http://www.perlmonks.org?node_id=479785

swaroop has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I need to find the time difference between creation time and modified time of the file in 'DAY : HOUR : MIN : SEC' format.

Please help.

Thanks,
Swaroop

Replies are listed 'Best First'.
Re: time difference
by mifflin (Curate) on Aug 01, 2005 at 04:44 UTC
    assuming you had two different epoch times $t1 and $t2 you could...
    $diff = $t2 - $t1; $ss = $diff % 60; $diff = ($diff - $ss) / 60; $mi = $diff % 60; $hh = ($diff - $mi) / 60; sprintf("%d:%02d:%02d" , $hh, $mi, $ss);
    This gives hours:minutes:seconds. I'll leave it as an exercise for you to add the days calculation.
Re: time difference
by cfreak (Chaplain) on Aug 01, 2005 at 04:47 UTC
Re: time difference
by anonymized user 468275 (Curate) on Aug 01, 2005 at 11:04 UTC
    Perl does not have an independent means of determining creation time (ctime is the inode change time and often confuses people).

    But if you can tell us the type of file system the file resides on, i.e. NTFS, FAT16, EXT2, EXT3, etc., then we can respond to this question more effectively.

    One world, one people

Re: time difference
by rob_au (Abbot) on Aug 01, 2005 at 04:47 UTC
    Untested code follows ... Update: Bugger, caught out on inode change/creation time - Remember that (stat _)[10] (or -C _) is not file creation time, but rather inode change time.
    sub diff { return -1 unless -e $_[0]; my @result = (); my $diff = ((stat _)[10]) - ((stat _)[9]); foreach my $time ((86400, 3600, 60, 1)) { push @result, $diff % $time; $diff -= $result[-1]; } return @result; }

     

    perl -le "print unpack'N', pack'B32', '00000000000000000000001000000000'"

      Is there a reliable way to get the true file creation time given that -C (and stat 10) is not it?
        Not in Unix. The Unix founders argue (and I agree) that "creation time" is at best an odd concept. But you didn't say on what platform, so you may be able to get a "fake" creation time proposed by your platform.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: time difference
by BrowserUk (Patriarch) on Aug 01, 2005 at 04:51 UTC

    Ignore this. It doesn't work! Please downvote.

    See POSIX for strftime and perlfunc for localtime, -C & -M

    ### BAD CODE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! use POSIX qw[ strftime ]; print strftime "%d : %H : %M : %S\n", localtime( ( -C 'file' ) - -M _ +); 01 : 00 : 00 : 47

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      See the update to my own node in this thread - -C is inode change time, not (universally) file creation time - The inode creation time may roughly equate with the file creation time or that of a permission change for the file.

       

      perl -le "print unpack'N', pack'B32', '00000000000000000000001000000000'"

      A simple question but somehow it seems to be confusing the heck out of me!

      I tried your code BrowserUK on UNIX and things did not come out well so I switched to Windows XP PRO.

      Here is what I did

      (Note: I changed your 'file' to 'junk')

      1. I created a file with some content

      2. After a few minutes i added some more content

      3. I ran the timestamp program

      I see funny results. No matter the file, i get the same result

      . Can you throw some light?

      C:\>echo hi > junk C:\>dir junk 7/31/2005 10:25 PM 5 junk 1 File(s) 5 bytes 0 Dir(s) 15,975,989,248 bytes free C:\>echo hi2 >> junk C:\>dir junk 07/31/2005 10:28 PM 11 junk 1 File(s) 11 bytes 0 Dir(s) 15,975,989,248 bytes free C:\>perl timestamp 1 : 16 : 00 : 00

      I am on activstate

      C:\>perl -v This is perl, v5.6.1 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail)

      When I run the program on UNIX i get  31 : 18 : 00 : 00 as output

      Any thoughts?

      thanks

      SK

        Two problems.

      • First I forgot that -C -A & -M give their results in days rather than seconds like every other timestamp.
      • Second, the days will always be +1 (because days of the month start from 1) and it would only work for values less than 31 (number of days in January).

        Original posts updated to reflect my laxity.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.