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.
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] |
|
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...
}
}
| [reply] [d/l] [select] |
|
|
|
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
| [reply] [d/l] |
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. | [reply] [d/l] [select] |
|
print "$fileAgeInDays days since it was created\n\n";
| [reply] [d/l] |
|
Gah! Cut'n'paste error. Corrected, thanks.
| [reply] |
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.
| [reply] |
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.
| [reply] |
|
Thank you Krambambuli
will there be any problem in copying yesterdays file.
| [reply] |
|
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, ...
}
| [reply] [d/l] |