Right, I would encourage you to post your full script, as rovf suggested, and let us know exactly what error you are having. What are $epoch_timestamp and $nowEpoc? Depending on how you're defining those variables, that may be part of your problem.

JavaFan certainly offers a simpler way to get what you're looking for (the script start time minus the file modification time, in days). For more info about the -M function, you can see the perlfunc manpage or here.

But, in terms of the way you're trying to do it:

I presume, also, that you're using File::stat to get the object-oriented interface to the stat function -- which, as JavaFan pointed out, returns a list without File::stat. (I like to use File::stat, too, since I can never remember what order stat returns things in.)

I am confused by what you're trying to do with time and localtime and $epoch_timestamp and all your other variables. This shouldn't be that complicated.

mtime from stat returns the file modification timestamp, in seconds since the epoch. The time function returns the current system time, in seconds since the epoch. localtime is used to split the seconds-since-the-epoch time from time into the current day, month, year, hour, second, etc. It also returns, in scalar context, the formatted ctime (e.g. Thu Oct 13 04:54:34 1994). You probably know all this.

I would simplify what you have above, like this:

#!/usr/bin/perl use strict; use warnings 'all'; use File::stat; # use the object-oriented interface to stat my $file = "rajesh.txt"; my $file_timestamp = stat($file)->mtime; # gets the file-modified ti +me, in # seconds since the epoch # my $startTime = time(); # You don't even really nee +d this -- Perl has a # built-in variable, $^T, t +hat records when your # script started to run. Se +e the perlvar manpage. my $timestamp = localtime($^T); # localtime, with no argume +nt, assumes # localtime(time). My examp +le gets the localtime of $^T. # Assigning this to a scala +r variable, as # you do here, gives you th +e # formatted ctime. You also + could # have done localtime(time) + here, # or just localtime with no + argument. my $rajesh = $^T - $file_timestamp; # This is really all you ne +ed to do here. You could # also use the time functio +n to get the current time, # i.e. $rajesh = time - $fi +le_timestamp my $pretty_file_timestamp = localtime($file_timestamp); # Get the sca +lar (ctime) format print "The current time is = $timestamp\n"; print "The file modified time is = $pretty_file_timestamp\n"; print "The difference between file modified time and current time is = + $rajesh\n";

Some other things that might be worth knowing:

If you don't want to bother with creating a new variable, you can always force a scalar return from localtime, like so:

print "The current time is ", scalar(localtime), "\n";

Also, if you want to make even prettier time and date formats than ctime, e.g. "Thursday, February 23, 2012, 11:41 a.m.", then Date::Format, in the TimeDate bundle, is a handy module.


In reply to Re: compare file modification time and current time by LonelyPilgrim
in thread compare file modification time and current time by rajeshatbuzz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.