Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

some problem with date

by Anonymous Monk
on Apr 13, 2007 at 13:25 UTC ( [id://609908]=perlquestion: print w/replies, xml ) Need Help??

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

i am a novice to perl scripting. i have tried this code to calculate the age of the file in a directory.But i am not able to get the files created on the previous day using this.And i also want to copy it to a some directory in xp.Not sure what is wrong in this.
my($yesterday)=1; my($fileAgeInDays) = ((time() - (stat($eachFile))[9]) / 24 / 60 / 60); print $fileAgeInDays, " days since it was created\n\n"; if($fileAgeInDays = $yesterday) { print $eachFile,"\n"; } else { print " No files found \n"; }

Replies are listed 'Best First'.
Re: some problem with date
by ikegami (Patriarch) on Apr 13, 2007 at 13:38 UTC
    Three bugs
    • if($fileAgeInDays = $yesterday)

      That should be == instead of =.

    • my($fileAgeInDays) = ((time() - (stat($eachFile))[9]) / 24 / 60 / 60);

      incorrectly assumes all days are 24*60*60 seconds long.

    • my($fileAgeInDays) = ((time() - (stat($eachFile))[9]) / 24 / 60 / 60);

      returns an incorrect answer when the time of day of the file modification time is less than today's time of day. For example, if today started 10 hours ago and the file was modified 20 hours ago, the file was created yesterday, but $fileAgeInDays is 0.

    Update: Changed the layout for readability, but not the content.

      hi Ah the operator problems. oops very silly ,
      my($fileAgeInDays) = ((time() - (stat($eachFile))[9]) / 24 / 60 / 60);
      is there any alternate method to copy the yesterday's logs to some different folder.
        Date::Calc provides Delta_Days.
        use strict; use warnings; use Date::Calc qw( Delta_Days ); use File::Spec qw( catfile ); my ($y, $m, $d) = (localtime())[5, 4, 3]; $y += 1900; $m += 1; my $dir = ...; opendir(my $dh, $dir) or die("Unable to open log dir \"$dir\": $!\n"); while (my $file_name = readdir($dh)) { my $file_full = catfile($dir, $file_name); next if ! -f $file_full; my $mtime = (stat($file_full))[9]; my ($fy, $fm, $fd) = (localtime($mtime))[5, 4, 3]; $fy += 1900; $fm += 1; my $delta_days = Delta_Days(($y, $m, $d), ($fy, $fm, $fd)); if ($delta_days == 1) { ...copy the file... } }
Re: some problem with date
by NetWallah (Canon) on Apr 13, 2007 at 13:39 UTC
    You have the standard Newbie bug - using the ASSIGNMENT operator, instad of the comparison. Try:
    if($fileAgeInDays == $yesterday)

         "Choose a job you like and you will never have to work a day of your life" - Confucius

Re: some problem with date
by johngg (Canon) on Apr 13, 2007 at 14:05 UTC
    To save you a little typing and make your code slightly clearer you can take advantage of the fact that variables interpolate inside double-quoted strings. So, instead of

    print $fileAgeInDays, " days since it was created\n\n";

    you can do

    print "$fileAgeInDays days since it was created\n\n";

    Note that interpolation does not occur inside single-quoted strings.

    I hope this is of use to you.

    Cheers,

    JohnGG

    Update: Corrected missing dollar sigil. Thanks, ikegami.

      That should be
      print "$fileAgeInDays days since it was created\n\n";
        Gah! Cut'n'paste error. Corrected, thanks.
Re: some problem with date
by ptum (Priest) on Apr 13, 2007 at 14:34 UTC

    While not related to your original question, I have often preferred to use File::stat to work with stat in a way that is a little more readable.

Re: some problem with date
by Krambambuli (Curate) on Apr 13, 2007 at 14:53 UTC
    Just a word of caution: dealing with dates and times is much trickier then it might look at first sight.

    Think for example that even for common situations, the 'distance' between yesterday and today might be anything between 1 second (yesterday 23:59:50 and today 00:00:00) and 48h - 1 second (yesterday 00:00:00 and today 23:59:59).

    If it happens that it is 'one of those days' when daylight saving times changes, the day might have 25h or only 23h... And so on.

    You might want to have a look on the documentation of Date::Manip to get some oversight; don't be scared, you'll soon find a reliable friend in it, but just realize that depending on the accuracy you need you might want somewhat different approaches.
      Thank you Krambambuli will there be any problem in copying yesterdays file.
        Well... once you know which they are, I guess not (or that's another problem), but it is important to correctly define what are 'yesterday files'.

        I think it would be not an decision based on file_age_in_days, but rather something like 'those files that have creation date between yesterday 00:00:00 and today 00:00:00'.

        Something like below:
        use strict; use warnings; use Date::Manip; use File::stat; my $yesterday = ParseDate( 'yesterday midnight' ); my $today = ParseDate( 'today midnight' ); my $yesterday_midnight = &UnixDate( $yesterday, "%s" ); my $today_midnight = &UnixDate( $today, "%s" ); my @yesterday_files = (); foreach my $file ( <*> ) { my $stat = stat( $file ) or die "Cannot stat file $file: $!\n"; my $creation_date = $stat->ctime; push( @yesterday_files, $file ) if ( $creation_date < $today_midnight and $creation_date > $yesterday_midnight ); } foreach my $file (@yesterday_files) { # process, copy, ... }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://609908]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-19 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found